flex实现布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>flex布局</title>
<style>
body *:not(.container){
background-color: lightcoral;
}
header,footer{
height: 8vh;
}
.container{
height: calc(84vh - 1em);
display: flex;
margin: 0.5em 0;
}
.container aside{
min-width: 15em;
}
.container main{
min-width: calc(100% - 30em - 1em);
margin: 0 0.5em;
}
</style>
</head>
<body>
<header>页眉</header>
<div class="container">
<aside>左侧</aside>
<main>内容区</main>
<aside>右侧</aside>
</div>
<footer>页脚</footer>
</body>
</html>
总结:
- 任何元素都可以通过添加
display: flex;
属性,转为弹性盒元素- 转为flex元素后,它的内部的
子元素
就支持flex布局了- 使用
display: flex;
属性的元素称为:flex容器- flex容器中的
子元素
称为:flex项目- 容器中的项目自动转为行内块元素
flex容器属性
flex-direction属性应用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>flex-direction</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container,.container1,.container2,.container3{
/* 转为flex弹性布局元素 */
display: flex;
height: 10em;
border: 1px solid #000;
padding: 1em;
margin: 1em;
}
.container>.item,
.container1>.item,
.container2>.item,
.container3>.item{
width: 2em;
background-color: lightcyan;
border: 1px solid #000;
}
.container{
flex-direction:row;
}
.container2{
flex-direction:row-reverse;
}
.container1{
flex-direction:column;
}
.container3{
flex-direction:column-reverse;
}
</style>
</head>
<body>
<h2>行方向水平显示默认靠左</h2>
<div class="container">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
<h2>行方向水平显示顺序相反默认靠右</h2>
<div class="container2">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
<h2>列方向垂直显示默认靠上</h2>
<div class="container1">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
<h2>列方向垂直显示顺序相反默认靠下</h2>
<div class="container3">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
</body>
</html>
flex-wrap属性应用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>flex-wrap</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container,.container1,.container2,.container3{
/* 转为flex弹性布局元素 */
display: flex;
height: 10em;
border: 1px solid #000;
padding: 1em;
margin: 1em;
}
.container>.item,
.container1>.item,
.container2>.item,
.container3>.item{
width: 2em;
background-color: lightcyan;
border: 1px solid #000;
}
.container{
flex-wrap:nowrap;
}
.container2{
flex-wrap:wrap;
}
.container1{
flex-wrap:wrap-reverse;
}
.container3{
flex-direction:column-reverse;
}
</style>
</head>
<body>
<h2>不换行</h2>
<div class="container">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
<div class="item">test5</div>
<div class="item">test6</div>
<div class="item">test7</div>
<div class="item">test8</div>
<div class="item">test9</div>
<div class="item">test10</div>
<div class="item">test11</div>
<div class="item">test12</div>
<div class="item">test13</div>
<div class="item">test14</div>
</div>
<h2>换行</h2>
<div class="container2">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
<div class="item">test5</div>
<div class="item">test6</div>
<div class="item">test7</div>
<div class="item">test8</div>
<div class="item">test9</div>
<div class="item">test10</div>
<div class="item">test11</div>
<div class="item">test12</div>
<div class="item">test13</div>
<div class="item">test14</div>
</div>
<h2>换行顺序相反</h2>
<div class="container1">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
<div class="item">test5</div>
<div class="item">test6</div>
<div class="item">test7</div>
<div class="item">test8</div>
<div class="item">test9</div>
<div class="item">test10</div>
<div class="item">test11</div>
<div class="item">test12</div>
<div class="item">test13</div>
<div class="item">test14</div>
</div>
</body>
</html>
flex-flow属性应用
- flex-flow属性是flex-direction和flex-wrap的组合方式
- flex-flow: row nowrap这样的书写形式
对其方式
justify-content(主轴 x轴)属性应用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>主轴对其方式</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container,.container1,.container2{
/* 转为flex弹性布局元素 */
display: flex;
height: 10em;
border: 1px solid #000;
padding: 1em;
margin: 1em;
}
.container>.item,
.container1>.item,
.container2>.item{
width: 2em;
background-color: lightcyan;
border: 1px solid #000;
}
.container{
justify-content:flex-start;
}
.container2{
justify-content:center;
}
.container1{
justify-content:flex-end;
}
</style>
</head>
<body>
<h2>靠左方式(默认)</h2>
<div class="container">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
<h2>居中方式</h2>
<div class="container2">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
<h2>靠右方式</h2>
<div class="container1">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>主轴对其方式</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container,.container1,.container2{
/* 转为flex弹性布局元素 */
display: flex;
height: 10em;
border: 1px solid #000;
padding: 1em;
margin: 1em;
}
.container>.item,
.container1>.item,
.container2>.item{
width: 2em;
background-color: lightcyan;
border: 1px solid #000;
}
.container{
justify-content:space-around;
}
.container2{
justify-content:space-between;
}
.container1{
justify-content:space-evenly;
}
</style>
</head>
<body>
<h2>分散对其</h2>
<div class="container">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
<h2>两端对其</h2>
<div class="container2">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
<h2>平均对其</h2>
<div class="container1">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
</body>
</html>
justify-content(侧轴 Y轴)属性应用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>侧轴对其方式</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container,.container1,.container2,.container3{
/* 转为flex弹性布局元素 */
display: flex;
height: 10em;
border: 1px solid #000;
padding: 1em;
margin: 1em;
}
.container>.item,
.container1>.item,
.container2>.item,
.container3>.item{
width: 2em;
background-color: lightcyan;
border: 1px solid #000;
}
.container{
align-items:stretch;
}
.container2{
align-items:flex-start;
}
.container1{
align-items:flex-end;
}
.container3{
align-items:center;
}
</style>
</head>
<body>
<h2>默认拉升等高</h2>
<div class="container">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
<h2>靠上部对其</h2>
<div class="container2">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
<h2>靠底部对其</h2>
<div class="container1">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
<h2>居中对其</h2>
<div class="container3">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
</body>
</html>
align-content(交叉轴)属性应用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>多行对其</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container,.container1,.container2,.container3{
/* 转为flex弹性布局元素 */
display: flex;
height: 10em;
border: 1px solid #000;
padding: 1em;
margin: 1em;
}
.container>.item,
.container1>.item,
.container2>.item,
.container3>.item{
width: 4em;
background-color: lightcyan;
border: 1px solid #000;
}
.container{
flex-wrap: wrap;
align-content:stretch;
}
.container2{
flex-wrap: wrap;
align-content:flex-start;
}
.container1{
flex-wrap: wrap;
align-content:flex-end;
}
.container3{
flex-wrap: wrap;
align-content:center;
}
</style>
</head>
<body>
<h2>默认值,撑满整个交叉轴空间</h2>
<div class="container">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
<div class="item">test5</div>
<div class="item">test6</div>
<div class="item">test7</div>
<div class="item">test8</div>
<div class="item">test9</div>
<div class="item">test10</div>
<div class="item">test11</div>
<div class="item">test12</div>
</div>
<h2>靠上部对其</h2>
<div class="container2">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
<div class="item">test5</div>
<div class="item">test6</div>
<div class="item">test7</div>
<div class="item">test8</div>
<div class="item">test9</div>
<div class="item">test10</div>
<div class="item">test11</div>
<div class="item">test12</div>
</div>
<h2>靠底部对其</h2>
<div class="container1">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
<div class="item">test5</div>
<div class="item">test6</div>
<div class="item">test7</div>
<div class="item">test8</div>
<div class="item">test9</div>
<div class="item">test10</div>
<div class="item">test11</div>
<div class="item">test12</div>
</div>
<h2>居中对其</h2>
<div class="container3">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
<div class="item">test5</div>
<div class="item">test6</div>
<div class="item">test7</div>
<div class="item">test8</div>
<div class="item">test9</div>
<div class="item">test10</div>
<div class="item">test11</div>
<div class="item">test12</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>多行对其</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container,.container1,.container2,.container3{
/* 转为flex弹性布局元素 */
display: flex;
height: 10em;
border: 1px solid #000;
padding: 1em;
margin: 1em;
}
.container>.item,
.container1>.item,
.container2>.item,
.container3>.item{
width: 4em;
background-color: lightcyan;
border: 1px solid #000;
}
.container{
flex-wrap: wrap;
align-content:space-between;
}
.container2{
flex-wrap: wrap;
align-content:space-around;
}
.container1{
flex-wrap: wrap;
align-content:space-evenly;
}
.container3{
flex-wrap: wrap;
align-content:center;
}
</style>
</head>
<body>
<h2>两端对其</h2>
<div class="container">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
<div class="item">test5</div>
<div class="item">test6</div>
<div class="item">test7</div>
<div class="item">test8</div>
<div class="item">test9</div>
<div class="item">test10</div>
<div class="item">test11</div>
<div class="item">test12</div>
</div>
<h2>分散对其</h2>
<div class="container2">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
<div class="item">test5</div>
<div class="item">test6</div>
<div class="item">test7</div>
<div class="item">test8</div>
<div class="item">test9</div>
<div class="item">test10</div>
<div class="item">test11</div>
<div class="item">test12</div>
</div>
<h2>平均对其</h2>
<div class="container1">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
<div class="item">test5</div>
<div class="item">test6</div>
<div class="item">test7</div>
<div class="item">test8</div>
<div class="item">test9</div>
<div class="item">test10</div>
<div class="item">test11</div>
<div class="item">test12</div>
</div>
</body>
</html>
flex项目属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>flex项目</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container,.container1,.container2,.container3{
/* 转为flex弹性布局元素 */
display: flex;
height: 10em;
border: 1px solid #000;
padding: 1em;
margin: 1em;
}
.container>.item,
.container1>.item,
.container2>.item,
.container3>.item{
width: 4em;
background-color: lightcyan;
border: 1px solid #000;
}
.container>.item {
flex: 0 1 auto;
}
.container2>.item{
flex: 1 1 auto;
}
.container1>.item{
flex: 0 0 auto;
}
.container3>.item{
flex-wrap: wrap;
align-content:center;
}
</style>
</head>
<body>
<h2>不放大 可收缩 默认宽度</h2>
<div class="container">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
<h2>可放大 可收缩 默认宽度</h2>
<div class="container2">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
<h2>不放大 不收缩 默认宽度</h2>
<div class="container1">
<div class="item">test1</div>
<div class="item">test2</div>
<div class="item">test3</div>
<div class="item">test4</div>
</div>
<h2>order排序,值越小越靠前</h2>
<div class="container3">
<div class="item" style="order:4">test1</div>
<div class="item" style="order:1">test2</div>
<div class="item" style="order:3">test3</div>
<div class="item" style="order:2">test4</div>
<div class="item" style="order:-1">我最靠前</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>align-self</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container,.container1,.container2,.container3{
/* 转为flex弹性布局元素 */
display: flex;
height: 10em;
border: 1px solid #000;
padding: 1em;
margin: 1em;
}
.container>.item{
width: 4em;
background-color: lightcyan;
border: 1px solid #000;
}
.container>.item {
flex: 0 1 auto;
}
</style>
</head>
<body>
<h2>设置项目自身对其方式</h2>
<div class="container">
<div class="item" style="align-self:center">test1</div>
<div class="item" style="align-self:flex-start">test2</div>
<div class="item" style="align-self:flex-end">test3</div>
</div>
</body>
</html>