gpt4 book ai didi

html - 如何让我的菜单在所有显示器上居中

转载 作者:太空宇宙 更新时间:2023-11-03 20:28:56 24 4
gpt4 key购买 nike

http://jsfiddle.net/aw4zgwso/

这是目前正在发生的事情,我似乎无法将导航菜单居中,除非我将 width:40% 添加到 .menu 但那不是效果不佳,并且会在较小分辨率的显示器上将文本打倒

.nav { 
width:100%;
height:40px;
display:block;
background-color:#313131;
}

.nav_inner {
height:100%;
margin:0 auto;
position:relative;
}


.nav_menu {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
height: 40px;
line-height: 15px;

}

.nav_menu li {
float: left;
}

.nav_menu li a {
display: block;
color:#ffffff;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

.nav_menu a:hover {
background-color: red;
color:white;
}

.clearfix:after {
display:block;
clear:both;
}

/*----- Menu Outline -----*/
.menu-wrap {
width:100%;
background:#3e3436;
}

.menu {
margin:0px auto;
}

.menu li {
margin:0px;
list-style:none;
}

.menu a {
color:#ffffff;
text-decoration:none;
}

.menu li:hover > a, .menu .current-item > a {
text-decoration:none;
color:#ffffff;
}

.menu .arrow {
font-size:12px;
line-height:0%;
color:#00AAFF;
padding-left: 5px;
}

/*----- Top Level -----*/
.menu > ul > li {
float:left;
display:inline-block;
position:relative;
font-size:16px;
color: #ffffff;
}

.menu > ul > li > a {
padding:9px 40px;
display:inline-block;
}

.menu > ul > li:hover > a, .menu > ul > .current-item > a {
background:#191919;
}

/*----- Bottom Level -----*/
.menu li:hover .sub-menu {
z-index:1;
opacity:1;
}

.sub-menu {
width:160%;
padding:5px 0px;
position:absolute;
top:95%;
left:0px;
z-index:-1;
opacity:0;
transition:opacity linear 0.15s;
background:#313131;
}

.sub-menu li {
display:block;
font-size:16px;
}

.sub-menu li a {
padding:10px 30px;
display:block;
}

.sub-menu li a:hover, .sub-menu .current-item a {
background:#191919;
}
<div class="nav">
<div class="nav_inner">
<nav class="hide-mobile menu">
<ul class="clearfix">
<li><a href="index.html">Home</a></li>
<li><a href="#" style="cursor: default;">About us<span class="arrow">&#9660;</span></a>
<ul class="sub-menu">
<li><a href="our-values.html">Our Values</a></li>
<li><a href="technology-partners.html">Technology Partners</a></li>
</ul>
</li>
<li><a href="#" style="cursor: default;">IT Solutions<span class="arrow">&#9660;</span></a>
<ul class="sub-menu">
<li><a href="audio-visual.html">Audio Visual</a></li>
<li><a href="backup-replication-business-continuity.html">Backup, Replication and
<li><a href="visitor-and-entry-management.html">Visitor &amp; Entry Management</a></li>
</ul>
</li>
<li><a href="contact-us.html">Contact Us</a></li>
</ul>
</nav>
</div>
</div>

最佳答案

您可以简单地使用 text-align:center 并像这样制作 li inline-block :

.menu>ul {
text-align: center;
margin: 0;
padding: 0;
}

.menu>ul>li {
display: inline-block;
position: relative;
font-size: 16px;
color: #ffffff;
}

这里是完整的代码

.nav {
width: 100%;
height: 40px;
display: block;
background-color: #313131;
}

.nav_inner {
height: 100%;
margin: 0 auto;
position: relative;
}

.nav_menu {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
height: 40px;
line-height: 15px;
}

.nav_menu li {
float: left;
}

.nav_menu li a {
display: block;
color: #ffffff;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

.nav_menu a:hover {
background-color: red;
color: white;
}

.clearfix:after {
display: block;
clear: both;
}


/*----- Menu Outline -----*/

.menu-wrap {
width: 100%;
background: #3e3436;
}

.menu {
margin: 0px auto;
}

.menu li {
margin: 0px;
list-style: none;
}

.menu a {
color: #ffffff;
text-decoration: none;
}

.menu li:hover>a,
.menu .current-item>a {
text-decoration: none;
color: #ffffff;
}

.menu .arrow {
font-size: 12px;
line-height: 0%;
color: #00AAFF;
padding-left: 5px;
}


/*----- Top Level -----*/

.menu>ul {
text-align: center;
margin: 0;
padding: 0;
}

.menu>ul>li {
display: inline-block;
position: relative;
font-size: 16px;
color: #ffffff;
}

.menu>ul>li>a {
padding: 9px 40px;
display: inline-block;
}

.menu>ul>li:hover>a,
.menu>ul>.current-item>a {
background: #191919;
}


/*----- Bottom Level -----*/

.menu li:hover .sub-menu {
z-index: 1;
opacity: 1;
}

.sub-menu {
width: 160%;
padding: 5px 0px;
position: absolute;
top: 95%;
left: 0px;
z-index: -1;
opacity: 0;
transition: opacity linear 0.15s;
background: #313131;
}

.sub-menu li {
display: block;
font-size: 16px;
}

.sub-menu li a {
padding: 10px 30px;
display: block;
}

.sub-menu li a:hover,
.sub-menu .current-item a {
background: #191919;
}
<div class="nav">
<div class="nav_inner">

<nav class="hide-mobile menu">
<ul class="clearfix">

<li><a href="index.html">Home</a></li>

<li><a href="#" style="cursor: default;">About us<span class="arrow">&#9660;</span></a>

<ul class="sub-menu">
<li><a href="our-values.html">Our Values</a></li>
<li><a href="technology-partners.html">Technology Partners</a></li>
</ul>

</li>

<li><a href="#" style="cursor: default;">IT Solutions<span class="arrow">&#9660;</span></a>

<ul class="sub-menu">
<li><a href="audio-visual.html">Audio Visual</a></li>
<li><a href="backup-replication-business-continuity.html">Backup, Replication and
<li><a href="visitor-and-entry-management.html">Visitor &amp; Entry Management</a></li>
</ul>

</li>



<li><a href="contact-us.html">Contact Us</a></li>

</ul>
</nav>

</div>
</div>

关于html - 如何让我的菜单在所有显示器上居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47053881/

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