css - 根据固定宽度列的数量动态拉伸(stretch)内容

标签 css

我对 CSS 的使用已经足够多了,如果我有一定数量的列,我可以让列与内容一起工作:我只是在内容之前在 HTML 中定义侧边栏,并指定它们 float 到左边或右边,同时给内容一个 margin-left 或 -right,这样它就不会流入侧边栏末端下方的空间。

不过,我遇到了一个新挑战,即列数可能因页面而异。在我的 HTML 中,我不想在主页内容之前指定边栏内容,这是我知道的唯一方法。

基本上,我希望 HTML 看起来像这样:

<div id="container">
    <div id="content">
        <!-- Main body content goes here. -->
    </div>
    <div class="sidebar">
        <!-- Sidebar DIV should appear to the right of the content DIV. -->
    </div>
    <div class="sidebar">
        <!--
            Another sidebar DIV, with this one *preferably* appearing to the
            right of the one above, since it appears lower in the code and is
            assuming LTR.  I'm open to CSS that makes this sidebar appear to
            the left instead of the right, however.
        -->
    </div>
    <!-- Any number of additional sidebar DIV's can appear here. -->
</div>

我不确定我是否应该尝试 float 内容/侧边栏 DIV,使它们绝对定位,指定宽度等,以使内容随着侧边栏的可变数量自动拉伸(stretch)。可能值得注意的是,每个侧边栏的大小将保持不变。

div.sidebar {
    width: 100px;
}

最后一点:如果我可以对 HTML 进行改进(例如:围绕父 DIV 中的所有侧边栏 DIV 元素,例如 <div id="sidebars"> ),我也愿意接受建议.

最佳答案

我想我必须接受这样一个事实,即这需要 HTML 表格,因为没有人可以为此提供 CSS 解决方案。

关于css - 根据固定宽度列的数量动态拉伸(stretch)内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1835239/

相关文章:

html - 三行模态,带有固定的标题、底行和可滚动的中间行

javascript - 如何让 2 个 YouTube 视频一个接一个地自动播放?

javascript - ext-6 gpl Hello World

css - 为什么 createshadowroot 给我的结果与 attachshadow 不同?

javascript - 让 CSS 过渡动画工作

jquery - 将 td 宽度扩展到容器的大小之外

html - SVG 剪辑路径不适用于 div

html - 另一个 IE8 损坏的 CSS 下拉菜单

iphone - iPhone 和 Mac/PC 上 html 表格的字体大小不同

html - Bootstrap/CSS 中固定宽度标签中的自动换行