达成效果:
1. 使用justify-content: space-evenly;
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>flex evenly</title>
<style>
* {
margin: 0;
padding: 0;
}
.wrap {
display: flex;
justify-content: space-evenly;
width: 700px;
height: 400px;
margin: 100px auto;
background-color: #ccc;
}
.item {
width: 140px;
height: 120px;
background-color: #368;
}
</style>
</head>
<body>
<div class="wrap">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</body>
</html>
缺点:苹果的兼容性不好,推荐以下方法
2. space-between配合before+after实现space-evenly效果
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>flex evenly兼容</title>
<style>
* {
margin: 0;
padding: 0;
}
.wrap {
display: flex;
justify-content: space-between;
width: 600px;
height: 400px;
margin: 100px auto;
background-color: #ccc;
}
.wrap:before,
.wrap:after {
/* 用空内容来占位达到效果 */
content: '';
}
.item {
width: 140px;
height: 120px;
background-color: #368;
}
</style>
</head>
<body>
<div class="wrap">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</body>
</html>