1.无flex布局的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
</head>
<body>
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</body>
</html>
.container {
/* display: flex; */
}
.box {
width: 100px;
height: 100px;
}
.box:nth-child(1) {
background-color: blue;
}
.box:nth-child(2) {
background-color: red;
}
.box:nth-child(3) {
background-color: green;
}
.box:nth-child(4) {
background-color: yellow;
}
水平方向
2.使用了flex布局
.container {
display: flex;
}
.box {
width: 100px;
height: 100px;
}
.box:nth-child(1) {
background-color: blue;
}
.box:nth-child(2) {
background-color: red;
}
.box:nth-child(3) {
background-color: green;
}
.box:nth-child(4) {
background-color: yellow;
}
2.宽度自适应flex
.container {
display: flex;
}
.box {
flex: 1;
/* width: 100px; */
height: 100px;
border: black solid 1px;
}
.box:nth-child(1) {
background-color: blue;
}
.box:nth-child(2) {
background-color: red;
}
.box:nth-child(3) {
background-color: green;
}
.box:nth-child(4) {
background-color: yellow;
}
3.自定比例的flex
.container {
display: flex;
}
.box {
flex: 1; /* 默认都是1 */
/* width: 100px; */
height: 100px;
border: black solid 1px;
}
.box:nth-child(1) {
flex: 4;/* 在第一个这里添加比例 */
background-color: blue;
}
.box:nth-child(2) {
background-color: red;
}
.box:nth-child(3) {
background-color: green;
}
.box:nth-child(4) {
background-color: yellow;
}
4.反向排序
.container {
display: flex;
flex-direction: row-reverse; /* 使其反向排序 带有 reverse 的参数会返向排序 */
}
.box {
flex: 1;
/* width: 100px; */
height: 100px;
border: black solid 1px;
}
.box:nth-child(1) {
flex: 4;
background-color: blue;
}
.box:nth-child(2) {
background-color: red;
}
.box:nth-child(3) {
background-color: green;
}
.box:nth-child(4) {
background-color: yellow;
}
水平居中
.container {
display: flex;
/* flex-direction: row-reverse; */ /* 返向排序 */
justify-content: center; /* 水平方向居中 */
}
.box {
/* flex: 1; */
width: 100px;
height: 100px;
border: black solid 1px;
}
.box:nth-child(1) {
/* flex: 4; */
background-color: blue;
}
.box:nth-child(2) {
background-color: red;
}
.box:nth-child(3) {
background-color: green;
}
.box:nth-child(4) {
background-color: yellow;
}
尾部
.container {
display: flex;
justify-content: flex-end; /* 尾部 */
}
.box {
/* flex: 1; */
width: 100px;
height: 100px;
border: black solid 1px;
}
.box:nth-child(1) {
/* flex: 4; */
background-color: blue;
}
.box:nth-child(2) {
background-color: red;
}
.box:nth-child(3) {
background-color: green;
}
.box:nth-child(4) {
background-color: yellow;
}
持续更新。。。
阮一峰 好文链接
http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html
http://www.ruanyifeng.com/blog/2015/07/flex-examples.html