如何证明水平列表的合理性?

发布于 2024-11-27 07:33:56 字数 759 浏览 2 评论 0原文

我有一个水平导航栏,如下所示:

<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 添加到 liul 选择器,但它们仍然左对齐。

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

顾北清歌寒 2024-12-04 07:33:56

现代方法 - CSS3 Flexboxes

现在我们有了 CSS3 flexboxes,您不再需要求助于技巧和解决方法,以便使这项工作有效。幸运的是,浏览器支持已经取得了长足的进步,我们大多数人都可以开始使用 Flexbox。

只需将父元素的 display 设置为 flex,然后更改 justify-content 属性空格空格< /code>以便在子 Flexbox 项目之间/周围添加空间。然后添加其他供应商前缀以获得更多浏览器支持

使用 justify-content: space- Between - (示例在这里)

ul {
    list-style: none;
    padding: 0;
    margin: 0;
}
.menu {
    display: flex;
    justify-content: space-between;
}
<ul class="menu">
    <li>About</li>
    <li>Contact</li>
    <li>Contact Longer Link</li>
    <li>Contact</li>
</ul>


使用 justify-content: space-around - (示例在这里)

ul {
    list-style: none;
    padding: 0;
    margin: 0;
}
.menu {
    display: flex;
    justify-content: space-around;
}
<ul class="menu">
    <li>About</li>
    <li>Contact</li>
    <li>Contact Longer Link</li>
    <li>Contact</li>
</ul>

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 to flex and then change the justify-content property to either space-between or space-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):

ul {
    list-style: none;
    padding: 0;
    margin: 0;
}
.menu {
    display: flex;
    justify-content: space-between;
}
<ul class="menu">
    <li>About</li>
    <li>Contact</li>
    <li>Contact Longer Link</li>
    <li>Contact</li>
</ul>


Using justify-content: space-around - (example here):

ul {
    list-style: none;
    padding: 0;
    margin: 0;
}
.menu {
    display: flex;
    justify-content: space-around;
}
<ul class="menu">
    <li>About</li>
    <li>Contact</li>
    <li>Contact Longer Link</li>
    <li>Contact</li>
</ul>

深海夜未眠 2024-12-04 07:33:56

你需要使用一个“技巧”来完成这项工作。

参见: http://jsfiddle.net/2kRJv/

HTML :

<ul id="Navigation">
    <li><a href="About.html">About</a></li>
    <li><a href="Contact.html">Contact</a></li>
    <!-- ... -->
    <li class="stretch"></li>
</ul>

CSS:

#Navigation
{
    list-style-type: none;
    text-align: justify;
    height: 21px;
    background: #ccc
}

#Navigation li
{
    display: inline
}
#Navigation .stretch {
    display: inline-block;
    width: 100%;

    /* if you need IE6/7 support */
    *display: inline;
    zoom: 1
}

IE6/7 欺骗细节:内联块在 Internet Explorer 7、6 中不起作用

You need to use a "trick" to make this work.

See: http://jsfiddle.net/2kRJv/

HTML:

<ul id="Navigation">
    <li><a href="About.html">About</a></li>
    <li><a href="Contact.html">Contact</a></li>
    <!-- ... -->
    <li class="stretch"></li>
</ul>

CSS:

#Navigation
{
    list-style-type: none;
    text-align: justify;
    height: 21px;
    background: #ccc
}

#Navigation li
{
    display: inline
}
#Navigation .stretch {
    display: inline-block;
    width: 100%;

    /* if you need IE6/7 support */
    *display: inline;
    zoom: 1
}

Details on IE6/7 trickery: Inline block doesn't work in internet explorer 7, 6

独守阴晴ぅ圆缺 2024-12-04 07:33:56

这也可以通过使用 ul 元素上的伪元素来实现:

ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
    text-align: justify;
}

ul:after {
    content: "";
    width: 100%;
    display: inline-block;
}

li {
    display: inline;
}

This can also be achieved using a pseudo element on the ul element:

ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
    text-align: justify;
}

ul:after {
    content: "";
    width: 100%;
    display: inline-block;
}

li {
    display: inline;
}
紫﹏色ふ单纯 2024-12-04 07:33:56

只要这样做:

ul { width:100%; }
ul li {
  display:table-cell;
  width:1%;
}

Just do:

ul { width:100%; }
ul li {
  display:table-cell;
  width:1%;
}
终遇你 2024-12-04 07:33:56

这可能适合您的需求:

