如何将水平居中对齐菜单?
我需要居中对齐水平菜单。
我尝试过各种解决方案,包括 inline-block
/ block
/ center-align
等的混合,但没有成功。
这是我的代码:
<div class="topmenu-design">
<!-- Top menu content: START -->
<ul id="topmenu firstlevel">
<li class="firstli" id="node_id_64"><div><a href="#"><span>Om kampanjen</span></a></div></li>
<li id="node_id_65"><div><a href="#"><span>Fakta om inneklima</span></a></div></li>
<li class="lastli" id="node_id_66"><div><a href="#"><span>Statistikk</span></a></div></li>
</ul>
<!-- Top menu content: END -->
</div>
更新
我知道如何在div
内居中对齐ul
。这可以通过 Sarfraz 的建议来实现。 但列表项仍然在 ul
内向左浮动。
我需要 JavaScript 来完成这个任务吗?
I need to center align a horizontal menu.
I've tried various solutions, including the mix of inline-block
/ block
/ center-align
etc., but haven't succeeded.
Here is my code:
<div class="topmenu-design">
<!-- Top menu content: START -->
<ul id="topmenu firstlevel">
<li class="firstli" id="node_id_64"><div><a href="#"><span>Om kampanjen</span></a></div></li>
<li id="node_id_65"><div><a href="#"><span>Fakta om inneklima</span></a></div></li>
<li class="lastli" id="node_id_66"><div><a href="#"><span>Statistikk</span></a></div></li>
</ul>
<!-- Top menu content: END -->
</div>
UPDATE
I know how to center align the ul
within the div
. That can be accomplished using Sarfraz's suggestion.
But the list items are still floated left within the ul
.
Do I need Javascript to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
来自http://pmob.co.uk/pob/centred-float.htm:
代码
From http://pmob.co.uk/pob/centred-float.htm:
Code
这对我有用。如果我没有误解你的问题,你可以尝试一下。
This works for me. If I haven't misconstrued your question, you might give it a try.
使用 CSS3 弹性盒。简单的。
With CSS3 flexbox. Simple.
这是我发现的最简单的方法。我用了你的html。填充只是为了重置浏览器默认值。
This is the simplest way I found. I used your html. The padding is just to reset browser defaults.
这是一篇很好的文章,介绍了如何以非常可靠的方式做到这一点,无需任何黑客攻击和完整的跨浏览器支持。对我有用:
--> http://matthewjamestaylor.com/blog/beautiful-css-居中菜单无黑客完全跨浏览器支持
Here's a good article on how to do it in a pretty rock-solid way, without any hacks and full cross-browser support. Works for me:
--> http://matthewjamestaylor.com/blog/beautiful-css-centered-menus-no-hacks-full-cross-browser-support
试试这个:
Try this:
这样做:
和CSS:
Do it like this :
And the CSS:
和你们很多人一样,我已经为此苦苦挣扎了一段时间。最终的解决方案与包含 UL 的 div 有关。所有关于改变 UL 的填充、宽度等的建议都没有效果,但以下建议却有效果。
这一切都与包含 div 上的
margin:0 auto;
有关。我希望这对一些人有帮助,并感谢所有已经将其与其他事情结合起来提出建议的人。Like so many of you, I've been struggling with this for a while. The solution ultimately had to do with the div containing the UL. All suggestions on altering padding, width, etc. of the UL had no effect, but the following did.
It's all about the
margin:0 auto;
on the containing div. I hope this helps some people, and thanks to everyone else who already suggested this in combination with other things.演示 - http://codepen.io/grantex/pen/InLmJ
天哪,干净多了。
Demo - http://codepen.io/grantex/pen/InLmJ
Omg so much cleaner.
一般来说,将黑电平元素(如
)居中的方法是使用
margin:auto;
属性。要对齐块级元素内的文本和内联级元素,请使用
text-align:center;
。所以像……这样的东西一起应该起作用。
边缘情况是 Internet Explorer6...甚至在不使用
时的其他 IE。 IE6 使用
text-align
错误地对齐块级元素。因此,如果您希望支持 IE6(或不使用),您的完整解决方案是...
作为脚注,我认为
id="topmenu firstlevel"< /code> 无效,因为
id
属性不能包含空格...?事实上,w3c 建议定义了id
属性作为 'name' 类型。 ..Generally speaking the way to center a black level element (like a
<ul>
) is using themargin:auto;
property.To align text and inline level elements within a block level element use
text-align:center;
. So all together something like...... should work.
The fringe case is Internet Explorer6... or even other IEs when not using a
<!DOCTYPE>
. IE6 incorrectly aligns block level elemnts usingtext-align
. So if you're looking to support IE6 (or not using a<!DOCTYPE>
) your full solution is...As a footnote, I think
id="topmenu firstlevel"
is invalid as anid
attribute can't contain spaces... ? Indeed the w3c recommendation defines theid
attribute as a 'name' type...我使用了 display:inline-block 属性:解决方案包括使用固定宽度的包装器。在内部,ul 块和 inline-block 用于显示。使用这个,ul只需获取真实内容的宽度!最后 margin: 0 auto,使该内联块居中 =)
I used the display:inline-block property: the solution consist in use a wrapper with fixed width. Inside, the ul block with the inline-block for display. Using this, the ul just take the width for the real content! and finally margin: 0 auto, to center this inline-block =)
我为此使用 jquery 代码。 (替代解决方案)
i use jquery code for this. (Alternative solution)
ul 作为内联表修复了 with 问题。我使用父 div 将文本居中对齐。
这样即使在其他语言中也看起来不错(翻译,不同的宽度)
ul as inline-table fixes the with issue. I used the parent div to align the text to center.
this way it looks good even in other languages (translation, different width)
@Robusto 的解决方案对于我想做的事情来说是最简单的,我建议你使用它。我试图对无序列表中的图像做同样的事情来制作画廊......我做了一个 js 小提琴来玩弄它。欢迎在这里尝试一下。
[它是使用robusto的示例代码设置的]
HTML:
CSS:
@Robusto's solution was the simplest for what I was trying to do, I suggest you use it. I was trying to do the same thing for images in an unordered list to make a gallery... I made a js fiddle to fool around with it. Feel free to try it here.
[it was set up using robusto's sample code]
HTML:
CSS:
在大屏幕上是一个不错的近似值。它不好,但是一个很好的肮脏修复。
Is a decent approximation on big screens. Its not good, but a good dirty fix.
对我有用的只是将
li
项的display
属性设置为inline-flex
:您可以选择将
justify-content: center
添加到li
中,并将padding: 0
添加到ul
中理顺事情。What worked for me was just setting the
li
item'sdisplay
property toinline-flex
:You may choose to add
justify-content: center
to theli
s, andpadding: 0
to theul
to straighten things out.就这些!
That all!