简体   繁体   English

如何在li内联列表项之间创建恒定空间

[英]How to create constant space BETWEEN li inline list items

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. 我了解我需要使用ul display: block和li的display: inline

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. 要使li元素具有恒定的宽度,有很多问题,但是我希望它们之间保持恒定的间隔它们本身包含不同宽度的文本。

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. 我正在寻找具有未知宽度li元素的固定宽度ul ,并希望它们之间具有恒定的空间,而不是恒定宽度li元素。 Is this only achievable with javascript? 这是只能通过javascript实现吗?

Use the margin css property. 使用margin css属性。 If they all have the same margin, then the distances between the content of the adjacent <li>s will be the same. 如果它们都具有相同的边距,则相邻<li>s内容之间的距离将相同。

You might want to take a look at the Box Model 您可能想看看Box模型

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. 如果要使项目的位置与内容无关而保持不变(因此它们之间的间隙会改变),则需要设置项目的宽度。

I'd like to offer my solution to this: http://jsfiddle.net/connectedcat/bZpaG/1/ 我想为此提供解决方案: http : //jsfiddle.net/connectedcat/bZpaG/1/

JQuery excerpt: jQuery摘录:

$(document).ready(function(){
    var nav_width = 0;
    $('nav li').each(function(){
        nav_width += $(this).width();
    });

    var for_each = ($('nav ul').width() - nav_width)/($('nav li').length - 1);
    $('nav li').each(function(){
        $('nav li').css('margin-right', for_each);
    });

    $('nav li:last-child').css('margin-right', 0);
});

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. 据我所知,没有办法仅在CSS中做到这一点,需要js来找出真正流畅的布局的距离。 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. 一个困扰我的难题仍然是,不同的浏览器呈现字体略有不同(例如,在Safari中呈现的某个单词可能占用48px,而在Firefox中为49px。)我输入了一些CSS来说明但这会导致一些杂散的像素。 If anybody knows how to make it super neat and clean across all platforms - I would appreciate some schooling:) 如果有人知道如何使其在所有平台上都超级干净整洁,我将不胜感激:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM