html - 将最后一个 li 扩展到 ul 的剩余宽度?

标签 html css html-lists

我一直对这个问题感到困惑。这是我现在拥有的:

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

那里,最后一个li只占了ul的一部分。我需要它看起来像这样:

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

我不能使用 Javascript(或 Jquery)。 我不想设置每个里的宽度,因为文本大小可能会有所不同,而且我不知道会有多少里。我可能有 5 或 3 个。”

我将如何做到这一点?谢谢。

这是一个包含我的一些代码的 jsfiddle。我需要较浅的颜色来扩展 ul 的其余部分。 JSFIDDLE

最佳答案

您可以将除最后一个元素之外的所有元素 float 到左侧。

最后一个元素的框模型将扩展到这些 float 列表项的后面(即使内容没有)。 overflow:hidden 将阻止这种行为;框模型将在最后一个 float 元素结束时开始,就好像 float 元素仍在页面的自然流动中一样。

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>

对新添加的 JSFiddle 的编辑:

JSFiddle

关于html - 将最后一个 li 扩展到 ul 的剩余宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19027764/

相关文章:

html - 或者和 > 使用 CSS 选择器

html - 有什么方法可以用 css3 和 html5 的新实现来隐藏部分 div 吗?

html - 在 Bootstrap 中强制文本区域中的文本从最左端开始时遇到问题

html - 创建一棵树,div 还是 ul 更好?

html-lists - 语法错误 : private fields are not currently supported

html - 在多行上水平对齐 ul 中的 li

javascript - jquery 检查 img 是否在 div 中

html - 复杂的定位元素(乘以行)

javascript - 如何在 Javascript if/else 语句中使用 HTML?

html - CSS 最小化窗口大小更改时的空白而不是网格