• Geek Answers Handbook

    Horizontal alignment / alignment of list items?

    I have the following situation:

    <nav id="access" role="navigation"> <div class="menu"> <ul> <li class="page_item"><a href="#pricing" title="Pricing">Pricing</a></li> <li class="page_item"><a href="#booking" title="Booking">Booking</a></li> <li class="page_item"><a href="#contact" title="Contact">Contact</a></li> <li class="page_item"><a href="#map" title="Map">Map</a></li> </ul> </div> </nav> 

    Since the external container in which the navigator is located has a width of 800 pixels, the nav container also has a width of 800 pixels.

     #access .menu ul li { float: left; } 

    I float, all menu items are left, so align side by side. I wonder how I can create equal space between all these list items, for example:

     ____________________________________ <- this is what I have now item item item item item ____________________________________ <- this is what I want item item item item item 

    Any ideas how to solve this? Or with pure CSS or jQuery?

    +7
    jquery html css navigation
    matt Jul 05 '11 at 16:46
    source share
    8 answers

    This answer applies if you want it to work with any number of "li" s:

    if you give your personal display: block;

     li {float:left; display:block; } 

    then the following script does the job for yours:

     var first = $("li:first"); var allOther = $("li").not(":first, :last"); var last = $("li:last"); var remainingWidth = $(".menu").width() - first.width() - last.width(); allOther.width(remainingWidth / allOther.length).css("text-align", "center"); 

    look at it in jsFiddler: http://jsfiddle.net/PLQFj/14/

    +3
    Mo valipour Jul 05 '11 at 16:59
    source share

    This method is WORK 100%!

    CSS

     .menu { position:relative; text-align:justify; text-align-last:justify; border:1px solid silver; height:2.2em; min-width:800px; } .menu li { display:inline-block; background:silver; padding:.5em 1em; cursor:pointer; text-align:center; } .menu li:hover { background:black; color:white; } .menu:after { content:""; display:inline-block; width:100%; height:0; overflow:hidden; } 

    HTML:

     <ul class="menu"> <li>About</li> <li>Company</li> <li>Products</li> <li>Menu item</li> </ul> 
    +6
    Viktor Dec 05 '12 at 15:08
    source share
     .menu { text-align: justify; } .menu ul, .menu li { display: inline; } .menu ul::after { display: inline-block; width: 100%; content: ""; } 

    But for browser compatibility (IE7) you must replace ::after with an additional markup element:

     <nav id="access" role="navigation"> <div class="menu"> <ul> <li class="page_item"><a href="#pricing" title="Pricing">Pricing</a></li> <li class="page_item"><a href="#booking" title="Booking">Booking</a></li> <li class="page_item"><a href="#contact" title="Contact">Contact</a></li> <li class="page_item"><a href="#map" title="Map">Map</a></li> </ul> <span></span> </div> </nav> 

    See the demo script .

    +2
    NGLN Jul 05 '11 at 16:56
    source share

    FIDDLE: http://jsfiddle.net/QR5s3/

    * CSS (changed from floating li to embedded li) *

     #access .menu ul { text-align:center; } #access .menu ul li:first-child { padding-left:0 !important; } #access .menu ul li:last-child { padding-right:0 !important; } #access .menu ul li { display:inline; padding:0 35px; /* ADJUST PADDING */ } 
    +2
    kei Jul 05 '11 at 17:02
    source share

    What about:

     #access .menu ul li { float: left; margin-right: 10px } 
    +1
    Fiona - myaccessible.website Jul 05 '11 at 16:49
    source share

    The first method I can think of is to use a table instead of an unordered list. By default, it will look the way you want. In addition, adding more menu items would be easy as alignment is automatic.

    Here are some ways to do this.

    0
    Hubro Jul 05 '11 at 16:49
    source share

    The only sensible cross-browser approach not related to JavaScript would be <table> , I'm afraid. It is not very elegant, but it does the job. A floating-point layout will not be able to correctly display elements with a variable size. All this should work even in IE 6.

    Markup

     <table class="nav"> <tr> <td class="item">item</td> <td class="separator">&nbsp;</td> <td class="item">item</td> <td class="separator">&nbsp;</td> ... </tr> </table> 

    Separator must contain &nbsp; -entity to make sure it displays correctly in all browsers (IE creates problems here, ignoring empty cells).

    Style

     .nav, .nav tbody, .nav tr { width: 100%; } .nav .item { /* this forces the item to be as small as possible, which forces the separators to take up the remaining space */ width: 1px; } 

    You need to make sure that the contents of all elements have spaces replaced with &nbsp; to prevent text flow around due to 1px width.

    0
    jwueller Jul 05 '11 at 16:50
    source share

    CSS

     #access { border: 1px solid #DDDDDD; height: 50px; width: 800px; } .menu ul li { float: left; } .menu ul { margin: 0; padding: 0 } 

    Jquery:

     var li = $('#access ul li').length - 1; var w = 0; $('#access ul li').each(function() { w = w + $(this).width(); }); var eachPlot = ($('#access').width() - w) / li; $('#access ul li:gt(0)').each(function() { $(this).css({ 'margin-left': parseInt(eachPlot) + 'px' }); }); 
    0
    thecodeparadox Jul 05 '11 at 17:32
    source share

    More articles:

    • WPF Popup Alternative - wpf
    • How to exclude CSS formatting? - css
    • The popup does not lose focus and closes until I clicked on the control inside it. - wpf
    • Adding and Removing NSMutableDictionary KVO - collections
    • Java overrides abstract generic method - java
    • IntelliJ Scala Working Plugin Tutorial? - scala
    • Javascript onChange arrow keys - javascript
    • Listview.getCheckedItemPositions () does not return the correct result when the item is not set - android
    • Scenario Recommendations in RESTful Services - rest
    • how to succinctly create a temporary file that is a copy of another file in python - python

    All Articles

    Geek Answers | 2019