红色跟灰色的元素宽度需要跟随绿色元素的宽度撑满一致,如下:
现有代码:
<!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>
.container{
width: 100%;
overflow-x: auto;
}
.item1{
background: red;
}
.item2{
background: gray;
}
.item3{
min-width: 1300px;
background: greenyellow;
}
</style>
<body>
<div class="container">
<div class="item1">item1</div>
<div class="item2">item2</div>
<div class="item3">item3</div>
</div>
</body>
</html>
请教各路大神最简便方法
其实很简单啦,给
.container
元素设置width: fit-content
就好了。但是滚动条就会出现在body
元素上。所以可以在
.container
外部再套一层div
就好了。以下是代码结构