Extend last li to remaining width of a ul?

前端 未结 2 1298
庸人自扰
庸人自扰 2020-12-19 04:41

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

-------------------------------------------------------------------------------
|            


        
相关标签:
2条回答
  • 2020-12-19 05:08

    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: #ffffd;
    
        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

    0 讨论(0)
  • 2020-12-19 05:16
    ul>li:last-of-type {width:whatever}
    

    So the two li's before the last would float:left and have 20% on both and the last one can have 60%;

    Also don't forgot to clear the flaots with

    ul {overflow:auto;}
    

    and keep the % perfect so the borders do not escape:

    ul>li {box-sizing:border-box;}
    
    0 讨论(0)
提交回复
热议问题