flex根据内容自动撑起宽度并换行每行保持宽度一致
html
<!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>根据内容撑起宽度</title>
<style>
.container {
display: flex;
flex-wrap: wrap;
padding: 16px;
border: 1px solid #ccc;
max-width: 100%; /* 防止超出父容器 */
}
.item {
flex: 1;
padding: 8px;
margin: 4px;
background: #eee;
text-align: center;
white-space: nowrap; /* 禁止换行 */
}
</style>
</head>
<body>
<div class="container">
<div class="item">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</div>
<div class="item">AAAAAAAAAAAAAAAAA</div>
<div class="item">BBBBBBBBBBBBBBBBB</div>
<div class="item">C</div>
<div class="item">D</div>
<div class="item">E</div>
</div>
</body>
</html>