gpt4 book ai didi

html - CSS,从新网页开始,试图让导航栏位于中间,宽度为 95%(每边 2.5% 空闲)

转载 作者:行者123 更新时间:2023-11-28 03:58:04 25 4
gpt4 key购买 nike

我试图让这段代码在中间和 95%(每边 2.5% 空闲)处显示我的导航栏。我在互联网上找到了一些方法,但它们会要求我使用不同的样式,我想保持不变。

#nav ul {
position: fixed;
top: 5px;
width: 100%;
list-style-type: none;
margin: 0;
margin-left: 10%;
padding: 0;
overflow: hidden;
background-color: #ffffff;
}

#nav li {
float: left;
}

#nav li a {
display: block;
color: #0066cc;
text-align: center;
padding: 10px 20px;
text-decoration: none;
}

#nav li a:hover {
background-color: #111;
}

#nav .active {
background-color: #ffffff;
border-top-width: 5px;
border-top-style: solid;
border-top-color: #0066cc;
border-bottom-width: 5px;
border-bottom-style: solid;
border-bottom-color: #0066cc;
}

#nav .inactive {
border-width: 5px;
border-top-style: solid;
border-top-color: #ffffff;
border-top-color: #0066cc;
border-bottom-width: 5px;
border-bottom-style: solid;
border-bottom-color: #0066cc;
}

Html part:
  <div id="nav">
<ul>
<li><a class="active" href="../HTML/Home.html">Home</a></li>
<li><a class="inactive" href="../HTML/Shop.html">Shop</a></li>
<li><a class="inactive" href="../HTML/SocialNews.html">Social News</a></li>
<li><a class="inactive" href="../HTML/Gallary.html">Gallary</a></li>
<li><a class="inactive" href="../HTML/CustomerReviews.html">Customer Reviews</a></li>
<li><a class="inactive" href="../HTML/Sponsorship.html">Sponsorship</a></li>
<li><a class="inactive" href="../HTML/Contact.html">Contact</a></li>
<li><a class="inactive" href="../HTML/Cart.html">Cart</a></li>
</ul>
</div>

最佳答案

使用宽度:95%;左:50%; transform: translateX(-50%); 使菜单宽度为 95% 并居中。

然后将 display: flex 添加到菜单并将 flex: 1 0 0 添加到 li 以拉伸(stretch)这些元素将填满菜单。

#nav ul {
position: fixed;
top: 5px;
list-style-type: none;
padding: 0;
overflow: hidden;
background-color: #ffffff;
width: 95%;
display: flex;
left: 50%;
transform: translateX(-50%);
}

#nav li {
flex: 1 0 0;
}

#nav li a {
display: block;
color: #0066cc;
text-align: center;
padding: 10px 20px;
text-decoration: none;
height: 100%;
box-sizing: border-box;
}

#nav li a:hover {
background-color: #111;
}

#nav .active {
background-color: #ffffff;
border-top-width: 5px;
border-top-style: solid;
border-top-color: #0066cc;
border-bottom-width: 5px;
border-bottom-style: solid;
border-bottom-color: #0066cc;
}

#nav .inactive {
border-width: 5px;
border-top-style: solid;
border-top-color: #ffffff;
border-top-color: #0066cc;
border-bottom-width: 5px;
border-bottom-style: solid;
border-bottom-color: #0066cc;
}
<div id="nav">
<ul>
<li><a class="active" href="../HTML/Home.html">Home</a></li>
<li><a class="inactive" href="../HTML/Shop.html">Shop</a></li>
<li><a class="inactive" href="../HTML/SocialNews.html">Social News</a></li>
<li><a class="inactive" href="../HTML/Gallary.html">Gallary</a></li>
<li><a class="inactive" href="../HTML/CustomerReviews.html">Customer Reviews</a></li>
<li><a class="inactive" href="../HTML/Sponsorship.html">Sponsorship</a></li>
<li><a class="inactive" href="../HTML/Contact.html">Contact</a></li>
<li><a class="inactive" href="../HTML/Cart.html">Cart</a></li>
</ul>
</div>

关于html - CSS,从新网页开始,试图让导航栏位于中间,宽度为 95%(每边 2.5% 空闲),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43353906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com