#Navigation{
}
#Navigation li{
    list-style-type: none;
    text-align: center;
    float: left;
    width: 50%; /*if 2 <li> elements, 25% if 4...*/
}

演示http://jsfiddle.net/KmqzQ/

This might suit your needs:

#Navigation{
}
#Navigation li{
    list-style-type: none;
    text-align: center;
    float: left;
    width: 50%; /*if 2 <li> elements, 25% if 4...*/
}

demo : http://jsfiddle.net/KmqzQ/

地狱即天堂 2024-12-04 07:33:56

HTML

<ul id="Navigation">
    <li><a href="#">The Missing Link</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Riluri</a></li>
    <li><a href="#">Contact us</a></li>
    <!-- ... -->
    <li class="stretch"></li>
</ul>

CSS

#Navigation {
    list-style-type: none;
    text-align: justify;
    height: 21px;
    background: #ccc
}

#Navigation li{
    display: inline
}

#Navigation li a {
    text-align: left;
    display:inline-block;
}

#Navigation .stretch {
    display: inline-block;
    width: 100%;

    /* if you need IE6/7 support */
    *display: inline;
    zoom: 1
}

查看演示:http://jsfiddle.net/2kRJv/392/

HTML

<ul id="Navigation">
    <li><a href="#">The Missing Link</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Riluri</a></li>
    <li><a href="#">Contact us</a></li>
    <!-- ... -->
    <li class="stretch"></li>
</ul>

CSS

#Navigation {
    list-style-type: none;
    text-align: justify;
    height: 21px;
    background: #ccc
}

#Navigation li{
    display: inline
}

#Navigation li a {
    text-align: left;
    display:inline-block;
}

#Navigation .stretch {
    display: inline-block;
    width: 100%;

    /* if you need IE6/7 support */
    *display: inline;
    zoom: 1
}

View demo: http://jsfiddle.net/2kRJv/392/

山有枢 2024-12-04 07:33:56

您需要有

  • ;元素分开,否则对齐将不起作用。
  • For example, this:
    
    <ul><li>test</li><li>test</li></ul>
    
    
    needs to be like this:
    <ul>
    <li>test</li>
    <li>test</li>
    </ul>
    

    或者至少在打开和关闭之间具有空间

  • 。标签。
  • You need to have the <li> elements separated, otherwise the justify won't work.

    For example, this:
    
    <ul><li>test</li><li>test</li></ul>
    
    
    needs to be like this:
    <ul>
    <li>test</li>
    <li>test</li>
    </ul>
    

    or at least have spaces between the opening and closing <li> tags.

    永不分离 2024-12-04 07:33:56

    此博客具有令人满意的稳健性解决方案。不过,它需要一些细微的更改才能适应 ul/li 导航:

    #Navigation {
        width: 100%;
        padding: 0;
        text-align: justify;
        font-size: 0;
        font-size: 12px\9; /* IE 6-9 */
    }
    #Navigation>li {
        font-size: 12px;
        text-align: center;
        display: inline-block;
        zoom: 1;
        *display: inline; /* IE only */
    }
    #Navigation:after {
        content:"";
        width: 100%;
        display: inline-block;
        zoom: 1;
        *display: inline;
    }
    

    http:// jsfiddle.net/mblase75/9vNBs/

    This blog has a satisfyingly robust solution. It needs some slight changes to accommodate a ul/li navigation, though:

    #Navigation {
        width: 100%;
        padding: 0;
        text-align: justify;
        font-size: 0;
        font-size: 12px\9; /* IE 6-9 */
    }
    #Navigation>li {
        font-size: 12px;
        text-align: center;
        display: inline-block;
        zoom: 1;
        *display: inline; /* IE only */
    }
    #Navigation:after {
        content:"";
        width: 100%;
        display: inline-block;
        zoom: 1;
        *display: inline;
    }
    

    http://jsfiddle.net/mblase75/9vNBs/

    暗藏城府 2024-12-04 07:33:56

    如果标记的答案中有空格,则该答案不起作用。

    这里的最佳答案适用于空格
    我如何*真正*证明 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?

    怂人 2024-12-04 07:33:56

    使用 CSS Flexbox

    #Navigation {
      width: 100%;
      padding: 0;
      margin: .5rem 0;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;
      list-style-type: none;
      color: #ffffff;
    }
    

    Using CSS Flexboxes

    #Navigation {
      width: 100%;
      padding: 0;
      margin: .5rem 0;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;
      list-style-type: none;
      color: #ffffff;
    }
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文