实现效果:三个div,当屏幕尺寸较大时,一行显示,缩小后显示两个并换行,再缩小一行只显示一个。
<!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>
<style>
.div0{
width:100%;
height:500px;
border:1px solid #ccc;
}
/* 子div浮动一行显示 */
.div0 div{
float:left;
height:200px;
}
/* 一行3个div */
@media screen and (min-width:400px){
.div0 div{
width: 33.3%;
}
.div0 div:nth-child(1){
background-color: red;
}
.div0 div:nth-child(2){
background-color: green;
}
.div0 div:nth-child(3){
background-color: blue;
}
}
/* 两行3个div */
@media screen and (min-width:300px) and (max-width:399px){
.div0 div{
width: 50%;
}
.div0 div:nth-child(1){
background-color: red;
}
.div0 div:nth-child(2){
background-color: green;
}
.div0 div:nth-child(3){
background-color: blue;
}
}
/* 三行3个div */
@media screen and (min-width:200px) and (max-width:299px){
.div0 div{
width: 100%;
}
.div0 div:nth-child(1){
background-color: red;
}
.div0 div:nth-child(2){
background-color: green;
}
.div0 div:nth-child(3){
background-color: blue;
}
}
</style>
</head>
<body>
<div class="div0">
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>