保证金汽车噩梦
我尝试了所有我能想到的方法,但我一生都无法弄清楚为什么导航菜单不会居中。我已经尝试过文本对齐、自动边距、块显示...在父级和子级上。我确信这是非常简单的事情,但这开始导致脱发。
该演示可在此处获取:
如果您想查看一些代码,这里是 HTML :
<div id="navigation" class="col-full">
<ul id="main-nav" class="nav fl">
<li id="menu-item-266" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-266"><a href="http://previews.tinygiantstudios.co.uk/">Home</a></li>
<li id="menu-item-29" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-29"><a href="http://previews.tinygiantstudios.co.uk/about-us/">About Us</a></li>
<li id="menu-item-38" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-38"><a href="http://previews.tinygiantstudios.co.uk/news/">News</a></li>
<li id="menu-item-28" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-28"><a href="http://previews.tinygiantstudios.co.uk/contact/">Contact</a></li>
</ul>
</div><!-- /#navigation -->
还有 CSS
#navigation {
margin-bottom: 0px;
background-color: #131313;
border-top: 0px solid #DBDBDB;
border-bottom: 0px solid #DBDBDB;
border-left: 0px solid #DBDBDB;
border-right: 0px solid #DBDBDB;
border-radius: 0px;
-moz-border-radius: 0px;
-webkit-border-radius: 0px;
font: 14px/14px sans-serif;
padding: 10px 0;
}
.nav {
z-index: 99;
margin: 0;
padding: 0;
list-style: none;
line-height: 1;
margin-left: 10px;
}
.fl {
float: left;
}
.nav li {
float: left;
width: auto;
}
谁能告诉我我缺少什么?
谢谢,
I'v tried every way I can think of, but for the life of me I can't figure our why the navigation menu won't center. I've tried text-align, margin-auto, block display... On both the parent and child. I'm sure it's something uber-simple but this is starting to cause hair-loss.
The demo is available here:
If you'd like to see some code, here's the HTML:
<div id="navigation" class="col-full">
<ul id="main-nav" class="nav fl">
<li id="menu-item-266" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-266"><a href="http://previews.tinygiantstudios.co.uk/">Home</a></li>
<li id="menu-item-29" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-29"><a href="http://previews.tinygiantstudios.co.uk/about-us/">About Us</a></li>
<li id="menu-item-38" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-38"><a href="http://previews.tinygiantstudios.co.uk/news/">News</a></li>
<li id="menu-item-28" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-28"><a href="http://previews.tinygiantstudios.co.uk/contact/">Contact</a></li>
</ul>
</div><!-- /#navigation -->
And the CSS
#navigation {
margin-bottom: 0px;
background-color: #131313;
border-top: 0px solid #DBDBDB;
border-bottom: 0px solid #DBDBDB;
border-left: 0px solid #DBDBDB;
border-right: 0px solid #DBDBDB;
border-radius: 0px;
-moz-border-radius: 0px;
-webkit-border-radius: 0px;
font: 14px/14px sans-serif;
padding: 10px 0;
}
.nav {
z-index: 99;
margin: 0;
padding: 0;
list-style: none;
line-height: 1;
margin-left: 10px;
}
.fl {
float: left;
}
.nav li {
float: left;
width: auto;
}
Can anyone show me what I'm missing?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要将边距设置为自动,您需要指定宽度,并将元素显示为块。
To set margins to auto, you need to specify a width, and display the element as block.
将边距设置为零/自动仅适用于固定宽度的块元素。删除内联浮动使其生效。
Setting the margin to zero/automatic only works with fixed width block elements. Remove the inline float for it to take effect.
要使用
margin: 0 auto;
居中,您的元素必须是display: block;
,具有固定宽度,并且不能使用浮动浮动。
因此,您需要从
中删除
fl
类,并为其指定固定宽度。For centering with
margin: 0 auto;
your element must bedisplay: block;
, have a fixed width and cannot be floated withfloat
.So you need to remove the
fl
class from your<ul>
and give it a fixed width.您应该为
#navigation
或#main-nav
指定宽度,现在 #navigation 跨越了
#wrapper
的整个宽度,因此永远无法中心。将其宽度设置为 500 像素左右即可解决问题。如果您想将 main-nav 居中而不是导航,则应删除其中的
float:left;
并为其指定固定宽度和margin: auto;
You should either specify a width for
#navigation
or#main-nav
Right now #navigation spans the entire width of
#wrapper
and thus cant ever center. Making it around 500px wide will fix the the issues.If you want to center main-nav instead of navigation, you should remove the
float:left;
of it and give it a fixed width andmargin: auto;
将此代码添加到您的 CSS 中:
它不适用于旧版本的 IE (
inline-block
),但是什么可以呢?Add this code to your CSS:
It won't work with older versions of IE (
inline-block
), but what will?