html - 对所有可调整大小的子元素强制使用 flex 容器的最大宽度
问题描述
给定一个具有 2 列的固定大小的flex 容器:
- 一个左侧基于内容的动态宽度列;
- 一个可调整大小(双向)的普通文本区域。
textarea
当用户调整大小(在两个方向上)时,我想控制宽度。
如何将容器的max-width
大小强制为textarea
(没有 javascript 和/或禁用水平调整大小),就好像它textarea
是容器的直接子级一样?
我发现的唯一可行的纯 CSS 解决方案display: contents
在.right-content
列上,不幸的是,并非所有浏览器都支持。
.container {
max-width: 100px;
background-color: red;
display: flex;
}
.left-content {
background-color: yellow;
}
.right-content {
display: contents;
}
<div class="container">
<div class="left-content">foo</div>
<div class="right-content">
<textarea>bar</textarea>
</div>
</div>
解决方案
只需执行以下操作:
.container {
max-width: 100px;
background-color: red;
display: flex;
}
.left-content {
background-color: yellow;
}
.right-content {
min-width:0; /* this will prevent the element from growing more than the flex container
related: https://stackoverflow.com/q/36247140/8620333*/
}
textarea {
max-width:100%; /* this will prevent the text area from growing more than its parent*/
display:block; /* this will remove the whitespace from the bottom*/
box-sizing:border-box; /* don't forget this*/
opacity:0.8; /* to illustrate */
}
<div class="container">
<div class="left-content">foo</div>
<div class="right-content">
<textarea>bar</textarea>
</div>
</div>
推荐阅读
- python - 我无法在网站的源代码中发现某些元素
- python - Python 正则表达式,包括逗号和点
- python - 如何使用 QR/Aruco 代码(Python CV)将点云从相机框架转换为世界框架?
- python - postgresql 集成后 Django 中的关系不存在错误
- angular - 我怎样才能使 [attr.src] 在角度 9 中工作?
- ruby-on-rails - 94632 上的全局消息超时,正在重新启动进程,95535 被“TERM”信号成功终止
- java - 无法调用自动装配类的函数
- javascript - 如何在网页中显示js查询的结果
- c# - 将运行时升级到 net5.0 后的 SSL/TLS 问题
- flutter - 如何使用 Flutter 创建 YouTube 模态底页?