html - 防止宽表拉伸(stretch) flexbox 元素和容器,改为使该表可滚动

标签 html css html-table flexbox overflow

我的顶层布局是 n 列,所有列的宽度都是固定的(侧边栏),除了应该自动填满剩余空间的中央列(主栏)。

所以,我在主栏中有这个棘手的宽表。它有一个带有 overflow-x: auto 的包装器,但是它不是在包装器上触发滚动,而是更喜欢将所有内容 拉伸(stretch)到顶级 flex 容器。这可以通过向主栏添加 width: calc(100% - {sum of widths of other columns}px) 来解决,但我正在寻找一种更灵活的解决方案,允许添加更多列并在不触及此 calc 规则的情况下调整现有的大小。

有什么想法吗?有什么方法可以对 flex 元素说:填满剩余空间,但不要让你的内容拉伸(stretch)你

UPD:设法通过使用 table-layout: fixed(代码是 here)将主栏的内容包装在一个表格中来做到这一点,但我对此感到难过。有谁知道更灵活的解决方案?或者这个可以吗?

// this JS generates placeholder text, ignore it
for (const el of document.querySelectorAll(".lorem")) {
    el.innerHTML = Array(Number(el.getAttribute("p")) || 1)
        .fill()
        .map(() => `<p>${chance.paragraph()}</p>`)
        .join("");
}
body {
    margin: 0;
}

.outer {
    display: flex;
}

.sidebar {
    flex: 0 0 300px;
    background: #eef;
}

.mainbar {
    background: #ffe;
    /* width: calc(100% - 500px); */
}

.rightbar {
    flex: 0 0 200px;
    background: #fef;
}

.table-wrapper {
    width: 100%;
    overflow-x: auto;
    background: pink;
}

.table-wrapper td {
    min-width: 400px;
}
<script src="https://unpkg.com/chance@1.0.16/chance.js"></script>
<div class="outer">
    <div class="sidebar">
        <div class="lorem" p="4"></div>
    </div>
    <div class="mainbar">
        <div class="table-wrapper">
            <table>
                <tr>
                    <td class="lorem"></td>
                    <td class="lorem"></td>
                    <td class="lorem"></td>
                    <td class="lorem"></td>
                </tr>
            </table>
        </div>
        <div class="lorem" p="10"></div>
    </div>
    <div class="rightbar">
        <div class="lorem" p="3"></div>
    </div>
</div>

最佳答案

如果我理解正确,请添加 flex: 1; min-width: 0; 到您的 .mainbar 规则,它应该正常运行。

flex: 1 将占用可用空间,min-width: 0 将允许 flex 元素小于其内容,您可以阅读更多关于这里:

堆栈片段

// this JS generates placeholder text, ignore it
for (const el of document.querySelectorAll(".lorem")) {
    el.innerHTML = Array(Number(el.getAttribute("p")) || 1)
        .fill()
        .map(() => `<p>${chance.paragraph()}</p>`)
        .join("");
}
body {
    margin: 0;
}

.outer {
    display: flex;
}

.sidebar {
    flex: 0 0 300px;
    background: #eef;
}

.mainbar {
    background: #ffe;
    flex: 1;
    min-width: 0;
    /* width: calc(100% - 500px); */
}

.rightbar {
    flex: 0 0 200px;
    background: #fef;
}

.table-wrapper {
    width: 100%;
    overflow-x: auto;
    background: pink;
}

.table-wrapper td {
    min-width: 400px;
}
<script src="https://unpkg.com/chance@1.0.16/chance.js"></script>
<div class="outer">
    <div class="sidebar">
        <div class="lorem" p="4"></div>
    </div>
    <div class="mainbar">
        <div class="table-wrapper">
            <table>
                <tr>
                    <td class="lorem"></td>
                    <td class="lorem"></td>
                    <td class="lorem"></td>
                    <td class="lorem"></td>
                </tr>
            </table>
        </div>
        <div class="lorem" p="10"></div>
    </div>
    <div class="rightbar">
        <div class="lorem" p="3"></div>
    </div>
</div>

关于html - 防止宽表拉伸(stretch) flexbox 元素和容器,改为使该表可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51338182/

相关文章:

jquery - 可点击链接中的整行未在新选项卡中打开

javascript - 如何制作一个简单的 jQuery 按钮来切换元素的背景

php - 来自 PHP 脚本的随机内容在重新加载时不刷新

html - 正文元素不在页面顶部

html - 响应式设计在 Firefox 中失效

css - 如何修复表单,输入默认溢出行为

Javascript、动态 css 和转换

jquery - HTML & CSS3 向下滑动

html - 水平和垂直居中表格单元格的内容?

javascript - 可滚动的主体问题