如何在 li 内联列表项之间创建恒定空间
我想使用水平链接列表。我知道我需要对 ul
使用 display: block
,对 li 使用 display: inline
。
要获得 li
元素的恒定宽度有很多问题,但我想要它们之间的恒定空间,它们本身包含不同宽度的文本。
谢谢。
我认为我的问题不清楚:
我希望有一个固定宽度的 ul
和未知宽度的 li
元素,并希望它们之间有恒定的空间,而不是恒定宽度的 li
元素。这只能用javascript实现吗?
I want to use a list for horizontal links. I understand I need to use display: block
for the ul
and display: inline
for the li.
There are a number of questions for getting constant width for the li
elements, but I want constant space between them, they contain text of different width themselves.
Thanks.
I think my question was unclear:
I am looking to have a fixed width ul
with unknown width li
elements and want to have constant space between them, not constant width li
elements. Is this only achievable with javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 margin css 属性。如果它们都具有相同的边距,则相邻
s
的内容之间的距离将相同。您可能想看看 盒子模型
编辑地址评论如下:
如果您希望无论内容如何,项目之间的间隙都保持不变,则边距应该可以做到这一点。如果您希望项目的位置保持不变,而不管内容如何(因此它们之间的间隙将会改变),则需要设置项目的宽度。
Use the margin css property. If they all have the same margin, then the distances between the content of the adjacent
<li>s
will be the same.You might want to take a look at the Box Model
Edit to address comment below:
If you want the gaps between the items to stay the same regardless of the content, margin should do the job. If you want the position of the items to stay the same regardless of the content (and so the gaps between them will change), you need to set the width of the item.
我想提供我的解决方案:
http://jsfiddle.net/connectedcat/bZpaG/1/
JQuery 摘录:
据我发现没有办法纯粹用 css 来做到这一点,需要 js 来计算出真正流畅布局的距离。
与此问题相关的一个难题仍然困扰着我,那就是不同的浏览器呈现的字体略有不同(例如在 Safari 中呈现的某个单词可能占用 48 像素,而在 Firefox 中则为 49 像素。)我放入了一些 css 来解释但这会导致一些杂散像素。如果有人知道如何使其在所有平台上变得超级整洁和干净 - 我将不胜感激一些教育:)
I'd like to offer my solution to this:
http://jsfiddle.net/connectedcat/bZpaG/1/
JQuery excerpt:
As far as I have figured out there is no way to do it purely in css, js is required to figure out the distances for the truly fluid layout.
One conundrum that still bugs me in relation to this issue is the fact that different browsers render fonts slightly differently (as in a certain word rendered in Safari may take up 48px, while in Firefox - 49px.) I put in some css to account for this, but it results in some stray pixels. If anybody knows how to make it super neat and clean across all platforms - I would appreciate some schooling:)