CSS的浮動(dòng)和flex布局
浮動(dòng)案例
<!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>
.content {
width: 1190px;
margin: 0 auto;
}
.wrapper {
margin-right: -10px;
}
.item {
width: 290px;
background-color: purple;
float: left;
margin-bottom: 10px;
margin-right: 10px;
}
.item.left {
height: 370px;
}
.item.right {
height: 180px;
}
</style>
</head>
<body>
<div class="content">
<div class="wrapper">
<div class="item left"></div>
<div class="item left"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
</div>
</div>
</body>
</html>
該思路有點(diǎn)問題,突出邊框
<!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>
.content {
width: 1100px;
margin: 0 auto;
height: 800px;
background-color: orange;
}
.box {
border: 1px solid #000;
margin-right: -1px;
height: 168px;
border-right: none;
}
.item {
width: 220px;
height: 168px;
float: left;
border-right: 1px solid #000;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="content">
<div class="box">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
</div>
</div>
</body>
</html>
小插曲 margin-left重疊
<!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>
span,
strong {
float: left;
}
span {
background-color: #f00;
}
strong {
background-color: #0f0;
margin-left: -10px;
}
</style>
</head>
<body>
<span>我是span元素</span>
<strong>我是strong元素</strong>
</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>
.content {
width: 1100px;
margin: 0 auto;
height: 800px;
background-color: orange;
}
.item {
width: 221px;
height: 168px;
float: left;
background-color: purple;
border: 1px solid #000;
margin-left: -1px;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="content">
<div class="box">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
</div>
</div>
</body>
</html>
正規(guī)思路一
<!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>
.content {
width: 1100px;
margin: 0 auto;
height: 800px;
background-color: orange;
}
.box {
margin-left: 1px;
}
.item {
width: 221px;
height: 168px;
float: left;
background-color: purple;
border: 1px solid #000;
margin-left: -1px;
box-sizing: border-box;
}
.item.first {
width: 220px;
}
</style>
</head>
<body>
<div class="content">
<div class="box">
<div class="item first">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
</div>
</div>
</body>
</html>
正規(guī)思路二
<!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>
.content {
width: 1100px;
margin: 0 auto;
height: 800px;
background-color: orange;
}
/* .box {
margin-left: 1px;
} */
.item {
width: 221px;
height: 168px;
float: left;
background-color: purple;
border: 1px solid #000;
margin-right: -1px;
box-sizing: border-box;
}
.item.first {
width: 220px;
}
</style>
</head>
<body>
<div class="content">
<div class="box">
<div class="item first">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
</div>
</div>
</body>
</html>
清除浮動(dòng)
高度塌陷
<!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>
.content {
width: 1190px;
margin: 0 auto;
background-color: #f00;
}
.wrapper {
margin-right: -10px;
}
.item {
width: 290px;
background-color: purple;
float: left;
margin-bottom: 10px;
margin-right: 10px;
}
.item.left {
height: 370px;
}
.item.right {
height: 180px;
}
.other {
height: 100px;
background-color: #0f0;
}
</style>
</head>
<body>
<div class="content">
<div class="wrapper">
<div class="item left"></div>
<div class="item left"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
</div>
</div>
<div class="other"></div>
</body>
</html>
高度塌陷解決方案
- 清楚浮動(dòng)的目的是
- 讓父元素計(jì)算總高度的時(shí)候羔巢,把浮動(dòng)子元素的高度算進(jìn)去
清楚浮動(dòng)的方案
給父元素設(shè)置固定高度
<!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>
.content {
width: 1190px;
margin: 0 auto;
background-color: #f00;
height: 560px;
}
.wrapper {
margin-right: -10px;
}
.item {
width: 290px;
background-color: purple;
float: left;
margin-bottom: 10px;
margin-right: 10px;
}
.item.left {
height: 370px;
}
.item.right {
height: 180px;
}
.other {
height: 100px;
background-color: #0f0;
}
</style>
</head>
<body>
<div class="content">
<div class="wrapper">
<div class="item left"></div>
<div class="item left"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
</div>
</div>
<div class="other"></div>
</body>
</html>
添加一個(gè)空塊級(jí)元素
- 給父元素最后添加一個(gè)空的塊級(jí)子元素,并且設(shè)置clear: both;
- clear屬性可以指定一個(gè)元素是否必須移動(dòng)(清除浮動(dòng))到在他之前的浮動(dòng)元素的下面
- 取值
- left
- right
- both
- none
<!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>
.content {
width: 1190px;
margin: 0 auto;
background-color: #f00;
}
.wrapper {
margin-right: -10px;
}
.item {
width: 290px;
background-color: purple;
float: left;
margin-bottom: 10px;
margin-right: 10px;
}
.item.left {
height: 370px;
}
.item.right {
height: 180px;
}
.other {
height: 100px;
background-color: #0f0;
}
.line {
height: 0;
clear: both;
}
</style>
</head>
<body>
<div class="content">
<div class="wrapper">
<div class="item left"></div>
<div class="item left"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="line"></div>
</div>
</div>
<div class="other"></div>
</body>
</html>
父元素設(shè)置::after
<!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>
.content {
width: 1190px;
margin: 0 auto;
background-color: #f00;
}
.wrapper {
margin-right: -10px;
}
.item {
width: 290px;
background-color: purple;
float: left;
margin-bottom: 10px;
margin-right: 10px;
}
.item.left {
height: 370px;
}
.item.right {
height: 180px;
}
.other {
height: 100px;
background-color: #0f0;
}
.bin::after {
content: "";
display: block;
clear: both;
}
</style>
</head>
<body>
<div class="content">
<div class="wrapper bin">
<div class="item left"></div>
<div class="item left"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
</div>
</div>
<div class="other"></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>
.content {
width: 1190px;
margin: 0 auto;
background-color: #f00;
}
.wrapper {
margin-right: -10px;
}
.item {
width: 290px;
background-color: purple;
float: left;
margin-bottom: 10px;
margin-right: 10px;
}
.item.left {
height: 370px;
}
.item.right {
height: 180px;
}
.other {
height: 100px;
background-color: #0f0;
}
.clearFix::after {
content: "";
display: block;
clear: both;
}
</style>
</head>
<body>
<div class="content">
<div class="wrapper clearFix">
<div class="item left"></div>
<div class="item left"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
</div>
</div>
<div class="other"></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>
.content {
width: 1190px;
margin: 0 auto;
background-color: #f00;
}
.wrapper {
margin-right: -10px;
}
.item {
width: 290px;
background-color: purple;
float: left;
margin-bottom: 10px;
margin-right: 10px;
}
.item.left {
height: 370px;
}
.item.right {
height: 180px;
}
.other {
height: 100px;
background-color: #0f0;
}
.clearFix::after {
content: "";
display: block;
clear: both;
/* 瀏覽器兼容 */
visibility: hidden;
height: 01;
}
/* 兼容IE6/7 */
.clearFix {
*zoom: 1;
}
</style>
</head>
<body>
<div class="content">
<div class="wrapper clearFix">
<div class="item left"></div>
<div class="item left"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>
</div>
</div>
<div class="other"></div>
</body>
</html>
flex布局
- flexbox
- 彈性盒子是一種用于按行或按列布局元素的一維布局方案
- 元素可以膨脹以填充額外的空間,收縮以適應(yīng)更小的空間
- 通常我們使用flexbox來進(jìn)行布局的方案稱之為flex布局
display: flex;
<!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;
background-color: #f00;
}
</style>
</head>
<body>
aaaa
<div class="box">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
bbb
</body>
</html>
display: inline-flex;
<!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: inline-flex;
background-color: #f00;
}
</style>
</head>
<body>
aaaa
<div class="box">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
bbb
</body>
</html>
flex
<!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;
background-color: #f00;
width: 200px;
height: 200px;
flex-wrap: wrap;
}
.item {
width: 60px;
height: 60px;
background-color: orange;
}
</style>
</head>
<body>
<div class="box">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">4</div>
<div class="item">5</div>
</div>
</body>
</html>
flex container
- flex-flow
- flex-direction
- flex-wrap
- justify-content
- align-items
- align-content
flex items
- flex-grow
- flex-basis
- flex-shrink
- order
- align-self
- flex
flex-direction
- 修改主軸的方向
- 屬性值
- row
- row-reverse
- column
- column-reverse
- 屬性值
<!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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
/* 默認(rèn)值 */
/* flex-direction: row; */
flex-direction: column;
}
.item {
width: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item5">5</div>
</div>
<!-- <script src="./itemRandomColor.js"></script> -->
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
/* 默認(rèn)值 */
/* flex-direction: row; */
/* flex-direction: column; */
flex-direction: row-reverse;
}
.item {
width: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item5">5</div>
</div>
<!-- <script src="./itemRandomColor.js"></script> -->
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
/* 默認(rèn)值 */
/* flex-direction: row; */
/* flex-direction: column; */
/* flex-direction: row-reverse; */
flex-direction: column-reverse;
}
.item {
width: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item5">5</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</body>
</html>
flex-wrap
- 是單行還是多行
- nowrap 不換行 (默認(rèn)值)
- wrap
- wrap-reverse
<!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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
/* 默認(rèn)值 */
flex-wrap: nowrap;
/* flex-wrap: wrap; */
}
.item {
width: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item5">5</div>
<div class="item item5">6</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
/* 默認(rèn)值 */
/* flex-wrap: nowrap; */
flex-wrap: wrap;
}
.item {
width: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item5">5</div>
<div class="item item5">6</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
/* 默認(rèn)值 */
/* flex-wrap: nowrap; */
/* flex-wrap: wrap; */
flex-wrap: wrap-reverse;
}
.item {
width: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item5">5</div>
<div class="item item5">6</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</body>
</html>
flex-flow
- flex-direction和flex-wrap的簡(jiǎn)寫屬性
- 任意順序贷币,并且都可以省略
<!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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
/* 默認(rèn)值 */
/* flex-wrap: nowrap; */
/* flex-wrap: wrap; */
/* flex-wrap: wrap-reverse; */
flex-flow: wrap;
}
.item {
width: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item5">5</div>
<div class="item item5">6</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
/* 默認(rèn)值 */
/* flex-wrap: nowrap; */
/* flex-wrap: wrap; */
/* flex-wrap: wrap-reverse; */
flex-flow: row-reverse wrap;
}
.item {
width: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item5">5</div>
<div class="item item5">6</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
/* flex-direction: row-reverse;
flex-wrap: wrap-reverse; */
flex-flow: row-reverse wrap-reverse;
}
.item {
width: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
/* flex-direction: row-reverse;
flex-wrap: wrap-reverse; */
/* flex-flow: row-reverse wrap-reverse; */
flex-flow: column-reverse wrap-reverse;
}
.item {
width: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
/* flex-direction: row-reverse;
flex-wrap: wrap-reverse; */
/* flex-flow: row-reverse wrap-reverse; */
flex-flow: column wrap-reverse;
}
.item {
width: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</body>
</html>
justify-content
<!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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
/* justify-content: flex-start; */
/* justify-content: flex-end; */
/* justify-content: center; */
justify-content: space-around;
/* justify-content: space-between; */
/* justify-content: space-evenly; */
}
.item {
width: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</body>
</html>
align-items
<!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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
/* align-items: flex-start; */
/* align-items: center; */
/* align-items: flex-end; */
/* align-items: baseline; */
align-items: stretch;
}
.item {
width: 120px;
height: 120px;
}
.item1 {
height: 80px;
/* baseline屬性值專用 */
font-size: 30px;
}
.item2 {
height: 150px;
/* baseline屬性值專用 */
font-size: 40px;
}
.item3 {
height: 60px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
/* align-items: flex-start; */
/* align-items: center; */
/* align-items: flex-end; */
/* align-items: baseline; */
align-items: stretch;
}
.item {
width: 120px;
/* height默認(rèn)為auto,可以不寫 */
height: auto;
}
/* .item1 {
height: 80px;
}
.item2 {
height: 150px;
}
.item3 {
height: 60px;
} */
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
/* align-items: flex-start; */
/* align-items: center; */
/* align-items: flex-end; */
/* align-items: baseline; */
/* align-items: stretch; */
align-items: normal;
}
.item {
width: 120px;
}
/* .item1 {
height: 80px;
}
.item2 {
height: 150px;
}
.item3 {
height: 60px;
} */
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</body>
</html>
align-content
<!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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
flex-wrap: wrap;
/* align-content: flex-start; */
/* align-content: flex-end; */
/* align-content: center; */
/* align-content: space-between; */
/* align-content: space-around; */
align-content: space-evenly;
}
.item {
width: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
flex-wrap: wrap;
justify-content: space-between;
align-content: flex-start;
/* align-content: flex-end; */
/* align-content: center; */
/* align-content: space-between; */
/* align-content: space-around; */
/* align-content: space-evenly; */
}
.item {
width: 120px;
height: 120px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
flex-wrap: wrap;
justify-content: space-between;
/* align-content: stretch;
align-content: normal; */
/* 拉伸 */
/* align-content: stretch; */
/* 拉伸 */
align-content: normal;
}
.item {
width: 120px;
/* height: 120px; */
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
flex-wrap: wrap;
justify-content: space-between;
align-content: center;
}
.item {
width: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</body>
</html>
order
- item的排列順序
- 默認(rèn)值 0
- 整數(shù)
- 數(shù)值越大苛萎,越靠后
<!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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
}
.item {
width: 120px;
height: 120px;
}
.item1 {
order: 5;
}
.item2 {
order: 3;
}
.item3 {
order: 9;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</body>
</html>
align-self
- 可以覆蓋container設(shè)置的align-items
<!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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
align-items: center;
}
.item {
width: 120px;
height: 120px;
}
.item1 {
height: 90px;
}
.item2 {
height: 150px;
align-self: flex-start;
}
.item3 {
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</body>
</html>
flex-grow
<!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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
}
.item {
width: 120px;
height: 120px;
flex-grow: 1;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
}
.item {
width: 120px;
height: 120px;
/* 默認(rèn)值 */
/* flex-grow: 0; */
flex-grow: 1;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
}
.item {
width: 120px;
height: 120px;
/* flex-grow: 1; */
}
.item1 {
flex-grow: 1;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
}
.item {
width: 120px;
height: 120px;
flex-grow: 1;
}
/* .item1 {
flex-grow: 1;
} */
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
}
.item {
width: 120px;
height: 120px;
flex-grow: 1;
}
.item1 {
flex-grow: 2;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
}
.item {
width: 120px;
height: 120px;
flex-grow: 0;
}
.item1 {
/* flex-grow: 1; */
flex-grow: 10000;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
}
.item {
width: 120px;
height: 120px;
flex-grow: 0;
}
.item1 {
flex-grow: 1;
max-width: 150px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</body>
</html>
flex-shrink
<!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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
}
.item {
width: 120px;
height: 120px;
/* 默認(rèn)值 */
/* flex-shrink: 1; */
/* 不收縮 */
flex-shrink: 0;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
}
.item {
width: 120px;
height: 120px;
/* 默認(rèn)值 */
/* flex-shrink: 1; */
/* 不收縮 */
flex-shrink: 0;
}
.item1,
.item2 {
flex-shrink: 1;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">1</div>
<div class="item item5">2</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
}
.item {
width: 120px;
height: 120px;
/* 默認(rèn)值 */
/* flex-shrink: 1; */
/* 不收縮 */
flex-shrink: 0;
}
.item1,
.item2 {
flex-shrink: 1;
}
.item1 {
min-width: 100px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">1</div>
<div class="item item5">2</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</body>
</html>
flex-basis
<!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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
}
.item {
/* width: 120px; */
flex-basis: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
}
.item {
/* width: 120px; */
flex-basis: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2我是why,哈哈哈沮翔,呵呵呵</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
}
.item {
/* width: 120px; */
flex-basis: 120px;
height: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2我是coderwhy_why_hahahaha</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</body>
</html>
flex
單值語法: 值必須為以下其中之一:
- 一個(gè)無單位數(shù)(<number>): 它會(huì)被當(dāng)作flex:<number> 1 0; <flex-shrink>的值被假定為1焰望,然后<flex-basis> 的值被假定為0骚亿。
- 一個(gè)有效的寬度(width)值: 它會(huì)被當(dāng)作 <flex-basis>的值。
- 關(guān)鍵字none熊赖,auto或initial.
雙值語法: 第一個(gè)值必須為一個(gè)無單位數(shù)来屠,并且它會(huì)被當(dāng)作 <flex-grow>的值。第二個(gè)值必須為以下之一:
- 一個(gè)無單位數(shù):它會(huì)被當(dāng)作 <flex-shrink> 的值震鹉。
- 一個(gè)有效的寬度值: 它會(huì)被當(dāng)作 <flex-basis>的值俱笛。
三值語法:
- 第一個(gè)值必須為一個(gè)無單位數(shù),并且它會(huì)被當(dāng)作 <flex-grow>的值传趾。
- 第二個(gè)值必須為一個(gè)無單位數(shù)嫂粟,并且它會(huì)被當(dāng)作 <flex-shrink>的值。
- 第三個(gè)值必須為一個(gè)有效的寬度值墨缘, 并且它會(huì)被當(dāng)作 <flex-basis> 的值星虹。
取值
- initial
元素會(huì)根據(jù)自身寬高設(shè)置尺寸。它會(huì)縮短自身以適應(yīng) flex 容器镊讼,但不會(huì)伸長(zhǎng)并吸收 flex 容器中的額外自由空間來適應(yīng) flex 容器 宽涌。相當(dāng)于將屬性設(shè)置為flex: 0 1 auto。
- auto
元素會(huì)根據(jù)自身的寬度與高度來確定尺寸蝶棋,但是會(huì)伸長(zhǎng)并吸收 flex 容器中額外的自由空間卸亮,也會(huì)縮短自身來適應(yīng) flex 容器。這相當(dāng)于將屬性設(shè)置為 flex: 1 1 auto.
none
元素會(huì)根據(jù)自身寬高來設(shè)置尺寸玩裙。它是完全非彈性的:既不會(huì)縮短兼贸,也不會(huì)伸長(zhǎng)來適應(yīng) flex 容器。相當(dāng)于將屬性設(shè)置為flex: 0 0 auto吃溅。
<flex-grow>
定義 flex 項(xiàng)目的 flex-grow溶诞。負(fù)值無效。省略時(shí)默認(rèn)值為 1决侈。 (初始值為 0)
<flex-shrink>
定義 flex 元素的 flex-shrink 螺垢。負(fù)值無效。省略時(shí)默認(rèn)值為1。 (初始值為 1)
- <flex-basis>
定義 flex 元素的 flex-basis屬性枉圃。若值為0功茴,則必須加上單位,以免被視作伸縮性孽亲。省略時(shí)默認(rèn)值為 0坎穿。(初始值為 auto)
<!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>
.container {
display: flex;
width: 500px;
height: 500px;
background-color: orange;
}
.item {
width: 120px;
height: 120px;
/* flex: flex-grow flex-shrink flex-basis */
/* none: 0 0 auto */
/* auto: 1 1 auto */
/* flex: 1 1; */
flex: 1 auto;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2我是coderwhy_why_hahahaha</div>
<div class="item item3">3</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</body>
</html>
修復(fù)justify-content
<!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>
.container {
display: flex;
flex-wrap: wrap;
width: 500px;
background-color: orange;
}
.item {
width: 110px;
height: 140px;
margin-right: 20px;
}
.item:nth-child(4n) {
margin-right: 0;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item1">1</div>
<div class="item item3">3</div>
<div class="item item1">1</div>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</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>
.container {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
width: 500px;
background-color: orange;
}
.item {
width: 110px;
height: 140px;
}
.container > span {
width: 110px;
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<!-- 列數(shù) - 2 -->
<span></span>
<span></span>
</div>
<script>
function getRandomColor() {
return `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${
Math.random() * 255
})`;
}
const itemEls = document.querySelectorAll(".item");
for (const item of itemEls) {
item.style.backgroundColor = getRandomColor();
}
</script>
</body>
</html>
內(nèi)容回顧
一、浮動(dòng)float
1.1.兩個(gè)案例
- 自己做一下
- 第二個(gè)邊框不要立馬掌握
1.2.清楚浮動(dòng)
- after偽元素
1.3.多種布局對(duì)比
- 標(biāo)準(zhǔn)流
- 定位
- 浮動(dòng)
二返劲、flex布局
2.1.認(rèn)識(shí)flex布局
2.2.flex布局重要的概念
- flex container
- flex item
- main axis/cross axis
- main start/main end /cross start/cross end
2.4.flex container中的屬性
- flex-direction
- flex-wrap
- flex-flow
- justify-content
- align-items
- align-content
2.4.flex item中的屬性
- order
- align-self
- flex-grow
- flex-shrinnk
- flex-basis
- flex
2.5.fle布局中justify-content最后一行布局問題
課后作業(yè)
一. 完成課堂所有的代碼
- 邊框可以不做
二. 說出為什么需要清除浮動(dòng)以及如何清除浮動(dòng)
三. 利用浮動(dòng)完成如下布局結(jié)構(gòu)(完成結(jié)構(gòu)即可)(選做)
四. 總結(jié)flex布局container和item的屬性以及作用(重要)
五. 自己找3個(gè)案例練習(xí)
- 案例一:其中用到元素定位
- 案例二:其中用到浮動(dòng)布局
- 案例三:其中用到flex布局