如何证明水平列表的合理性?
我有一个水平导航栏,如下所示:
<ul id = "Navigation">
<li><a href = "About.html">About</a></li>
<li><a href = "Contact.html">Contact</a></li>
<!-- ... -->
</ul>
我使用 CSS 删除项目符号点并使其水平。
#Navigation li
{
list-style-type: none;
display: inline;
}
我试图证明文本合理,以便每个链接均匀分布以填充 ul
的整个空间。我尝试将 text: justify
添加到 li
和 ul
选择器,但它们仍然左对齐。
#Navigation
{
text-align: justify;
}
#Navigation li
{
list-style-type: none;
display: inline;
text-align: justify;
}
这很奇怪,因为如果我使用 text-align: right
,它的行为会按预期进行。
如何均匀分布链接?
I have a horizontal navbar like the following:
<ul id = "Navigation">
<li><a href = "About.html">About</a></li>
<li><a href = "Contact.html">Contact</a></li>
<!-- ... -->
</ul>
I use CSS to remove the bullet points and make it horizontal.
#Navigation li
{
list-style-type: none;
display: inline;
}
I'm trying to justify the text so each link is spread out evenly to fill up the entirety of the ul
's space. I tried adding text: justify
to both the li
and ul
selectors, but they're still left-aligned.
#Navigation
{
text-align: justify;
}
#Navigation li
{
list-style-type: none;
display: inline;
text-align: justify;
}
This is strange, because if I use text-align: right
, it behaves as expected.
How do I spread out the links evenly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
现代方法 - CSS3 Flexboxes
现在我们有了 CSS3 flexboxes,您不再需要求助于技巧和解决方法,以便使这项工作有效。幸运的是,浏览器支持已经取得了长足的进步,我们大多数人都可以开始使用 Flexbox。
只需将父元素的
display
设置为flex
,然后更改justify-content
属性 到空格
或空格< /code>
以便在子 Flexbox 项目之间/周围添加空间。然后添加其他供应商前缀以获得更多浏览器支持。
使用
justify-content: space- Between
- (示例在这里):使用
justify-content: space-around
- (示例在这里):Modern Approach - CSS3 Flexboxes
Now that we have CSS3 flexboxes, you no longer need to resort to tricks and workarounds in order to make this work. Fortunately, browser support has come a long way, and most of us can start using flexboxes.
Just set the parent element's
display
toflex
and then change thejustify-content
property to eitherspace-between
orspace-around
in order to add space between/around the children flexbox items. Then add additional vendor prefixes for more browser support.Using
justify-content: space-between
- (example here):Using
justify-content: space-around
- (example here):你需要使用一个“技巧”来完成这项工作。
参见: http://jsfiddle.net/2kRJv/
HTML :
CSS:
IE6/7 欺骗细节:内联块在 Internet Explorer 7、6 中不起作用
You need to use a "trick" to make this work.
See: http://jsfiddle.net/2kRJv/
HTML:
CSS:
Details on IE6/7 trickery: Inline block doesn't work in internet explorer 7, 6
这也可以通过使用
ul
元素上的伪元素来实现:This can also be achieved using a pseudo element on the
ul
element:只要这样做:
Just do:
这可能适合您的需求:
演示:http://jsfiddle.net/KmqzQ/
This might suit your needs:
demo : http://jsfiddle.net/KmqzQ/
HTML
CSS
查看演示:http://jsfiddle.net/2kRJv/392/
HTML
CSS
View demo: http://jsfiddle.net/2kRJv/392/
您需要有
或者至少在打开和关闭之间具有空间
You need to have the <li> elements separated, otherwise the justify won't work.
or at least have spaces between the opening and closing <li> tags.
此博客具有令人满意的稳健性解决方案。不过,它需要一些细微的更改才能适应
ul/li
导航:http:// jsfiddle.net/mblase75/9vNBs/
This blog has a satisfyingly robust solution. It needs some slight changes to accommodate a
ul/li
navigation, though:http://jsfiddle.net/mblase75/9vNBs/
如果标记的答案中有空格,则该答案不起作用。
这里的最佳答案适用于空格
我如何*真正*证明 HTML 中的水平菜单合理+CSS?
The marked answer does not work if has a white space in it.
The top answer here works with white spaces
How do I *really* justify a horizontal menu in HTML+CSS?
使用 CSS Flexbox
Using CSS Flexboxes