學(xué)習(xí)來源:尚硅谷前端html+css零基礎(chǔ)教程,2023最新前端開發(fā)html5+css3視頻
系列筆記:
- 【HTML4】(一)前端簡介
- 【HTML4】(二)各種各樣的常用標(biāo)簽
- 【HTML4】(三)表單及HTML4收尾
- 【CSS2】(四)CSS基礎(chǔ)及CSS選擇器
- 【CSS2】(五)CSS三大特性及常用屬性
- 【CSS2】(六)CSS盒子模型
- 【CSS2】(七)浮動
- 【CSS2】( 八)定位與布局
- 【實操】( 九)尚品匯實操練習(xí)
- 【HTML5】( 十)HTML5簡介及相關(guān)新增屬性
- 【CSS3】( 十一)CSS3簡介及基本語法(上)| 相關(guān)新增屬性
- 【CSS3】( 十二)CSS3簡介及基本語法(中)| 變換、過渡與動畫
- 【CSS3】 (十三)CSS3簡介及基本語法(下)| 伸縮盒模型
??前文回顧:前端 | ( 十二)CSS3簡介及基本語法(中)| 變換、過渡與動畫 | 尚硅谷前端html+css零基礎(chǔ)教程2023最新
??前文對應(yīng)p178-p183
,本文對應(yīng)p183-p200
??補充網(wǎng)站:W3school,MDN
??伸縮盒模型
??伸縮盒模型簡介
??伸縮容器、伸縮項目
-
主軸
: 伸縮項目沿著主軸排列,主軸默認(rèn)是水平的,默認(rèn)方向是:從左到右(左邊是起點,右邊是終點)。 -
側(cè)軸
: 與主軸垂直的就是側(cè)軸,側(cè)軸默認(rèn)是垂直的,默認(rèn)方向是:從上到下(上邊是起點,下邊是終點)。
??主軸方向
??主軸換行方式
??flex-flow
??主軸對齊方式
??側(cè)軸對齊方式
-
一行的情況:
-
多行的情況:
??flex實現(xiàn)水平垂直居中
方法一:父容器開啟 flex 布局,隨后使用 justify-content
和 align-items
實現(xiàn)水平垂直居中
.outer {
width: 400px;
height: 400px;
background-color: #888;
display: flex;
justify-content: center;
align-items: center;
}
.inner {
width: 100px;
height: 100px;
background-color: orange;
}
方法二:父容器開啟 flex 布局,隨后子元素 margin: auto
.outer {
width: 400px;
height: 400px;
background-color: #888;
display: flex;
}
.inner {
width: 100px;
height: 100px;
background-color: orange;
margin: auto;
}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>元素水平垂直居中</title>
<style>
.outer {
width: 400px;
height: 400px;
background-color: #888;
display: flex;
/* 方案一 */
/* justify-content: center; */
/* align-items: center; */
}
.inner {
width: 100px;
height: 100px;
background-color: orange;
/* 方案二 */
margin: auto;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
</html>
??伸縮性
.inner {
/* 設(shè)置伸縮項目在主軸上的基準(zhǔn)長度,若主軸是橫向的寬失效,若主軸是縱向的高失效 */
flex-basis: 300px;
}
伸
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>伸縮項目_伸</title>
<style>
.outer {
width: 1000px;
height: 900px;
background-color: #888;
margin: 0 auto;
/* 伸縮盒模型相關(guān)屬性-start */
/* 將該元素變?yōu)榱松炜s容器(開啟了flex布局) */
display: flex;
/* 調(diào)整主軸方向,水平從左到右,默認(rèn) */
flex-direction: row;
/* 主軸換行方式,換行 */
flex-wrap: wrap;
/* 主軸的對齊方式,主軸的起始位置 */
justify-content: flex-start;
}
.inner {
width: 200px;
height: 200px;
background-color: skyblue;
border: 1px solid black;
box-sizing: border-box;
flex-grow: 0;
}
/* 瓜分比例:1/6 */
.inner1 {
flex-grow: 1;
}
/* 1/3 */
.inner2 {
flex-grow: 2;
width: 300px;
}
/* 1/2 */
.inner3 {
flex-grow: 3;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner inner1">1</div>
<div class="inner inner2">2</div>
<div class="inner inner3">3</div>
</div>
</body>
</html>
縮
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>伸縮項目_縮</title>
<style>
.outer {
width: 400px;
height: 900px;
background-color: #888;
margin: 0 auto;
/* 伸縮盒模型相關(guān)屬性-start */
/* 將該元素變?yōu)榱松炜s容器(開啟了flex布局) */
display: flex;
/* 調(diào)整主軸方向,水平從左到右,默認(rèn) */
flex-direction: row;
/* 主軸換行方式,換行 */
/* 想縮就別說這玩意,不然直接就換行處理了 */
/* flex-wrap: wrap; */
/* 主軸的對齊方式,主軸的起始位置 */
justify-content: flex-start;
/* 側(cè)軸的對齊方式 */
align-content: flex-start;
}
.inner {
width: 200px;
height: 200px;
background-color: skyblue;
/* border: 1px solid black; */
/* box-sizing: border-box; */
flex-grow: 1;
}
.inner1 {
flex-shrink: 1;
}
.inner2 {
flex-shrink: 2;
width: 300px;
}
.inner3 {
flex-shrink: 3;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner inner1">
<!-- 縮的下限就是保證內(nèi)容的呈現(xiàn) -->
<div style="width: 50px;height:50px;background-color: green;">1</div>
</div>
<div class="inner inner2">2</div>
<div class="inner inner3">3</div>
</div>
</body>
</html>
帶上邊框,瀏覽器計算的時候會有一些
誤差
。實際應(yīng)用shrink默認(rèn)就是1,就不寫了。簡化!
??flex復(fù)合屬性
??項目排序和單獨對齊(了解)
-
order
屬性定義項目的排列順序。數(shù)值越小,排列越靠前,默認(rèn)為 0 。 - 通過
align-self
屬性,可以單獨調(diào)整某個伸縮項目的對齊方式 - 默認(rèn)值為
auto
,表示繼承父元素的align-items
屬性。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>項目排序與單獨對齊</title>
<style>
.outer {
width: 600px;
height: 900px;
background-color: #888;
margin: 0 auto;
/* 伸縮盒模型相關(guān)屬性-start */
/* 將該元素變?yōu)榱松炜s容器(開啟了flex布局) */
display: flex;
/* 調(diào)整主軸方向,水平從左到右,默認(rèn) */
flex-direction: row;
/* 主軸換行方式,換行 */
/* flex-wrap: wrap; */
/* 主軸的對齊方式,主軸的起始位置 */
justify-content: flex-start;
/* 側(cè)軸的對齊方式 */
align-content: flex-start;
}
.inner {
width: 200px;
height: 200px;
background-color: skyblue;
border: 1px solid black;
box-sizing: border-box;
/* 可以拉伸 可以壓縮 設(shè)置基準(zhǔn)長度為0,可簡寫為:flex:1 */
flex: 1 1 0;
}
.inner1 {
order: 3;
}
.inner2 {
order: 2;
}
.inner3 {
order: 1;
}
.inner2 {
align-self: center;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner inner1">1</div>
<div class="inner inner2">2</div>
<div class="inner inner3">3</div>
</div>
</body>
</html>
??案例
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>尚硅谷官網(wǎng)</title>
<style>
* {
font-family: Arial;
font-size: 14px;
margin: 0;
padding: 0;
border: none;
}
a { text-decoration: none; }
ul { list-style: none; }
html,body {
width: 100%;
height: 100%;
}
body {
/* 背景鋪滿 */
background-image: url('../images/bg.jpg');
/* 背景圖不重復(fù) */
background-repeat: no-repeat;
/* 當(dāng)圖片和背景不適配時的最優(yōu)解 */
background-size: cover;
}
.page-header {
height: 70px;
/* 需要設(shè)置透明度 */
background-color: rgba(0, 0, 0, 0.7);
/* 設(shè)置伸縮盒子 */
display: flex;
/* 主軸兩邊定格 */
justify-content: space-between;
/* 側(cè)軸對齊方式 */
align-items: center;
padding: 0 20px;
}
.header-nav {
display: flex;
}
.header-nav li a {
color: white;
font-size: 20px;
border: 1px solid white;
/* 圓角8px */
border-radius: 8px;
padding: 10px;
margin-right: 20px;
}
.header-nav li:last-child a {
margin-right: 0;
}
.page-content {
display: flex;
/* calc進行數(shù)值計算 */
height: calc(100vh - 70px);
}
.content-nav {
width: 1000px;
height: 300px;
/* 垂直居中 */
margin: auto;
display: flex;
/* 主軸對齊方式:均分 */
justify-content: space-evenly;
/* 側(cè)軸 */
align-items: center;
}
.content-nav .item {
width: 180px;
height: 200px;
background-color: orange;
display: flex;
/* 轉(zhuǎn)換主軸 */
flex-direction: column;
/* 側(cè)軸 */
align-items: center;
/* 主軸均分 */
justify-content: space-evenly;
transition: 0.2s linear;
cursor: pointer;
}
.content-nav .item:hover {
/* 邊框陰影 */
box-shadow: 0px 0px 20px black;
}
.content-nav .item span {
font-size: 20px;
color: white;
}
.content-nav .item:nth-child(1) {
background-color:#595CA8;
}
.content-nav .item:nth-child(2) {
background-color:#FF9D2E;
}
.content-nav .item:nth-child(3) {
background-color:#01A6DE;
}
.content-nav .item:nth-child(4) {
background-color:#015E91;
}
.content-nav .item:nth-child(5) {
background-color:#1DC128;
}
</style>
</head>
<body>
<!-- 頭部 -->
<header class="page-header">
<a href="#">
<img src="../images/logo.png" alt="logo">
</a>
<ul class="header-nav">
<li><a href="#">國內(nèi)校區(qū)</a></li>
<li><a href="#">澳洲校區(qū)</a></li>
<li><a href="#">英國校區(qū)</a></li>
<li><a href="#">美國校區(qū)</a></li>
</ul>
</header>
<!-- 內(nèi)容區(qū) -->
<div class="page-content">
<div class="content-nav">
<div class="item">
<img src="../images/item1.png" alt="">
<span>我的郵箱</span>
</div>
<div class="item">
<img src="../images/item2.png" alt="">
<span>云服務(wù)</span>
</div>
<div class="item">
<img src="../images/item3.png" alt="">
<span>手機課堂</span>
</div>
<div class="item">
<img src="../images/item4.png" alt="">
<span>微信服務(wù)</span>
</div>
<div class="item">
<img src="../images/item5.png" alt="">
<span>在線客服</span>
</div>
</div>
</div>
</body>
</html>
??相應(yīng)式布局
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>01_媒體查詢_媒體類型</title>
<style>
h1 {
width: 600px;
height: 400px;
line-height: 400px;
background-image: linear-gradient(30deg,red,yellow,green);
margin: 0 auto;
text-align: center;
font-size: 100px;
color: white;
text-shadow: 0 0 10px black;
}
/* 只有在打印機或打印預(yù)覽才應(yīng)用的樣式 */
@media print {
h1 {
background: transparent;
}
}
/* 只有在屏幕上才應(yīng)用的樣式 */
@media screen {
h1 {
font-family: "翩翩體-簡";
}
}
/* 一直都應(yīng)用的樣式 */
@media all {
h1 {
color: red;
}
}
</style>
</head>
<body>
<h1>新年快樂</h1>
</body>
</html>
打印視圖
完整列表參考
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>媒體查詢_媒體特性</title>
<style>
* {
margin: 0;
padding: 0;
}
h1 {
height: 200px;
background-color: gray;
text-align: center;
line-height: 200px;
font-size: 100px;
}
/* 檢測到視口的寬度為800px時,應(yīng)用如下樣式 */
@media (width:800px) {
h1 {
background-color: green;
}
}
/* 檢測到視口的寬度小于等于700px時,應(yīng)用如下樣式 */
@media (max-width:700px) {
h1 {
background-color: orange;
}
}
/* 檢測到視口的寬度大于等于900px時,應(yīng)用如下樣式 */
@media (min-width:900px) {
h1 {
background-color: deepskyblue;
}
}
</style>
</head>
<body>
<h1>你好啊</h1>
</body>
</html>
??BFC
更加通俗的理解:文章來源:http://www.zghlxwxcb.cn/news/detail-603585.html
-
BFC是
Block Formatting Context (塊級格式上下文)
,可以理解成元素的一個“特異功能”。 - 該 “特異功能”,在默認(rèn)的情況下處于關(guān)閉狀態(tài);當(dāng)元素滿足了某些條件后,該“特異功能”被激活。
-
所謂激活“特異功能”,專業(yè)點說就是:該元素創(chuàng)建了 BFC (又稱:開啟了 BFC )。
- 元素開啟 BFC 后,其子元素不會再產(chǎn)生 margin 塌陷問題。
- 元素開啟 BFC 后,自己不會被其他浮動元素所覆蓋。
- 元素開啟 BFC 后,就算其子元素浮動,元素自身高度也不會塌陷。
文章來源地址http://www.zghlxwxcb.cn/news/detail-603585.html
到了這里,關(guān)于前端 | ( 十三)CSS3簡介及基本語法(下)| 伸縮盒模型 | 尚硅谷前端html+css零基礎(chǔ)教程2023最新的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!