多项子元素垂直排列且折行,父容器宽度如何自适应。
方法一:display:inline-flex
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.container {
height: 500px;
display: inline-flex;
flex-wrap: wrap;
writing-mode: vertical-lr;
background: lightcoral;
}
.container div {
width: 200px;
height: calc(100% / 5);
writing-mode: horizontal-tb;
background: lightblue;
}
</style>
</head>
<body>
<div class="container">
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
</div>
</body>
</html>
方法二:display:grid
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.container {
height: 500px;
display: grid;
grid-template-rows: repeat(5, 1fr);
grid-auto-flow: column;
background: lightcoral;
position:absolute;
}
.container div {
background: lightblue;
}
</style>
</head>
<body>
<div class="container">
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
<div>斯为泰山而不骄</div>
</div>
</body>
</html>
此处子元素可设置宽高,若不设置,盒子宽高自适应内容。