CSS 水平菜单 - 等间距?
我有一个标准的 CSS 菜单,由 UL 和 LI 标签制成。 我需要它们水平地覆盖整个页面(不是我的真实情况,但我会以此来简化情况)。 但是,这些项目是动态创建的,因此我无法对 LI 项目或边距进行硬编码。
我见过使用 JavaScript 来设置这些值的解决方案,但我真的很想避免它们。
最后,我看到了一个非常好的解决方案,它设置
#menu {
width: 100%;
/* etc */
}
#menu ul {
display: table;
}
#menu ul li {
display: table-cell;
}
This will create thedesired behavior in most browsers... except IE.
有任何想法吗?
编辑:感谢您的回复。 但是,由于生成项目的代码不是我的,因此我无法在创建项目时设置内联样式,除非稍后使用 JavaScript。
I have a standard CSS menu, made with UL and LI tags. I need them to get to cover the whole page, horizontally (not my real case, but I'll take this to simplify the situation). However, the items are created dynamically and so I'm not able to hardcode any with to LI items, nor margins.
I've seen solutions using JavaScript to set those values but I would really love to avoid them.
Lastly, I've seen a pretty good solution which is setting
#menu {
width: 100%;
/* etc */
}
#menu ul {
display: table;
}
#menu ul li {
display: table-cell;
}
This will create the desired behavior in most browsers... except for IE.
Any ideas?
EDIT: Thanks for the responses. However, as the code that generates the items isn't mine, I'm not able to set inline styles when creating them without using JavaScript later.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您无法设置内联元素的高度或宽度。 http://www.w3.org/TR/CSS2/visudet.html #inline-width
尝试
display:inline-block;
这里是 ie 的修复:
You can't set the height or width of an inline element. http://www.w3.org/TR/CSS2/visudet.html#inline-width
Try
display:inline-block;
here is the fix for ie:
如果您想让元素获得整个可用空间,则无需先定义菜单元素的宽度(当然,这将有助于均匀调整 li 元素的大小)。 您可以通过处理
display
属性来解决此问题。请注意,
width:1%
是必需的以避免单元格折叠。If you want to let the element get the whole available space, there is no need to define a priori the width of the menu elements (of course, it will help in equally sizing the li elements). You can solve this problem by working on the
display
property.Note that
width:1%
is required to avoid cell collapsing.如果您的菜单项是动态生成的(因此您不知道之前会有多少个),那么您可以向
li
添加style="width:xx"
属性code>s (或者在顶部的...或者任何你喜欢的地方,真的)。 其中
xx
应为width_of_parent_div_in_px/number_of_elements+'px'
或100/number_of_elements+'%'
。li
也应该是block
级元素,并且是float
edleft
。If your menu items are being dynamically generated (so you don't know how many there will be prior) then you can add a
style="width:xx"
attribute to theli
s (or in<style>
at the top... or where ever you please, really). Wherexx
should either bywidth_of_parent_div_in_px/number_of_elements+'px'
, or100/number_of_elements+'%'
. Theli
s should also beblock
-level elements, andfloat
edleft
.如果您有 5 个元素,考虑到边框和填充,则宽度:18% 可能是正确的。 但它会根据你有多少元素、多少填充等而有所不同。
The width: 18% may be about right if you have 5 elements across, accounting for border and padding. But it will vary due to how many elements you have, how much padding, etc.
如果您愿意使用 Flexbox,那么这并不难。 我即将发布的代码完全归功于 CSS 技巧因为这是他们的 CSS。
以下是包含供应商前缀的示例。
Flexbox 的唯一问题是您是否需要支持 IE 9 及以下版本,否则,我认为没有理由不使用 Flexbox。 您可以在此处查看浏览器对 Flexbox 的支持。
If you are open to using Flexbox then it isn't hard to do. Full credit for the code I am about to post goes to CSS Tricks as this is their CSS.
Below is an example that includes vendor prefixes.
The only issue with Flexbox is if you need to support IE 9 and below, otherwise, I see no reason to not use Flexbox. You can view browser support for Flexbox here.
这对我有用:
Here's what worked for me: