弹性布局可以用于做响应式页面的制作
使用弹性布局需要使用要display里的flex属性
例:让一个div是弹性布局,代码如下
.div{
display:flex;
}
flex里有很多属性,其中设置在容器上的属性有6个
<head>
<meta charset="utf-8">
<title>弹性布局title>
<style>
.div{
width: 1300px;
background-color: green;
height: 200px;
display: flex;
flex-direction:row;
}
.div1,.div2,.div3{
width: 300px;
height: 200px;
}
.div1{
background-color: red;
}
.div2{
background-color: darkcyan;
}
.div3{
background-color: orange;
}
style>
head>
<body>
<div class="div">
<div class="div1">onediv>
<div class="div2">twodiv>
<div class="div3">threediv>
div>
body>
.div{
width: 1300px;
background-color: green;
height: 200px;
display: flex;
flex-direction:row;
}
.div{
width: 1300px;
background-color: green;
height: 200px;
display: flex;
flex-direction:column;
}
div的高度被自动压缩
.div{
width: 1300px;
background-color: green;
height: 200px;
display: flex;
flex-direction:column-reverse;
}
.div{
width: 100%;
background-color: green;
height: 200px;
display: flex;
flex-wrap: wrap;
}
.div{
width: 100%;
background-color: green;
height: 200px;
display: flex;
flex-wrap: wrap;
}
div不换行,宽度自动收缩
.div{
width: 100%;
background-color: green;
height: 200px;
display: flex;
flex-wrap: wrap-reverse;
margin-top: 200px;
}
例
.div{
width: 100%;
background-color: green;
height: 200px;
display: flex;
flex-flow: row wrap;
margin-top: 200px;
}
.div{
width: 100%;
background-color: green;
height: 200px;
display: flex;
justify-content:flex-start;
margin-top: 200px;
}
.div{
width: 100%;
background-color: green;
height: 200px;
display: flex;
justify-content:flex-end;
margin-top: 200px;
}
结果如下
.div{
width: 100%;
background-color: green;
height: 200px;
display: flex;
justify-content:center;
margin-top: 200px;
}
-space-around:每个项目两侧的间隔相等
例
.div{
width: 100%;
background-color: green;
height: 200px;
display: flex;
justify-content:space-around;
margin-top: 200px;
}
-space-between:两端对齐,项目之间的间隔都相等
例
.div{
width: 100%;
background-color: green;
height: 200px;
display: flex;
justify-content:space-between;
margin-top: 200px;
}
.div{
width: 100%;
background-color: green;
height: 200px;
display: flex;
justify-content:space-between;
align-item:flex-start;
}
.div{
width: 100%;
background-color: green;
height: 200px;
display: flex;
justify-content:space-between;
align-item:flex-end;
}
结果如下
.div{
width: 100%;
background-color: green;
height: 200px;
display: flex;
justify-content:space-between;
align-item:center;
}
结果如下
.div{
width: 100%;
background-color: green;
height: 500px;
display: flex;
justify-content: space-between;
align-items: stretch;
}
.div{
width: 100%;
background-color: green;
height: 500px;
display: flex;
justify-content: space-between;
align-items: stretch;
}