
css 兄弟元素宽度跟随最长元素等宽
在想要实现多个元素宽度跟随最长元素等宽时,可以使用 width: fit-content 属性。
在给兄弟元素的父元素设置 width: fit-content 后,兄弟元素的宽度将自动调整为其自身内容的宽度。但是,此时滚动条会出现在父元素上。
为了解决这个问题,可以在父元素外再套一层 div,并给该 div 设置 overflow-x: auto 属性。这样,滚动条就会出现在最外层的 div 上,而兄弟元素将跟随最长元素撑满父元素的宽度。
以下代码演示了这种实现方式:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.wrap {
width: 100%;
overflow-x: auto;
}
.container{
width: fit-content;
}
.item1{
background: red;
}
.item2{
background: gray;
}
.item3{
min-width: 1300px;
background: greenyellow;
}
</style>
<body>
<div class="wrap">
<div class="container">
<div class="item1">item1</div>
<div class="item2">item2</div>
<div class="item3">item3</div>
</div>
</div>
</body>
</html>以上就是如何实现多个兄弟元素宽度跟随最长元素等宽,同时避免滚动条出现在父元素上?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号