
批改狀態(tài):合格
老師批語:一旦習(xí)慣了flex, 傳統(tǒng)的float+position都不想碰了
1.flex單行容器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexbox彈性盒布局快速預(yù)覽</title>
<style>
.container{
width:800px;
/* 轉(zhuǎn)為flexbox彈性盒子 */
/* 如果這個(gè)容器中的內(nèi)容要使用彈性盒子布局,首先需要將這個(gè)容器轉(zhuǎn)化為flex彈性盒子 */
display:flex;
}
/* 只在一行顯示flex:auto可以自動(dòng)平均彈性項(xiàng)目大小 */
.container>.item{
/* 一但將父元素轉(zhuǎn)換為彈性容器,容器內(nèi)的子元素(子元素的子元素不行)就會(huì)自動(dòng)成為彈性項(xiàng)目 */
/* 不管這個(gè)項(xiàng)目標(biāo)簽之前是什么類型,統(tǒng)統(tǒng)轉(zhuǎn)為行內(nèi)塊 */
/* 行內(nèi)塊是一行內(nèi)顯示可以設(shè)置寬高 */
flex:auto;
/* width:60px;
height:66px; */
}
</style>
</head>
<body>
<!-- 快速實(shí)現(xiàn).container>.item{$}*3 -->
<div class="container">
<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">6</div>
</div>
</body>
</html>
2.flex多行容器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexbox彈性盒多行容器</title>
<style>
.container{
width: 600px;
display:flex;
}
/* 多行的時(shí)候要計(jì)算彈性容器的寬度和彈性項(xiàng)目的寬度,留下剩余空間不好看 */
.container>.item{
flex:auto;
width:150px;
}
.container{
flex-wrap:wrap;
}
</style>
</head>
<body>
<!-- 快速實(shí)現(xiàn).container>.item{$}*3 -->
<div class="container">
<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">6</div>
</div>
</body>
</html>
3.flex單行容器項(xiàng)目對(duì)齊
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>單行容器的項(xiàng)目對(duì)齊</title>
<style>
.container{
width:300px;
display:flex;
/* justify-content:設(shè)置容器中的剩余空間在所有項(xiàng)目之間的分配方案 */
/* 1.容器剩余空間在所有項(xiàng)目兩邊如何分配,將所有項(xiàng)目視為一個(gè)整體 */
/* 開頭對(duì)齊 */
justify-content:flex-start;
/* 不建議向下面兩種方式寫 */
/* justify-content:start; */
/* justify-content:left; */
/* 末尾對(duì)齊 */
justify-content:flex-end;
/* 中間對(duì)齊 */
justify-content:center;
/* 2.容器剩余空間在所有項(xiàng)目之間如何分配,將所有項(xiàng)目視為一個(gè)個(gè)的個(gè)體 */
/* 兩端對(duì)齊 */
justify-content:space-between;
/* 分散對(duì)齊 剩余空間在所有項(xiàng)目的兩側(cè)對(duì)齊*/
justify-content:space-around;
/* 平均分配 */
justify-content:space-evenly;
}
.item{
width:60px;
}
</style>
</head>
<body>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
</body>
</html>
4.flex多行容器項(xiàng)目水平對(duì)齊
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>多行容器的項(xiàng)目對(duì)齊主軸為水平時(shí)</title>
<style>
.container{
width:300px;
display:flex;
flex-wrap:wrap;
/* 多行容器要給一下高度 */
height:150px;
/* align-content:設(shè)置多行容器中項(xiàng)目的排列方式 */
/* stretch默認(rèn)值 */
align-content:stretch;
/* 1.容器剩余空間在所有項(xiàng)目兩邊如何分配,將所有項(xiàng)目視為一個(gè)整體 */
/* 開頭對(duì)齊 */
align-content:flex-start;
/* 末尾對(duì)齊 */
align-content:flex-end;
/* 中間對(duì)齊 */
align-content:center;
/* 2.容器剩余空間在所有項(xiàng)目之間如何分配,將所有項(xiàng)目視為一個(gè)個(gè)的個(gè)體 */
/* 兩端對(duì)齊 */
align-content:space-between;
/* 分散對(duì)齊 剩余空間在所有項(xiàng)目的兩側(cè)對(duì)齊*/
align-content:space-around;
/* 平均分配 */
align-content:space-evenly;
}
.item{
width:60px;
}
</style>
</head>
<body>
<div class="container">
<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">6</div>
<div class="item">7</div>
<div class="item">8</div>
</div>
</body>
</html>
5.flex多行容器垂直對(duì)齊
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>多行容器的項(xiàng)目對(duì)齊主軸為垂直時(shí)</title>
<style>
.container{
width:300px;
height:150px;
display:flex;
flex-direction:row;
flex-direction:column;
/* 項(xiàng)目兩邊分配 */
justify-content:flex-start;
justify-content:flex-end;
justify-content:center;
/* 項(xiàng)目之間對(duì)齊 */
justify-content:space-between;
justify-content:space-around;
justify-content:space-evenly;
}
</style>
</head>
<body>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
</body>
</html>
6.flex項(xiàng)目在交叉軸排列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>項(xiàng)目在交叉軸排列</title>
<style>
.container{
width:300px;
display:flex;
height:200px;
/* 默認(rèn)項(xiàng)目時(shí)在交叉軸自動(dòng)拉伸的 */
align-items:stretch;
align-items:flex-start;
align-items:flex-end;
align-items:center;
}
.item{
width:60px;
}
</style>
</head>
<body>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
</body>
</html>
7.flex主軸方向與項(xiàng)目排列的簡寫
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>主軸方向與項(xiàng)目排列的簡寫</title>
<style>
.container{
width:300px;
display:flex;
height:50px;
/* 默認(rèn)值就不用寫出來了
flex-direction:row;
flex-wrap:nowrap;
這一行代碼代替上面兩行
flex-flow:row nowrap; */
flex-flow:column wrap;
}
.item{
width:60px;
}
</style>
</head>
<body>
<div class="container">
<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>
</body>
</html>
8.用felx快速寫一個(gè)主導(dǎo)航
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>flex彈性容器快速擼一個(gè)主導(dǎo)航</title>
<style>
*{
margin:0;
padding:0;
box-sizing:border-box;
}
a{
text-decoration:none;
color:#ccc;
}
nav{
height:40px;
background-color:#333;
padding:0 50px;
display:flex;
}
nav a{
height:inherit;
line-height:40px;
padding:0 15px;
}
nav a:hover{
background-color:lightslategray;
color:white;
}
nav a:last-of-type{
margin-left:auto;
}
</style>
</head>
<body>
<header>
<nav>
<a href="">首頁</a>
<a href="">教學(xué)視頻</a>
<a href="">社區(qū)問答</a>
<a href="">資源下載</a>
<a href>登錄/注冊(cè)</a>
</nav>
</header>
</body>
</html>
效果展示:
9.flex項(xiàng)目屬性order控制項(xiàng)目順序
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>項(xiàng)目屬性-order控制項(xiàng)目順序</title>
<style>
.container{
width:300px;
display:flex;
}
.container>.item{
width:60px;
}
.container>.item:first-of-type{
/* order默認(rèn)值是0 */
order:3;
}
.container>.item:last-of-type{
order:5;
}
</style>
</head>
<body>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
</body>
</html>
10.order實(shí)例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>order小案列,用戶調(diào)整元素順序</title>
<style>
.container{
width:300px;
display:flex;
flex-direction:column;
}
.container>.item{
border:1px solid #000;
padding:10px;
display:flex;
position:relative;
}
.container>.item>div{
display:flex;
}
</style>
</head>
<body>
<div class="container">
<div class="item">imag-1<div><button onclick="up(this)">向上</button><button onclick="down(this)">向下</button></div></div>
<div class="item">imag-2<div><button onclick="up(this)">向上</button><button onclick="down(this)">向下</button></div></div>
<div class="item">imag-3<div><button onclick="up(this)">向上</button><button onclick="down(this)">向下</button></div></div>
</div>
</body>
<script>
let up=(ele)=>(ele.offsetParent.style.order-=1);
let down=(ele)=>(ele.offsetParent.style.order+=1);
</script>
</html>
效果展示:
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)