[英]Horizontal Centered Menu in CSS?
I want to make a horizontal centered menu. 我想制作一个水平居中菜单。 I have tried using things like text align center and margin auto but can't get them to work.
我试过使用诸如文本居中对齐和边距自动等功能,但无法使它们正常工作。 I do not want to use a table.
我不想使用表格。
Here's my code: 这是我的代码:
<footer class="container">
<div class="row">
<div class="span12">
<ul>
<li>footer info 1</li>
<li>footer info 2</li>
<li>footer info 3</li>
</ul>
</div>
</div>
With the provided HTML: 使用提供的HTML:
ul { text-align: center; }
li { display: inline-block; } /* Don't float them */
The following will work without using text-align
: 以下将不使用
text-align
:
footer {
width: 100%;
}
.row {
position: absolute;
left: 50%;
}
.span12 {
position: relative;
left: -50%;
}
ul {
padding: 0;
}
li {
display: inline;
list-style: none;
margin-left: 1em;
}
li:first-child {
margin-left: 0;
}
The important bits are: 重要的位是:
(1) that the outer container for the menu has 100% width, (1)菜单的外部容器的宽度为100%,
(2) that the inner container is absolutely positioned at 50% left (which positions the left side of the menu at the center of the page), and (2)内部容器绝对位于左50%的位置(将菜单的左侧置于页面的中心),并且
(3) that the menu is then relatively positioned at -50% left (moving it back to the left half its width, so that the center of the menu is now at the center of the page). (3)然后将菜单相对地定位在-50%的左侧(将其移回到宽度的左侧,因此菜单的中心现在位于页面的中心)。
The other stuff is just cosmetic. 其他的东西只是化妆品。
See http://jsfiddle.net/aCSgz/ 参见http://jsfiddle.net/aCSgz/
Basically you need to set the ul and li to display: block. 基本上,您需要将ul和li设置为display:block。
ul { display: block; text-align:center; }
ul li { display: block; }
You need to set the display
property on the LIs to inline-block
and set the text-align
on the UL to center
. 您需要将LI上的
display
属性设置为inline-block
,并将UL上的text-align
设置为center
。
HTML: HTML:
<footer class="container">
<div class="row">
<div class="span12">
<ul>
<li>footer info 1</li>
<li>footer info 2</li>
<li>footer info 3</li>
</ul>
</div>
</div>
</footer>
CSS: CSS:
footer {
background:#fdd;
}
div.row {
background: #dfd;
}
ul {
background: #ddf;
list-style: none;
margin: 0;
padding: 0;
text-align: center;
}
li {
display: inline-block;
background: #fff;
margin: 0.5em;
padding: 0.5em;
}
http://jsfiddle.net/ghodmode/h2gT3/1/ http://jsfiddle.net/ghodmode/h2gT3/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.