html - 需要帮助将我的导航栏链接居中,因为在右侧添加了搜索栏。任何人?
问题描述
我的链接很好地位于我的徽标下方的中心,现在我添加了一个搜索栏并将其放在右侧,因为我想要它,我现在唯一的问题是我不能再让我的链接居中了:-/ 任何人都知道一个解决方案所以我仍然可以将导航栏链接居中吗?
/*Topnav*/
.topnav {
width: 100%;
opacity: 1;
background-color: rgba(255,255,255,0.6);
margin-bottom: 10px;
padding: 5px 0px 5px 0;
border-bottom: dotted #66A761;
border-top: dotted #66A761;
}
.topnav ul {
text-align: center;
}
.topnav ul li {
display: inline;
margin-left: 15px;
}
.topnav a {
font-size: 20px;
font-weight: bold;
text-decoration: none;
}
.topnav a:visited {
color: #FFF;
}
.topnav a:link {
color :#9F257D;
}
.topnav a:hover {
color: #66A761;
}
.topnav input {
float: right;
padding: 3px;
margin: 0 5px;
}
<!--Topnav-->
<div class="topnav">
<nav>
<ul>
<li><a href="aboutblank.html">recepten</a></li>
<li><a href="aboutblank.html">varianten</a></li>
<li><a href="aboutblank.html">contact</a></li>
<li><a href="aboutblank.html">over ons</a></li>
<input type="text" placeholder="Search..">
</ul>
</nav>
</div>
解决方案
对 css 进行以下更改
.topnav input {
float: right;
padding: 3px;
margin: 0 5px;
position: absolute; //input will hover on the navbar
right: 20px; //this gives a bit of padding to the right
}
.topnav {
width: 100%;
opacity: 1;
background-color: rgba(255, 255, 255, 0.6);
margin-bottom: 10px;
padding: 5px 0px 5px 0;
border-bottom: dotted #66A761;
border-top: dotted #66A761;
position:relative; //this allows the navbar to be the anchor to child elements with position absolute
}
导航按钮没有居中,因为搜索栏的位置是静态的,因此如果您将其设置为绝对,它将浮动在您的导航栏上。请注意,您必须将导航栏更改为相对位置,因为您希望搜索栏锚定到该元素。
HTML:
<div class="topnav">
<nav>
<ul>
<li><a href="aboutblank.html">recepten</a></li>
<li><a href="aboutblank.html">varianten</a></li>
<li><a href="aboutblank.html">contact</a></li>
<li><a href="aboutblank.html">over ons</a></li>
<input type="text" style="right:0" placeholder="Search..">
</ul>
</nav>
</div>
推荐阅读
- android - Recyclerview 测量滚动距离以禁用键盘
- windows - 如何从命令行编译visual suidop 2019 c#解决方案而无需在Windows上打开IDE?
- java - Intellij 无法在运行配置中的运行配置中设置系统属性
- javascript - 使用 webpack 包含两次 Primer react-occticons?
- node.js - 如何处理较大的节点 js 请求?
- javascript - 离子:点击更改离子列表项目
- foreach - Stata中的foreach命令
- docker - 在容器中的 Tomcat 中运行 Pulse 时,我需要在 Docker 容器上打开哪些端口?
- azure - 从没有项目结果的天蓝色搜索中获取所有“方面”
- python - 使用 pandas 从时间序列数据中删除异常值