初始化代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
margin: 0;
padding: 0;
}
.box {
display: flex;
display: -webkit-flex;
height: 500px;
background-color: wheat;
}
.sub {
background-color: #000;
color: white;
font-size: 32px;
width: 70px;
height: 70px;
line-height: 70px;
text-align: center;
border: 2px solid aquamarine;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="box">
<div class="sub">1</div>
<div class="sub">2</div>
<div class="sub">3</div>
<div class="sub">4</div>
<div class="sub">5</div>
<div class="sub">6</div>
</div>
</body>
</html>
(演示时,要给外框的加上 flex-wrap: wrap;
这个属性一起使用, 同时将盒子的宽高均调到 200px)
<!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>
.father {
width: 200px;
height: 200px;
background-color: antiquewhite;
display: flex;
justify-content: center;
}
.son {
background-color: aqua;
}
</style>
</head>
<body>
<div class="father">
<span class="son">son</span>
</div>
</body>
</html>
<!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>
.father {
width: 200px;
height: 200px;
background-color: antiquewhite;
display: flex;
align-items: center;
}
.son {
background-color: aqua;
}
</style>
</head>
<body>
<div class="father">
<span class="son">son</span>
</div>
</body>
</html>
<!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>
.father {
width: 200px;
height: 200px;
border: 1px solid red;
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
.son {
width: 100px;
height: 100px;
background: yellow;
}
</style>
</head>
<body>
<div class="father">
<span class="son">son</span>
</div>
</body>
</html>
<!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 {
height: 200px;
display: flex;
}
.box-left {
width: 200px;
background-color: aquamarine;
}
.box-right {
flex-grow: 1;
overflow: hidden;
background-color: blueviolet;
}
</style>
</head>
<body>
<div class="box">
<div class="box-left"></div>
<div class="box-right"></div>
</div>
</body>
</html>
<!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>
.layout.flexbox .left-center-right {
display: flex;
}
.layout.flexbox .left {
width: 300px;
background: red;
}
.layout.flexbox .center {
flex: 1;
background: yellow;
}
.layout.flexbox .right {
width: 300px;
background: blue;
}
</style>
</head>
<body>
<section class="layout flexbox">
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>flexbox的解决方案</h1>
<p>1.这是布局的中间部分</p>
<p>2.这是布局的中间部分</p>
</div>
<div class="right"></div>
</article>
</section>
</body>
</html>