Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend last li to remaining width of a ul?

I have been puzzled about this for a while now. Here is what i have now:

-------------------------------------------------------------------------------
|                   |                  |                     |
|       Item 1      |      Item 2      |      Last item      |
|                   |                  |                     |
-------------------------------------------------------------------------------

There, the last li is only taking up part of the ul. I need it to look like this:

-------------------------------------------------------------------------------
|                   |                  |                                      |
|       Item 1      |      Item 2      |              Last item               |
|                   |                  |                                      |
-------------------------------------------------------------------------------

I can not use Javascript (or Jquery). I do not want to set the width of each li, since the text size could vary, and i do not know how many li's there will be. I might have 5 or 3.'

How would i accomplish this? Thanks.

Here is a jsfiddle with some of my code. I need the lighter color to expand the rest of the ul. JSFIDDLE

like image 825
Hunter Mitchell Avatar asked Sep 26 '13 12:09

Hunter Mitchell


People also ask

Does Li have to be inside UL?

The <li> HTML element is used to represent an item in a list. It must be contained in a parent element: an ordered list ( <ol> ), an unordered list ( <ul> ), or a menu ( <menu> ).

How do you increase the width of an UL?

You just put width property to ul element.

How do I make a DIV take up the remaining width?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

What is the difference between Li and UL?

ul stands for unordered list. li stands for list item. They are the HTML tags for "bulleted" lists as opposed to "numbered" lists (which are specified by ol for ordered list).


1 Answers

You can float all elements but the last to the left.

The last element's box model will then extend behind these floated list items (even though the content doesn't). overflow:hidden will prevent this behaviour; the box model will begin when the last floated item ends, as if the floated items were still in the natural flow of the page.

ul, li{
    margin:0;
    padding:0;
    
    list-style-type:none;
}

ul li{
    display: block;
    float:left;
}

ul li:last-child{
    background-color: #ddd;

    float:none;
    overflow:hidden;
}
<ul>
    <li>Item</li>
    <li>Item</li>
    <li>I will extend to the end of the page.. No matter how far. I will also not drop beneath my previous items, even if the text in here requires me to span over multiple lines.</li>    
</ul>

An edit of your newly added JSFiddle:

JSFiddle

like image 140
George Avatar answered Nov 04 '22 20:11

George