Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

html - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...