我的 CSS 导航菜单拒绝在浏览器中居中?
我已经尝试了所有放置的位置
margin-right: auto
,并且
margin-left:auto
在实际链接上使用效果
display: inline-block
非常好,但如果作为父项添加,则会与菜单混淆。
我觉得为了使其在浏览器视口的中心,我需要失去一些样式。我觉得样式的某些方面之间存在冲突,导致无法居中。
我希望我是错的,并且根据某人的建议是可能的......
亲切的问候, Dale
这是我的 CSS:
.navigation {
height: auto
width: 96%;
border-top-color: rgb(119,120,122);
border-top-style: solid;
border-top-width: 1px;
border-bottom-color: rgb(119,120,122);
border-bottom-style: solid;
border-bottom-width: 1px;
margin-top: 1%;
margin-right: auto;
margin-bottom: 1%;
margin-left: auto;
padding-top: 1%;
padding-bottom: 1%;
background-color: transparent;
background-image: none;
}
ul {
height: auto;
overflow: hidden;
margin-right: auto;
margin-left: auto;
}
ul li {
display: inline;
}
ul li ul {
display: none;
}
ul li:hover ul {
height: auto;
width: 96%;
display: block;
position: absolute;
margin-right: auto;
margin-left: auto;
}
ul li a {
display: inline-block;
cursor: pointer;
position: relative;
margin-right: 0.15%;
margin-bottom: -0.2%;
padding-top: 1.25%;
padding-right: 1.4%;
padding-bottom: 1.25%;
padding-left: 1.4%;
}
ul li:hover ul a {
margin-right: -0.2%;
margin-bottom: 0;
}
PS 我删除了我的注释和任何非必要的代码,我意识到我是用长手 CSS 编写的,但我喜欢这样做;-)
I've tried all the places to put
margin-right: auto
and
margin-left:auto
The use of
display: inline-block
works brilliantly on the actual links, but otherwise messes with the menu if added as a parent.
I feel that in order to make it center in the browser viewport, that I will need to lose some of the styling. I feel there is a conflict between some aspect of the styling that is preventing the centering.
I hope that I'm wrong, and it is possible with somebody's suggestion(s)...
Kind Regards,
Dale
Here is my CSS:
.navigation {
height: auto
width: 96%;
border-top-color: rgb(119,120,122);
border-top-style: solid;
border-top-width: 1px;
border-bottom-color: rgb(119,120,122);
border-bottom-style: solid;
border-bottom-width: 1px;
margin-top: 1%;
margin-right: auto;
margin-bottom: 1%;
margin-left: auto;
padding-top: 1%;
padding-bottom: 1%;
background-color: transparent;
background-image: none;
}
ul {
height: auto;
overflow: hidden;
margin-right: auto;
margin-left: auto;
}
ul li {
display: inline;
}
ul li ul {
display: none;
}
ul li:hover ul {
height: auto;
width: 96%;
display: block;
position: absolute;
margin-right: auto;
margin-left: auto;
}
ul li a {
display: inline-block;
cursor: pointer;
position: relative;
margin-right: 0.15%;
margin-bottom: -0.2%;
padding-top: 1.25%;
padding-right: 1.4%;
padding-bottom: 1.25%;
padding-left: 1.4%;
}
ul li:hover ul a {
margin-right: -0.2%;
margin-bottom: 0;
}
P.S. I took out my comments and any non-essential codes, I realize that I write in long-hand CSS, but I like to do so ;-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
之所以没有居中显示,是因为保存菜单的 ul 没有指定宽度;因此,它以 100% 宽度渲染;换句话说,左/右页边距处于自动状态没有任何效果。
尝试将
text-align: center
添加到ul
中,以便内联的li
能够居中。The reason why it's not displaying centered is because the
ul
holding the menu has no width specified; therefore, it's rendering at 100% width; in other words, the left/right margins being at auto have no effect.Try adding
text-align: center
to theul
so that theli
s, being inline, will be able to be centered.最简单的方法是为顶层
添加宽度,例如:
编辑: 抱歉,我读错了问题,我以为您只是 em> 希望整个菜单不跨越窗口的整个宽度,而不是使菜单内容居中。正如 @Jacob 已经建议的那样,将
text-align:center;
添加到我的答案中。Simplest way is to add a width to your top level
<ul>
, for example:Edit: Sorry I misread the question, I thought you just wanted the entire menu to not span the entire width of the window, rather than center the menu contents. Added
text-align:center;
to my answer as @Jacob already suggested.