首先将容器属性设置成
.container {
display: flex;
}
然后就是内容的渲染
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexbox Layout Example</title>
<style>
.container {
display: flex;
flex-direction: row; /* 子元素沿水平方向排列 */
justify-content: space-between; /* 子元素之间的空间分布 */
align-items: center; /* 子元素在垂直方向居中对齐 */
height: 100vh; /* 容器高度为视口高度 */
border: 1px solid #ccc;
}
.item {
background-color: lightblue;
padding: 20px;
margin: 10px;
border: 1px solid #000;
flex: 1 1 auto; /* 允许子元素伸缩 */
text-align: center;
}
.item:nth-child(2) {
flex: 2; /* 第二个子元素占用的空间是其他子元素的两倍 */
}
</style>
</head>
<body>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2 (Flex: 2)</div>
<div class="item">Item 3</div>
</div>
</body>
</html>