全宽水平导航栏,其中项目间隔均匀
起点:
终点:
我试图拥有一个填充 100% 容器的水平导航栏。在第一个示例中,您将看到所有项目都左对齐。我试图让它填充容器的整个宽度,如第二个示例所示。我希望所有项目之间的间距保持一致(与显示的方式不同,我只是快速将其放在一起,以便让您了解我正在尝试做什么)。我需要文本不是图像,并且它所在的容器是流动的而不是固定的。
Starting point:
End point:
I'm trying to have a horizontal navigation bar that fills 100% of it's container. In the first example, you'll see all of the items aligned to the left. I'm trying to get it to fill the full width of the container as shown in the second example. I want the spacing between all of the items to be uniform (unlike the way it's shown, I just put that together quickly to give you an idea of what I'm trying to do). I need the text to not be an image and the container it goes in is fluid not fixed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于静态数量的元素,这很容易 - http://jsfiddle.net/X56cJ/
但是,如果你想在元素之间有统一的间距文本,而不是元素本身 - 这变得很棘手。同样,如果元素数量不变,则可以使用 css 类和静态宽度来实现。否则它必须是 javascript
编辑:这是在元素之间获得相同空间的 JavaScript 方法。
HTML:
JS:
完整示例此处
With a static number of elements it's easy - http://jsfiddle.net/X56cJ/
However, if you want to have uniform spacing between the text, not the elements themselves - it becomes tricky. Again, if the number of elements doesn't change, you do it with css classes and static widths. Otherwise it'll have to be javascript
EDIT: Here's the JavaScript way of getting same space between elements.
HTML:
JS:
Full example here
在你的父级上使用 display: table ,在你的子级上使用 display: table-cell 就可以了。 <= IE7 不支持此功能。
http://codepen.io/simply-simpy/pen/jzski
Using display: table on your parent, and display: table-cell on your children will do the trick. This is not supported in <= IE7.
http://codepen.io/simply-simpy/pen/jzski
如果您知道将拥有多少个元素,则可以将每个元素的宽度指定为百分比。否则,如果不借助表格或 javascript,这是不可能的。
If you know how many elements you'll have, you can specify a width of each element as a percent. Otherwise it won't be possible without resorting to tables or javascript.