flex简介
flex 是 Flexible Box 的缩写,就是弹性盒子布局的意思
给父元素加display:flex;父元素就是弹性容器.子盒子就是弹性盒子
flex属性
主轴对其方式
flex-start 默认值, 起点开始依次排列
<!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>
.box{
display: flex;
width: 800px;
height: 600px;
background-color: red;
}
.son{
width: 100px;
height: 100px;
background-color: cyan;
}
</style>
</head>
<body>
<div class="box">
<div class="son"></div>
<div class="son"></div>
<div class="son"></div>
<div class="son"></div>
</div>
</body>
</html>
flex-end 终点开始依次排列
justify-content: flex-end;
center 沿主轴居中排列
justify-content: center;
space-around 弹性盒子沿主轴均匀排列, 空白间距均分在弹性盒子两侧
justify-content:space-around;
space-between 弹性盒子沿主轴均匀排列, 空白间距均分在相邻盒子之间
justify-content:space-between;
space-evenly 弹性盒子沿主轴均匀排列, 弹性盒子与容器之间间距相等
justify-content:space-evenly;
侧轴对齐方式
center 沿侧轴居中排列
align-items: center;
设置主轴方向
主轴默认是水平方向,侧轴默认是垂直方向
修改主轴方向属性:flex-direction
row 行,水平(默认值)
column 列,垂直
圣杯布局
左右侧固定,中间自适应
<!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>
.box{
display: flex;
width: 100%;
height: 600px;
background-color: red;
}
.box .left{
width: 100px;
height: 100px;
background-color: green;
}
.box .right{
width: 100px;
height: 100px;
background-color: pink;
}
.box .center{
flex: 1;
background-color: purple;
}
</style>
</head>
<body>
<div class="box">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
</body>
</html>