
如何在 css 中使兄弟元素与宽度最长的元素等宽?
遇到如下问题:
现有代码:
.container{
width: 100%;
overflow-x: auto;
}
.item1{
background: red;
}
.item2{
background: gray;
}
.item3{
min-width: 1300px;
background: greenyellow;
}要求红色和灰色元素自动撑满至绿色元素的宽度。
解决方案:
立即学习“前端免费学习笔记(深入)”;
最便捷的方法,只需给 .container 元素设置 width: fit-content,即可。
但需要注意,这会导致滚动条出现在 body 元素上。所以,可以考虑在外围再套一层 div,即:
.wrap {
width: 100%;
overflow-x: auto;
}
.container{
width: fit-content;
}










