不知道宽度和数量时如何使用 CSS 均匀分布菜单项? [英] How to evenly distribute menu items with CSS when width and quantity is not known?

查看:19
本文介绍了不知道宽度和数量时如何使用 CSS 均匀分布菜单项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于使用 css 均匀分布元素的内容,但我发现的每个解决方案都要求您了解并定义要分布的元素的宽度和/或数量.

I've read a lot on evenly distributing elements using css but every solution i've found requires you to know and define the width and/or the number of elements being distributed.

我有一个固定宽度的容器 - 这里可以是任意数量的自动宽度的 li 元素,这些元素需要从左到右均匀分布在父元素中.

I have a container that is of a fixed width - within here could be any number of li elements of an automatic width which need to be distributed evenly across the parent element from left to right.

这是我的标记 - 除了可以有任意数量的标签,其中包含任意长度的文本.

Here is my markup - except there could be any number of tabs with any length text in them.

<ul class="tabs">
     <li class="tab active" id="tab-1"><span class="tab-title">Tab 1</span></li>
     <li class="tab " id="tab-2"><span class="tab-title">Tab 2</span></li>
     <li class="tab " id="tab-3"><span class="tab-title">Tab 3</span></li>
     <li class="tab " id="tab-4"><span class="tab-title">Tab 4</span></li>
</ul>

所以当元素的数量和宽度是动态的时,使用 css 进行均匀分布 - 可以做到吗?

So Even Distribution with css when the number of elements and width is dynamic - can it be done?

这似乎是一件显而易见的事情——该死的 CSS!

Seems like such an obvious thing to want to do - Damn CSS!

推荐答案

为此,您可以使用 CSS display:table-cell 属性.写这个:

For this, you can use the CSS display:table-cell property. Write this:

.tabs {
  display: table;
  width: 300px;
  border: 1px solid green;
}

.tabs li {
  display: table-cell;
  border: 1px solid red;
  height: 40px;
}

检查这个JSFiddle.

这篇关于不知道宽度和数量时如何使用 CSS 均匀分布菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