具有均匀宽度导航项目的流体布局
我正在为移动设备构建一个网站,因此具有流畅的布局。 我的导航列表如下所示:
<ul>
<li>home</li>
<li>about</li>
<li>work</li>
<li>contact</li>
</ul>
问题是,第一个列表项只需 100px(始终左对齐),其他 3 个列表项均匀分割,因此除了第一个列表项之外的所有列表项是否可以具有均匀宽度(不使用 JavaScript)。
I'm building a site for mobile devices and therefore has a fluid layout.
My navigation list looks like this:
<ul>
<li>home</li>
<li>about</li>
<li>work</li>
<li>contact</li>
</ul>
Problem is, the first list item needs to be 100px only (left aligned always), and the other 3 split evenly, therefore is it possible to have even width for all list items except for the first one (without using javascript).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我能想到的最简单的方法:
主要思想是将第一个
li
从流程中取出(position:absolute
),并向li 添加一个 padding-left 到 < code>ul
(第一个li
的空格)。现在,如果我们为其他 li 设置百分比宽度,它们将占用剩余空间。这是一个 jsFiddle 演示。我在
ul
上添加了红色边框,这表明由于百分比,li
无法准确填充它。我不确定您想要支持哪些移动浏览器,但除了
:first-child
(可以通过在第一个列表项上添加类来解决)我认为它们必须支持我使用的所有内容。This is the simplest way I could think of:
The main idea is taking the first
li
out of the flow (position: absolute
) and adding a padding-left to theul
(space for the firstli
). Now if we set the percentage width for the otherli
s, they will take up the remaining space.And here is a jsFiddle Demo. I added a red border on the
ul
which shows that because of the percentagesli
s will not accurately fill it.I am unsure what mobile browsers you want to support, but except
:first-child
(which can be worked around by adding a class on the first list item) I assume they must support everything I used.嗯有点笨拙 - 但这似乎有效,它确实需要嵌套列表(单独列表中的第二个 3 个链接)和“主页”链接的跨度,理论上是您需要第一个链接浮动,宽度:100px,那么您需要第二组不浮动并隐藏其溢出,以便该组占用剩余空间..然后您将 3 个链接@ 33% 浮动在非浮动容器内
示例:此处
CSS:
HTML:
hmm a bit cludgy - but this seems to work, it does require nesting the list (second 3 links in separate list) and a span for the "home" link, theory is that you need the first link to float, width: 100px, then you need the second group not to float and have their overflow hidden so the group take up the remaining space.. then you float the 3 links @ 33% inside the non-floated container
Example : HERE
CSS:
HTML:
就其价值而言,这就是我在对您的问题发表评论时所想到的:
http://jsfiddle.net /4t9fV/
For what it's worth, this was what I was thinking of when I made my comment on your question:
http://jsfiddle.net/4t9fV/