
批改狀態(tài):合格
老師批語(yǔ):看來(lái)你是看了不少資料
用來(lái)定義一個(gè) flex 容器。如果設(shè)置為 flex 則容器呈現(xiàn)為塊狀元素,設(shè)置為inline- flex 則容器呈現(xiàn)為行內(nèi)元素。
CSS 代碼:
.container {
display: flex; /* or inline-flex */
}
flex-direction 屬性確立了主軸,從而定義了 flex 項(xiàng)在 flex 容器中的排布方向。 Flexbox 是單向布局,有些時(shí)候我們也稱作一維布局。 可以將 flex 項(xiàng)視為主要沿著水平行或垂直列排布。
CSS 代碼:
.container {
flex-direction: row | row-reverse | column | column-reverse;
}
默認(rèn)情況下,flex 項(xiàng)會(huì)盡可能地嘗試排在同一行上(行或列),通過(guò)設(shè)置 flex-wrap 來(lái)決定 flex 項(xiàng)是否允需要換行。
CSS 代碼:
.container{
flex-wrap: nowrap | wrap | wrap-reverse;
}
nowrap (默認(rèn)值) : 不換行
wrap: 換行
wrap-reverse: flex 項(xiàng)將 從下到上 根據(jù)實(shí)際情況排布再多行上折行。
這是 flex-direction 和 flex-wrap 屬性的縮寫形式。同時(shí)定義 flex 容器的主軸和交叉軸。默認(rèn)是 row nowrap
CSS 代碼:
flex-flow: <‘flex-direction’> || <‘flex-wrap’>
justify-content 屬性定義了flex 項(xiàng)沿主軸方向的對(duì)齊方式。
CSS 代碼:
.container {
justify-content: flex-start | flex-end | center | space-between | space-around;
}
flex-start(默認(rèn)值):左對(duì)齊
flex-end:右對(duì)齊
center: 居中
space-between:兩端對(duì)齊,項(xiàng)目之間的間隔都相等。
space-around:每個(gè)項(xiàng)目?jī)蓚?cè)的間隔相等。所以,項(xiàng)目之間的間隔比項(xiàng)目與邊框的間隔大一倍。
align-items 定義了 flex 項(xiàng)如何沿當(dāng)前行在交叉軸上排布的默認(rèn)行為??梢詫⑵湟暈榻徊孑S(垂直于主軸)上的對(duì)齊方式。
CSS 代碼:
.container {
align-items: flex-start | flex-end | center | baseline | stretch;
}
flex-start:交叉軸的起點(diǎn)對(duì)齊。
flex-end:交叉軸的終點(diǎn)對(duì)齊。
center:交叉軸的中點(diǎn)對(duì)齊。
baseline: 項(xiàng)目的第一行文字的基線對(duì)齊。
stretch(默認(rèn)值):如果項(xiàng)目未設(shè)置高度或設(shè)為auto,將占滿整個(gè)容器的高度。
當(dāng)交叉軸上有剩余空間時(shí),align-content 可以設(shè)置 flex 容器中的 行 在交叉軸上如何分配剩余空間,類似于 justify-content 在主軸上對(duì)齊單個(gè) flex 項(xiàng)的方式。
CSS 代碼:
.container {
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}
flex-start:與交叉軸的起點(diǎn)對(duì)齊。
flex-end:與交叉軸的終點(diǎn)對(duì)齊。
center:與交叉軸的中點(diǎn)對(duì)齊。
space-between:與交叉軸兩端對(duì)齊,軸線之間的間隔平均分布。
space-around:每根軸線兩側(cè)的間隔都相等。所以,軸線之間的間隔比軸線與邊框的間隔大一倍。
stretch(默認(rèn)值):軸線占滿整個(gè)交叉軸。
默認(rèn)情況下,flex 項(xiàng)按源(HTML結(jié)構(gòu))順序排布。但是,order 屬性可以控制它們?cè)?/span> flex 容器中的顯示順序。
CSS 代碼:
.item {
order: <integer>; /* 默認(rèn)值是 0 */
}
order值越小排列越靠前
flex-grow 定義了 flex 項(xiàng)在有可用剩余空間時(shí)拉伸比例。
它接受的值作為比例,無(wú)單位。它規(guī)定了 flex 項(xiàng)應(yīng)該占 flex 容器中可用空間的比例。
如果所有 flex 項(xiàng)的 flex-grow 都設(shè)置為 1 ,則父容器中的剩余空間將平均分配給所有子項(xiàng)。 如果其中一個(gè)子項(xiàng)的值為 2 ,則該子項(xiàng)占用的剩余空間是其他子項(xiàng)的兩倍(或者至少會(huì)盡力獲得)。
CSS 代碼:
.item {
flex-grow: <number>; /* default 0 */
}
注:負(fù)值對(duì)于 flex-grow 無(wú)效。
比如我們得 flex 容器中有 6 個(gè) flex 項(xiàng),每個(gè) flex 項(xiàng)的 flex-grow 初始值都是 1。如果我們將每個(gè) flex 項(xiàng)的 flex-grow 相加起來(lái),總和為 6。因此 flex 容器的總寬度被平均分成了 6 份。每個(gè) flex 項(xiàng)增長(zhǎng)到填充容器可用空間的1/6。
當(dāng)我們將第 3 個(gè) flex 項(xiàng)的 flex-grow 設(shè)置為 2 時(shí),flex 容器現(xiàn)在被分成了 7 等份,因?yàn)樗?flex-grow 屬性是:1 + 1 + 2 + 1 + 1 + 1。第 3 個(gè) flex 項(xiàng)占了整個(gè)容器空間的 2/7,其他的占了 1/7。
flex-shrink 定義了 flex 項(xiàng)的收縮的能力。(注:與 flex-grow 拉伸正好相反,flex-shrink 決定 flex 項(xiàng)允許收縮多少比例。)
CSS 代碼:
.item {
flex-shrink: <number>; /* default 1 */
}
注:負(fù)值對(duì)于 flex-shrink 無(wú)效。
flex-basis 定義了在分配剩余空間之前 flex 項(xiàng)默認(rèn)的大小??梢栽O(shè)置為某個(gè)長(zhǎng)度值(e.g. 20%, 5rem,等)或者關(guān)鍵字。關(guān)鍵字 auto 意味著 flex 項(xiàng)會(huì)按照其本來(lái)的大小顯示
CSS 代碼:
.item {
flex-basis: <length> | auto; /* default auto */
}
如果設(shè)置為 0 , 則 flex 項(xiàng)內(nèi)容周圍的空隙不會(huì)根據(jù) flex-grow 按比例分配,如果設(shè)置為 auto,則 flex 項(xiàng)周圍額外的空襲會(huì)根據(jù) flex-grow 按照比例分配
flex 是 flex-grow、flex-shrink、flex-basis 三個(gè)屬性的縮寫。其中第二個(gè)和第三個(gè)參數(shù)(flex-shrink 和 flex-basis)是可選的。默認(rèn)值為0 1 auto。
CSS 代碼:
.item {
flex: none | [ < 'flex-grow'> < 'flex-shrink'>? || < 'flex-basis'> ]
}
推薦使用縮寫形式而不是單獨(dú)地設(shè)置每一個(gè)屬性,縮寫形式中會(huì)更加智能地計(jì)算出相關(guān)值。
align-self 屬性允許某個(gè)單獨(dú)的 flex 項(xiàng)覆蓋默認(rèn)的對(duì)齊方式
CSS 代碼:
.item {
align-self: auto | flex-start | flex-end | center | baseline | stretch;
}
title>PC端布局的通用解決方案</title>
<style>
/*初始化*/
* {
font-size: 16px;
padding: 0;
margin: 0;
box-sizing: border-box; /*不加內(nèi)邊距和邊框的寬度,默認(rèn)是content-box*/
}
body {
display: flex;
flex-flow: column nowrap; /*從上到下排列,不換行*/
}
nav {
display: flex;
justify-content: start;
align-items: center;
min-width: 80px;
background: #000;
}
nav > a:not(:first-of-type) {
width: 150px;
text-align: center;
color: #fff;
text-decoration: none;
height: 50px;
line-height: 50px;
margin-left: 40px;
}
nav > a:not(:first-of-type):hover {
background: rgba(87, 87, 87, 0.5);
color: rgb(241, 79, 79);
}
nav > a:last-of-type {
margin-left: auto;
}
nav > a > img {
height: 50px;
}
.container {
display: flex;
flex-flow: row nowrap;
justify-content: center;
align-items: center;
margin: 10px auto;
}
aside {
min-width: 200px;
min-height: 600px;
background: lightgoldenrodyellow;
}
main {
min-width: 600px;
min-height: 600px;
margin: 0 10px;
background: lightblue;
}
footer {
display: flex;
height: 80px;
background: #000;
justify-content: center;
align-items: center;
flex-flow: column nowrap;
}
footer > div {
display: flex;
justify-content: center;
}
footer > div:first-of-type a {
width: 100px;
font-size: 14px;
height: 20px;
}
a {
text-decoration: none;
color: rgb(184, 184, 184);
}
footer > div:last-of-type {
margin-top: 10px;
color: rgb(184, 184, 184);
}
footer > div:last-of-type span {
font-size: 14px;
margin: 0 20px;
}
</style>
</head>
<body>
<!-- 頁(yè)眉 -->
<header>
<nav>
<a href=""><img src="logo.png" alt="" /></a>
<a href="">首頁(yè)</a>
<a href="">電器城</a>
<a href="">醫(yī)藥館</a>
<a href="">營(yíng)業(yè)廳</a>
<a href="">登錄/注冊(cè)</a>
</nav>
</header>
<!-- 主體 -->
<div class="container">
<aside>左邊欄</aside>
<main>主體內(nèi)容</main>
<aside>右邊欄</aside>
</div>
<!-- 頁(yè)腳 -->
<footer>
<div>
<a href="">關(guān)于天貓</a>
<a href="">幫助中心</a>
<a href="">開(kāi)放平臺(tái)</a>
<a href="">誠(chéng)聘英才</a>
<a href="">聯(lián)系我們</a>
<a href="">網(wǎng)站合作</a>
<a href="">法律聲明</a>
<a href="">隱私權(quán)政策</a>
<a href="">知識(shí)產(chǎn)權(quán)</a>
<a href="">廉正舉報(bào)</a>
<a href="">規(guī)則意見(jiàn)征集</a>
</div>
<div>
<span>浙江省網(wǎng)絡(luò)食品銷售第三方平臺(tái)提供者備案:浙網(wǎng)食A33010002</span>
<span> 互聯(lián)網(wǎng)藥品交易服務(wù)資格證書:國(guó)A20150001</span>
</div>
</footer>
</body>
</html>
<title>移動(dòng)端布局</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
a {
text-decoration: none;
color: #666;
}
html {
width: 100vw; /*當(dāng)前可視區(qū)的寬度*/
/* vh:可視區(qū)的高度 */
height: 100vh;
font-size: 14px;
color: #666;
}
body {
min-width: 360px;
background: #fff;
display: flex;
flex-flow: column nowrap;
}
body > header {
color: #fff;
background: #333;
height: 30px;
display: flex;
align-items: center;
justify-content: space-between;
position: fixed;
width: 100vw;
padding: 0 15px;
}
/*輪播圖*/
body > .slider {
height: 180px;
}
body > .slider > img {
width: 100%;
}
/*導(dǎo)航區(qū)*/
nav {
height: 200px;
margin-bottom: 10px;
display: flex;
/*轉(zhuǎn)為多行容器*/
flex-flow: row wrap;
}
nav div {
width: 25vw;
display: flex;
flex-flow: column nowrap;
align-items: center;
}
nav > div > a:first-of-type {
text-align: center;
}
nav > div img {
width: 50%;
}
/* 熱銷商品 */
.hot-goods {
display: flex;
flex-flow: row wrap;
justify-content: space-around;
margin: 10vw 0;
}
.hot-goods > .goods-img {
display: flex;
flex-flow: column nowrap;
justify-content: center;
align-items: center;
}
.hot-goods .hot {
color: coral;
}
/* 商品列表 */
.shop {
display: flex;
flex-flow: column nowrap;
margin-top: 10vw;
font-size: small;
}
.shop > .goods-desc {
display: flex;
margin-bottom: 10vw;
}
.shop > .goods-desc img {
width: 20vw;
}
.shop > .goods-desc > a:first-of-type {
margin-left: 5vw;
}
.shop > .goods-desc > a:last-of-type {
margin: 0 10vw;
}
.shop > .goods-desc .hot {
color: coral;
}
footer {
display: flex;
justify-content: space-around;
height: 10vw;
background: rgb(231, 231, 231);
position: fixed;
bottom: 0;
width: 100vw;
}
footer > a {
display: flex;
flex-flow: column nowrap;
justify-content: center;
text-align: center;
}
footer > a > span:first-of-type {
color: coral;
}
footer > a > span:last-of-type {
color: #333;
}
</style>
</head>
<body>
<!-- 頁(yè)眉 -->
<header>
<a href="">LOGO</a>
<span class="iconfont"></span>
</header>
<!-- 輪播圖 -->
<div class="slider">
<img src="static/images/banner.jpg" alt="" />
</div>
<!-- 主導(dǎo)航區(qū) -->
<nav>
<div>
<a href=""><img src="static/images/link4.webp" alt="" /></a>
<a href="">優(yōu)惠劵</a>
</div>
<div>
<a href=""><img src="static/images/link1.webp" alt="" /></a>
<a href="">超市精選</a>
</div>
<div>
<a href=""><img src="static/images/link2.webp" alt="" /></a>
<a href="">服裝百貨</a>
</div>
<div>
<a href=""><img src="static/images/link3.webp" alt="" /></a>
<a href="">數(shù)碼精品</a>
</div>
<div>
<a href=""><img src="static/images/link4.webp" alt="" /></a>
<a href="">優(yōu)惠劵</a>
</div>
<div>
<a href=""><img src="static/images/link1.webp" alt="" /></a>
<a href="">超市精選</a>
</div>
<div>
<a href=""><img src="static/images/link2.webp" alt="" /></a>
<a href="">服裝百貨</a>
</div>
<div>
<a href=""><img src="static/images/link3.webp" alt="" /></a>
<a href="">數(shù)碼精品</a>
</div>
</nav>
<!-- 熱銷商品 -->
<h2>
熱銷商品<span class="iconfont hot" style="color: red;"></span>
</h2>
<div class="hot-goods">
<div class="goods-img">
<a href=""><img src="static/images/goods1.jpg" alt="" /></a>
<p>Apple iPhone 11 128g</p>
<div>
<span>6299 元</span>
<span class="iconfont hot"></span>
</div>
</div>
<div class="goods-img">
<a href=""><img src="static/images/goods2.jpg" alt="" /></a>
<p>華為筆記本電腦</p>
<div>
<span>8999 元</span>
<span class="iconfont hot"></span>
</div>
</div>
<div class="goods-img">
<a href=""><img src="static/images/goods1.jpg" alt="" /></a>
<p>Apple iPhone 11 128g</p>
<div>
<span>6299 元</span>
<span class="iconfont hot"></span>
</div>
</div>
<div class="goods-img">
<a href=""><img src="static/images/goods2.jpg" alt="" /></a>
<p>華為筆記本電腦</p>
<div>
<span>8999 元</span>
<span class="iconfont hot"></span>
</div>
</div>
<div class="goods-img">
<a href=""><img src="static/images/goods1.jpg" alt="" /></a>
<p>Apple iPhone 11 128g</p>
<div>
<span>6299 元</span>
<span class="iconfont hot"></span>
</div>
</div>
<div class="goods-img">
<a href=""><img src="static/images/goods2.jpg" alt="" /></a>
<p>華為筆記本電腦</p>
<div>
<span>8999 元</span>
<span class="iconfont hot"></span>
</div>
</div>
</div>
<!-- 商品列表 -->
<h2>
商品列表<span class="iconfont hot" style="color: red;"></span>
</h2>
<div class="shop">
<div class="goods-desc">
<a href=""><img src="static/images/p40.png" alt="" /></a>
<a href=""
>領(lǐng)劵200【12期免息+當(dāng)天發(fā)】HUAWEI/華為P40
5G手機(jī)官方旗艦店官網(wǎng)正品華為mate30pro/直降nova7se/p40pro+
<span class="iconfont hot" style="vertical-align: middle;"
></span
></a
>
</div>
<div class="goods-desc">
<a href=""><img src="static/images/i7.png" alt="" /></a>
<a href=""
>華為/HUAWEI MateBook X Pro2020款英特爾十代i7-10510U+16GB+512GB/1TBSSD獨(dú)顯筆記本
<span class="iconfont hot" style="vertical-align: middle;"
></span
>
</a>
</div>
<div class="goods-desc">
<a href=""><img src="static/images/p40.png" alt="" /></a>
<a href=""
>領(lǐng)劵200【12期免息+當(dāng)天發(fā)】HUAWEI/華為P40
5G手機(jī)官方旗艦店官網(wǎng)正品華為mate30pro/直降nova7se/p40pro+
<span class="iconfont hot" style="vertical-align: middle;"
></span
></a
>
</div>
<div class="goods-desc">
<a href=""><img src="static/images/i7.png" alt="" /></a>
<a href=""
>華為/HUAWEI MateBook X Pro2020款英特爾十代i7-10510U+16GB+512GB/1TBSSD獨(dú)顯筆記本
<span class="iconfont hot" style="vertical-align: middle;"
></span
>
</a>
</div>
<div class="goods-desc">
<a href=""><img src="static/images/p40.png" alt="" /></a>
<a href=""
>領(lǐng)劵200【12期免息+當(dāng)天發(fā)】HUAWEI/華為P40
5G手機(jī)官方旗艦店官網(wǎng)正品華為mate30pro/直降nova7se/p40pro+
<span class="iconfont hot" style="vertical-align: middle;"
></span
></a
>
</div>
<div class="goods-desc">
<a href=""><img src="static/images/i7.png" alt="" /></a>
<a href=""
>華為/HUAWEI MateBook X Pro2020款英特爾十代i7-10510U+16GB+512GB/1TBSSD獨(dú)顯筆記本
<span class="iconfont hot" style="vertical-align: middle;"
></span
>
</a>
</div>
<div class="goods-desc">
<a href=""><img src="static/images/p40.png" alt="" /></a>
<a href=""
>領(lǐng)劵200【12期免息+當(dāng)天發(fā)】HUAWEI/華為P40
5G手機(jī)官方旗艦店官網(wǎng)正品華為mate30pro/直降nova7se/p40pro+
<span class="iconfont hot" style="vertical-align: middle;"
></span
></a
>
</div>
<div class="goods-desc">
<a href=""><img src="static/images/i7.png" alt="" /></a>
<a href=""
>華為/HUAWEI MateBook X Pro2020款英特爾十代i7-10510U+16GB+512GB/1TBSSD獨(dú)顯筆記本
<span class="iconfont hot" style="vertical-align: middle;"
></span
>
</a>
</div>
</div>
<footer>
<a href="">
<span class="iconfont hot"></span>
<span>首頁(yè)</span>
</a>
<a href="">
<span class="iconfont hot"></span>
<span>分類</span>
</a>
<a href="">
<span class="iconfont hot"></span>
<span>購(gòu)物車</span>
</a>
<a href="">
<span class="iconfont hot"></span>
<span>未登錄</span>
</a>
</footer>
</body>
</html>
只有通過(guò)多練習(xí)才能知道自己到底會(huì)不會(huì),自己掌握了那些內(nèi)容,自己是怎么理解的,還有哪些內(nèi)容是自己不會(huì)的,從而幫助自己更快的學(xué)習(xí)。
微信掃碼
關(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)