1、项目-对齐方式
弹性盒里边的小盒子叫项目
(1)例子1
效果:
代码:
<!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{
width: 500px;
height: 500px;
border: 2px solid black;
margin: 100px auto;
display: flex;
}
.box div{
width: 100px;
/* height: 100px; */
border: 1px dashed red;
box-sizing: border-box;
}
.div1{
height: 100px;
align-self: flex-start;
}
.div2{
height: 100px;
align-self: flex-end;
}
.div3{
height: 100px;
align-self: center;
}
.div4{
height: 100px;
align-self: baseline;
}
.div5{
align-self: stretch;
}
</style>
</head>
<body>
<div class="box">
<div class="div1">111</div>
<div class="div2">222</div>
<div class="div3">333</div>
<div class="div4">444</div>
<div class="div5">555</div>
</div>
</body>
</html>
(2)例子2
效果:
代码:
<!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{
width: 500px;
height: 500px;
border: 2px solid black;
margin: 100px auto;
display: flex;
flex-direction: column;
}
.box div{
/* width: 100px; */
height: 100px;
border: 1px dashed red;
box-sizing: border-box;
}
.div1{
align-self: flex-start;
}
.div2{
align-self: flex-end;
}
.div3{
align-self: center;
}
.div4{
align-self: baseline;
}
.div5{
align-self: stretch;
}
</style>
</head>
<body>
<div class="box">
<div class="div1">111</div>
<div class="div2">222</div>
<div class="div3">333</div>
<div class="div4">444</div>
<div class="div5">555</div>
</div>
</body>
</html>
2、项目-调整顺序order
效果:
代码:
<!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{
width: 500px;
height: 500px;
border: 2px solid black;
margin: 100px auto;
display: flex;
flex-direction: row-reverse;
}
.box div{
width: 100px;
height: 100px;
border: 1px dashed red;
box-sizing: border-box;
}
.div3{
/*
order默认值是0
order值越大,越往后跑
order值一样,按顺序来
*/
order: 1;
}
.box4{
order: 1;
}
.div5{
order: -1;
}
</style>
</head>
<body>
<div class="box">
<div class="div1">111</div>
<div class="div2">222</div>
<div class="div3">333</div>
<div class="div4">444</div>
<div class="div5">555</div>
</div>
</body>
</html>
3、项目-剩余宽高flex
(1)案例一
效果:
代码:
<!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{
width: 500px;
height: 500px;
border: 2px solid black;
margin: 0 auto;
display: flex;
}
.box div{
width: 100px;
height: 100px;
border: 1px dashed red;
}
.div1,.div3{
flex: 1;
}
.div2{
/* 占满剩余空间 */
/* 分成六份,div2占5份大小 */
flex: 5;
}
</style>
</head>
<body>
<div class="box">
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
</div>
</body>
</html>
(2)案例二
效果:
代码:
<!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>
*{
margin: 0%;
padding: 0%;
}
html,body{
height: 100%;
}
body{
display: flex;
}
.box1,.box3{
/* 注意根据需求只设宽度或高度 */
width: 100px;
background: grey;
}
.box2{
flex: 1;
background: yellow;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>
(3)案例三
效果:
代码:
<!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>
*{
margin: 0%;
padding: 0%;
}
html,body{
height: 100%;
}
body{
display: flex;
flex-direction: column;
}
.box1,.box3{
/* 注意根据需求只设宽度或高度 */
height: 100px;
background: grey;
}
.box2{
flex: 1;
background: yellow;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>