我是CSS的新手,我正在制作导航栏。目前,我的导航栏位于左侧,我想将整个栏移到中间位置。我怎样才能做到这一点?
以下是代码。提前谢谢!
/* Navigation bar */
#navigation_bar {
list-style-type: none;
margin: 0;
position: fixed;
background-color: #333;
top: 0;
Left: 0;
width: 100%;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active), .dropdown:hover .dropbtn {
background-color: #111;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
}
.active {
background-color: #4CAF50;
}
<!--Navigation bar-->
<ul id="navigation_bar">
<li><a class="active" href="navigation_bar/home.html">Home.</a></li>
<li><a href="navigation_bar/promotion.html">Promotion.</a></li>
<!--drop down menu-->
<li class="dropdown"><a href="navigation_bar/hot_products.html" class="dropbtn">Hot Products.</a>
<div class="dropdown-content">
<a herf="navigation_bar/sub_menu/sandwiches.html">Sandwiches</a>
<a herf="navigation_bar/sub_menu/burger.html">Burger</a>
<a herf="navigation_bar/sub_menu/rice.html">Rice</a>
<a herf="navigation_bar/sub_menu/noodles.html">Noddles</a>
</div>
</li>
<!--Back to normal-->
<li><a href="navigation_bar/cold_products.html">Cold Products.</a></li>
<li><a href="navigation_bar/snacks.html">Snacks.</a></li>
<li><a href="navigation_bar/about_us.html">About Us.</a></li>
<li><a href="navigation_bar/contact_us.html">Contact Us.</a></li>
</ul>
答案 0 :(得分:3)
使用
更新#navigation_bar
#navigation_bar {
display: flex;
align-items: center;
justify-content: center;
padding: 0;
}
以下是工作 Demo
#navigation_bar {
list-style-type: none;
margin: 0;
position: fixed;
background-color: #333;
top: 0;
Left: 0;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
padding: 0;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active), .dropdown:hover .dropbtn {
background-color: #111;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
}
.active {
background-color: #4CAF50;
}
<ul id="navigation_bar">
<li><a class="active" href="navigation_bar/home.html">Home.</a></li>
<li><a href="navigation_bar/promotion.html">Promotion.</a></li>
<!--drop down menu-->
<li class="dropdown"><a href="navigation_bar/hot_products.html" class="dropbtn">Hot Products.</a>
<div class="dropdown-content">
<a herf="navigation_bar/sub_menu/sandwiches.html">Sandwiches</a>
<a herf="navigation_bar/sub_menu/burger.html">Burger</a>
<a herf="navigation_bar/sub_menu/rice.html">Rice</a>
<a herf="navigation_bar/sub_menu/noodles.html">Noddles</a>
</div>
</li>
<!--Back to normal-->
<li><a href="navigation_bar/cold_products.html">Cold Products.</a></li>
<li><a href="navigation_bar/snacks.html">Snacks.</a></li>
<li><a href="navigation_bar/about_us.html">About Us.</a></li>
<li><a href="navigation_bar/contact_us.html">Contact Us.</a></li>
</ul>
答案 1 :(得分:1)
通过更改CSS
代码的一小部分,您可以轻松地执行以下操作:
#navigation_bar {
text-align: center;
}
#navigation_bar li {
float: none;
display: inline-block;
vertical-align: top;
}
li .dropdown-content a:hover:not(.active) {
background: #d0d0d0; /* for altering the hover effect on submenus */
}
我还创建了一个JSFiddle。
答案 2 :(得分:0)
将 navigation_bar css 更改为:
#navigation_bar {
list-style-type: none;
margin: 0 auto;
position: fixed;
background-color: #333;
top: 0;
width: auto;
display: block;
float: left;
}
答案 3 :(得分:0)
试试这个
#navigation_bar{ text-align: center;} /*add align center */
li {display: inline; /* float:left; */} /* remove float left here and add display inline */