Skip to content Skip to sidebar Skip to footer

How To Display Inline Several
  • With 100% Width?
  • I have the following html:
    • element 1
    • element 2
    applied with a

    Solution 1:

    Like the others I'm struggling to understand what you're looking for exactly but does this do what you want?

    <!DOCTYPE HTMLPUBLIC"-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"><html><head><title>Horizontal 100% LIs</title><styletype="text/css">#container { width:100%; overflow:auto;}
    #containerul { padding:0; margin:0; white-space:nowrap; }
    #containerli { width: 100%; list-style-type:none; display:inline-block; }
    * html#containerli { display:inline; }  /* IE6 hack */
    * html#container { padding-bottom:17px;}  /* IE6 hack  */
    *:first-child+html#containerli { display:inline; } /* IE7 hack */
    *:first-child+html#container { padding-bottom:17px; overflow-y:hidden; }  /* IE7 hack  */</style></head><body><divid="container"><ul><listyle="background-color:red">element 1</li><listyle="background-color:green">element 2</li><listyle="background-color:blue">element 3</li></ul></div></body></html>

    Getting the LIs on to one line has two parts. The white-space:nowrap on the ul stops any automatic wrapping and the display:inline-block on the LIs allows them to run one after another, but have widths, paddings and margins set on them. For standards compliant browsers that's sufficient.

    However IE6 and IE7 need special treatment. They don't support the display:inline-block properly, but fortunately display:inline on elements with hasLayout set gives a behaviour very like display:inline-block. The width:100% has already forced hasLayout to be set on the LIs so all we have to do is to direct the display:inline to IE6 and IE7 only. There are a number of ways of doing this (conditional comments are popular on StackOverflow) but here I've chosen the * html and *:first-child+html hacks to do the job.

    Additionally, IE6 and IE7 have another bug where the scroll bar overlays the content, so the container is given a padding-bottom to make space for the scroll bar. The scroll bar is a platform control, so its height cannot be known exactly, but 17 pixels seems to work for most cases.

    Finally, IE7 also wants to put in a vertical scroll bar, so the overflow-y:hidden directed at IE7 stops this from happening.

    (The padding:0, margin:0, list-style:none, and the styles on the individual LIs are just there to show more clearly what's happening.)

    Solution 2:

    You want it to act like a good old fashioned table:

    <ulclass="menu"><li>one</li><li>two</li><li>three</li></ul>
    
    
    .menu {
        display: table;
        width: 100%;
    }
    .menu li {
        display: table-cell;
        padding: 2px;
    
        background: #900990;
        border: 1px solid yellow;
        color: white;
    }
    

    then you can also collapse it nicely when the page is small:

    /* responsive smaller screen turn into a vertical stacked menu */@media (max-width: 480px) {
    
       .menu, .menuli {
           display: normal;
        }
    
    }
    

    Solution 3:

    What you're trying to do resembles what table cells do and is impossible otherwise, without using JavaScript (I don't suggest using tables or JavaScript for this, however). Your best bet is to set a certain amount of horizontal padding on the <li> tags and float them so that they are auto-width based on their content's width instead of the window's width:

    <divid="container"><ul><li>element 1</li><li>element 2</li></ul></div><styletype="text/css">#container, #containerul {
        width: 100%;
        float: left;
    }
    
    #containerulli {
        margin: 010px00;
        padding: 5px10px;
        float: left;
    }
    </style>

    The other method is to use display: inline-block; to achieve what you want, but it's kind of a hack (not cross-browser compatible).

    Solution 4:

    using jquery to space your li's after your page has

    var ul = $('#yourListId');
      var numChildren = ul.children().length;
      if(numChildren <= 0){
        return;
      }
      ul.css({'list-style':'none','margin':0,'padding':0});
      var parentWidth = ul.width();
      var childWidth = parentWidth/numChildren;
      ul.children().each(function(index, value){
        $(this).css({'width':childWidth,'margin':0,'float':'left','display':'inline-block'});
      });
      ul.after('<div style="clear:both">');
    

    With the exception of the "childWidth" css... you can, of course, replace the other css with something from a style sheet.

    Solution 5:

    Generally to show li's horizontally you would float them all left. The part that confuses me is the 100% width. How can each li have 100% width when they can only be as wide as their container? It seems like the li's need to be fixed width or autosized with no width at all.

    <!DOCTYPE HTMLPUBLIC"-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=iso-8859-1"><title>Untitled Document</title><styletype="text/css">#container { width:100%; overflow:auto; }
            #containerul { width: 100%; }
            #containerli { float:left; }
        </style></head><body><divid="container"><ul><li>element 1</li><li>element 2</li></ul></div></body></html>

    Post a Comment for "How To Display Inline Several
  • With 100% Width?"