html - s 与容器内的相对宽度- 如何溢出?
- 如何溢出?
问题描述
情况
<div>
我在一个容器中有一组元素<div>
。- 内部的宽度
<div>
设置为百分比的相对大小。 - 有时内部 div 的宽度之和超过 100%。
怎么样
当前发生的情况是,内部 div 以适合容器的方式被压扁。
<div id="container" style="float:left; display:flex; width: 500px">
<div style="background-color: black; width: 90%">test</div>
<div style="background-color: red; width: 90%">test</div>
</div>
https://jsfiddle.net/pgvhfb9e/2/
如您所见,红色的与黑色的尺寸完全相同。
我想要的是
我需要的是最后一个 div 在容器的末端被简单地切断。因此,红色的应该只有 10%,而黑色的应该是 90%。
解决方案
flex-shrink: 0
在子元素上overflow: hidden
在父母身上
这可以防止子元素被缩小以适应父元素,并防止您的“10% 可见”子元素在父元素的范围之外可见。
<div id="container" style="float:left; display:flex; width: 500px; overflow: hidden;">
<div style="background-color: black; width: 90%; flex-shrink: 0;">test</div>
<div style="background-color: red; width: 90%; flex-shrink: 0;">test</div>
</div>
这是一个使用flex
速记属性同时设置flex-grow
、flex-shrink
和flex-basis
属性的变体。
<div id="container" style="float:left; display:flex; width: 500px; overflow: hidden;">
<div style="background-color: black; width: 90%; flex: 1 0 auto;">test</div>
<div style="background-color: red; width: 90%; flex: 1 0 auto;">test</div>
</div>
推荐阅读
- java - 如何在Java中修复对象不移出框架?
- r - 计算 2(为 2 定义一个函数,其中模型是一条线)
- c# - Autofac 使用接口自动进行程序集扫描 - .net core 3.0 - 根据 Lifetime 注册
- jquery - 鼠标悬停后无法删除悬停
- regex - Python 正则表达式多行
- javascript - JavaScript EventListener 未将事件变量传递给事件处理程序
- go - 在 Cloud Build 期间将存储库名称传递给 Slack 机器人
- python - QT QOpenGLWidget 大小错误
- python - 特定年份的熊猫数据框总和
- google-bigquery - Google Bigquery 中的事务管理