css - flex列内的水平滚动div
问题描述
我似乎在 flex 列中没有水平滚动的 div。
.container { display: flex; margin: 20px; height: 300px; border: 1px solid blue; }
.side-nav { flex-shrink: 0; width: 100px; min-height: 100%; background: grey; }
.main { padding: 20px; }
.scrollable { overflow-x: auto; max-width: 100%; }
.long-content { width: 2000px; height: 50px; background: red; }
<div class='container'>
<div class='side-nav'>
</div>
<div class='main'>
<h1>Test</h1>
<div class='scrollable'>
<div class='long-content'>
This is supposed to scroll horizontally unless your window is super wide
</div>
</div>
<p>Some paragraph below the scrollable box</p>
</div>
</div>
如果我将max-width
of更改.scrollable
为px
它会起作用,但我需要它来填充该列。
我错过了什么?
解决方案
在我看来,这是一个模块“错误”(Wierd)。无论如何,一个非常简单的解决方案是使用flex-basis
(而不是width
)。
步骤 1main
添加width: 0
;
.container {
display: flex;
margin: 20px;
height: 300px;
border: 1px solid blue;
}
.side-nav { flex-shrink: 0; width: 100px; min-height: 100%; background: grey; }
.main {
padding: 20px;
border: 5px dashed orange;
/* "new code" */
width: 0px;
}
.scrollable { overflow-x: auto; max-width: 100%; }
.long-content { width: 2000px; height: 50px; background: red; }
<div class='container'>
<aside class='side-nav'>
Aside
</aside>
<main class='main'>
<h1>Test</h1>
<div class='scrollable'>
<div class='long-content'>
This is supposed to scroll horizontally unless your window is super wide
</div>
</div>
<p>Some paragraph below the scrollable box</p>
</main>
</div>
第 2 步 -main
添加flex-basis: 100%
;
.container {
display: flex;
margin: 20px;
height: 300px;
border: 1px solid blue;
}
.side-nav { flex-shrink: 0; width: 100px; min-height: 100%; background: grey; }
.main {
padding: 20px;
border: 5px dashed orange;
/* "new code" */
width: 0px;
flex-basis: 100%;
}
.scrollable { overflow-x: auto; max-width: 100%; }
.long-content { width: 2000px; height: 50px; background: red; }
<div class='container'>
<aside class='side-nav'>
Aside
</aside>
<main class='main'>
<h1>Test</h1>
<div class='scrollable'>
<div class='long-content'>
This is supposed to scroll horizontally unless your window is super wide
</div>
</div>
<p>Some paragraph below the scrollable box</p>
</main>
</div>
另一种选择是使用width: 0;
&flex-grow:1
.container {
display: flex;
margin: 20px;
height: 300px;
border: 1px solid blue;
}
.side-nav { flex-shrink: 0; width: 100px; min-height: 100%; background: grey; }
.main {
padding: 20px;
border: 5px dashed orange;
/* "new code" */
width: 0px;
flex-grow: 1;
}
.scrollable { overflow-x: auto; max-width: 100%; }
.long-content { width: 2000px; height: 50px; background: red; }
<div class='container'>
<aside class='side-nav'>
Aside
</aside>
<main class='main'>
<h1>Test</h1>
<div class='scrollable'>
<div class='long-content'>
This is supposed to scroll horizontally unless your window is super wide
</div>
</div>
<p>Some paragraph below the scrollable box</p>
</main>
</div>
我不喜欢这些想法中的任何一个——但这就是生活……
推荐阅读
- batch-file - 如何从循环内部设置全局变量
- xamarin.forms - Xamarin Forms 从 GridLength Star 或 Auto 获取绝对值
- firebase - 在 Firestore 中查询具有类型引用的文档
- ionic-framework - _co.gotoArchivePage 不是函数
- c# - UWP IoT 如何添加自定义字体
- c# - 在 Linux 上使用单声道的 C# 代码的 CIL 级调试器
- selenium - selenium 网格 Safari 替代方案(Docker 节点)?
- java - 我在编写阶乘程序时遇到困难
- html - 如何删除 h1 上方的空格?
- python - 执行在 python 中创建的应用程序时出现致命的 Python 错误