简体   繁体   English

导航栏填充方法-表格/ CSS / JS

[英]Method for padding nav bar - Tables / CSS / JS

I'm developing a navigation bar for a site that needs ie7>= support. 我正在为需要ie7> =支持的网站开发导航栏。 The nav bar can contain any amount of links, and each link can contain different amounts of text, and so can be different sizes. 导航栏可以包含任意数量的链接,每个链接可以包含不同数量的文本,因此可以具有不同的大小。 However I'd like the nav bar to automatically space each link out so that they fill the parent container. 但是,我希望导航栏自动将每个链接隔开,以便它们填充父容器。

Here's a solution using pure css to set the padding on each nav bar: https://jsfiddle.net/n07x8963/4/ 这是使用纯CSS在每个导航栏上设置填充的解决方案: https : //jsfiddle.net/n07x8963/4/

#nav_1 li { padding:0px 36px;}

The trouble is this requires padding to be manually specified for each nav bar depending on how many links there are and the width of the text within the links. 麻烦在于,这需要根据每个导航栏手动指定填充,具体取决于链接数和链接内文本的宽度。 This has to be done every time the content of the nav bar changes. 每当导航栏的内容更改时,都必须执行此操作。

A way of doing this automatically is of course to use tables: https://jsfiddle.net/t0t29yne/1/ 自动执行此操作的一种方法当然是使用表: https : //jsfiddle.net/t0t29yne/1/

Or by JavaScript as seen here: https://jsfiddle.net/jtz4thfo/1/ 或通过JavaScript(如此处所示): https//jsfiddle.net/jtz4thfo/1/

Each of the solutions have advantages / disadvantages, so I'd like to know if anyone has a better solution, or an argument towards using any of the mentioned methods. 每个解决方案都有优点/缺点,所以我想知道是否有人有更好的解决方案,或者是否有使用任何上述方法的争论。

Thanks 谢谢

UPDATE: I've ended up opting for tables, ie7+ browsers get served UL's, LI's with display:table-row, display:table-cell overridden in the CSS. 更新:我最终选择了表,ie7 +浏览器在CSS中被display:table-row,display:table-cell覆盖了UL,LI。

Where as ie7 get's served actual tables. 凡为ie7送达的实际表。

I know using Tables for layout is generally seen as wrong, but in this case there is no adequate pure CSS solution so I think it's the best / easiest solution. 我知道使用Tables进行布局通常被认为是错误的,但是在这种情况下,没有足够的纯CSS解决方案,因此我认为这是最好/最简单的解决方案。

In IE7 i suggest using tables for only via CSS will not be possible. 在IE7中,我建议仅通过CSS使用表是不可能的。

 .nav { width: 600px; height: 20px; background-color: #FF9; } ul { list-style-type: none; padding: 0; margin: 0; display: table; /*IE8+ and non-IE*/ width: 100%; } ul li { background-color: #F9F; display: table-cell; /*IE8+ and non-IE*/ text-align: center; } 
 <div id="nav_1" class="nav"> <ul> <li id="link_1">AAAA</li> <li id="link_2">BB</li> <li id="link_3">CCCCCC</li> <li id="link_4">DDDD</li> <li id="link_5" class="last">EEEEEE</li> </ul> </div> <div id="nav_2" class="nav"> <ul> <li id="link_6">FFFFFF</li> <li id="link_7">GGGG</li> <li id="link_8">HH</li> <li id="link_9">IIIIIII</li> <li id="link_10" class="last">JJJJ</li> </ul> </div> 

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

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