本文已收錄至我的Github倉庫**DayDayUP**:github.com/RobodLee/DayDayUP,歡迎Star
ef="https://blog.csdn.net/weixin_43461520/article/details/113785278" />
時間:2023-07-05 13:36:01 | 來源:網(wǎng)站運營
時間:2023-07-05 13:36:01 來源:網(wǎng)站運營
手摸手帶你學移動端WEB開發(fā):? 「好好學習,天天向上」
本文已收錄至我的Github倉庫**DayDayUP**:github.com/RobodLee/DayDayUP,歡迎Star
ef="https://blog.csdn.net/weixin_43461520/article/details/113785278">?????轉載請注明出處!?????
鏈接:https://blog.csdn.net/weixin_43461520/article/details/113785278
?????**轉載請注明出處!**?????
?
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=0,minimal-ui">
其中,**name=“viewport”**表示這是一個視口標簽,「content」用于聲明某些屬性,常用屬性有 屬性 | 說明 |
---|---|
「width」 | 用于設置寬度,「device-width」 |
「initial-scale」 | 初始化縮放比,大于0的數(shù)字 |
「maximum-scale」 | 最大縮放比,大于0的數(shù)字 |
「minimum-scale」 | 最小縮放比,大于0的數(shù)字 |
「user-scalable」 | 用戶是否可以縮放,「yes」或「no」(1或0) |
「minimal-ui」 | 在網(wǎng)頁加載是隱藏頂部的地址欄和底部的導航欄 |
img { width: 50px; height: 50px;}
/* 語法: */background-size: 背景圖片寬度 背景圖片高度;/* 可以只寫一個參數(shù),一個參數(shù)表示的是寬度,高度省略了,會等比例縮放 */background-size: 背景圖片寬度;<br/>/* 參數(shù)百分比也是ok的,表示父盒子的百分之多少,同樣,百分比也可以指定一個或兩個 */background-size: 66% 50%; / background-size: 80%;<br/>/* cover表示背景圖片寬度和高度等比例擴展至足夠大,完全覆蓋背景區(qū)域,可能會導致一部分圖片內(nèi)容丟失 */background-size: cover;<br/>/* contain表示寬度和高度進行等比例縮放,當寬度或者高度中的一個占滿整個盒子的時候就不再拉伸了 */background-size: contain;
看一下「cover」和「contain」的區(qū)別 <head> <style> div { width: 300px; height: 400px; border: 5px solid red; background: url(./images/robod.png) no-repeat; background-size: contain; } </style></head><body> <div></div></body>
<head> <style> div { width: 80%; /* 將寬度指定為父盒子的80% */ height: 50px; /* 高度還是指定具體的px */ background-color: skyblue; margin: 0 auto; /* 盒子始終居中對齊 */ } </style></head><body> <div></div></body>
? 「flex 布局就是通過給父盒子添加flex屬性,來控制子元素的位置和排列方式」
?
flex-direction
(設置主軸的方向)屬性值 | 說明 |
---|---|
「row」 | x軸為主軸,子元素從左向右排列,默認值 |
row-reverse | x軸為主軸,子元素從右向左排列 |
「column」 | y軸為主軸,子元素從上到下排列 |
column-reverse | y軸為主軸,子元素從下到上排列 |
<head> <style> .father { display: flex; /* 使用flex布局 */ width: 500px; height: 500px; background-color: tomato; /* row、row-reverse、column、column-reverse */ flex-direction: column-reverse; } .son { width: 200px; height: 200px; margin: 10px; background-color: skyblue; } </style></head><body> <div class="father"> <div class="son">11111微信公眾號:Robod</div> <div class="son">22222微信公眾號:Robod</div> </div></body>
justify-content
(設置主軸上的子元素排列方式)屬性值 | 說明 |
---|---|
flex-start | 默認值,從頭部開始(如果主軸是x軸,則從左到右) |
felx-end | 從尾部開始排列 |
center | 在主軸居中對齊(如果主軸是x軸,則水平居中) |
space-around | 平分剩余空間 |
space-between | 先兩邊貼邊,再平分剩余空間 |
<head> <style> .box { width: 500px; height: 200px; display: flex; /* flex-start、flex-end、center、space-around、space-between */ justify-content: flex-start; background-color: aquamarine; } .box div { width: 100px; height: 100px; margin: 10px; background-color: blanchedalmond; } </style></head><body> <div class="box"> <div>Robod111</div> <div>Robod222</div> <div>Robod333</div> </div></body>
flex-wrap
(設置子元素是否換行)flex-wrap
屬性來設置「nowrap」(默認值,不換行),「wrap」(換行)。<head> <style> .box { width: 700px; height: 300px; display: flex; flex-wrap: nowrap; /* nowrap、wrap */ background-color: aquamarine; } .box div { width: 300px; height: 100px; border: 1px solid red; background-color: blanchedalmond; } </style></head><body> <div class="box"> <div>Robod111</div> <div>Robod222</div> <div>Robod333</div> </div></body>
align-items
(設置側軸上的子元素的排列方式,單行)屬性值 | 說明 |
---|---|
flex-start | 從上到下 |
flex-end | 從下到上 |
center | 擠在一起居中(垂直居中) |
stretch | 拉伸,默認值,子盒子不設置height時,高度和父盒子一致 |
<!-- 現(xiàn)在主軸是x軸,y軸是側軸--><head> <style> .box { width: 400px; height: 100px; display: flex; /* flex-start、flex-end、center、stretch */ align-items: stretch; background-color: aquamarine; } .box div { width: 100px; border: 1px solid red; background-color: blanchedalmond; } </style></head><body> <div class="box"> <div>Robod111</div> <div>Robod222</div> <div>Robod333</div> </div></body>
align-content
(設置側軸上的子元素的排列方式,多行)屬性值 | 說明 |
---|---|
flex-start | 在側軸的頭部開始排列,默認值 |
flex-end | 在側軸的尾部開始排列 |
center | 在側軸中間顯示 |
space-around | 子元素在側軸平分剩余空間 |
space-between | 子元素在側軸先分布在兩頭,再平分剩余空間 |
stretch | 子元素未指定height屬性時平分父元素高度 |
<head> <style> .box { width: 150px; height: 400px; display: flex; flex-wrap: wrap; /* 設置子元素換行顯示 */ /* flex-start、flex-end、center、space-around、space-between、stretch*/ align-content: stretch; background-color: aquamarine; } .box div { width: 100px; /* height: 100px; */ border: 1px solid red; background-color: blanchedalmond; } </style></head><body> <div class="box"> <div>Robod111</div> <div>Robod222</div> <div>Robod333</div> </div></body>
flex-flow
flex-flow: row wrap;
flex
<head> <style> .box { display: flex; width: 100%; height: 100px; background-color: skyblue; } .box div { height: 100px; } .box div:nth-child(1) { background-color: red; flex: 3; } .box div:nth-child(2) { background-color: green; flex: 1; } .box div:nth-child(3) { background-color: blue; width: 100px; } </style></head><body> <div class="box"> <div></div> <div></div> <div></div> </div></body>
上面這個例子中,第三個藍色盒子占了100px,紅色和綠色盒子分配剩下的空間,紅色的占2/3,綠色的占1/3,而且會根據(jù)父盒子寬度的調(diào)整去動態(tài)調(diào)整盒子的寬度。align-self
order
<head> <style> .box { display: flex; width: 200px; height: 200px; background-color: skyblue; } .box div { width: 50px; height: 50px; margin: 10px; background-color: burlywood; } .box div:nth-child(2){ align-self: flex-end; } .box div:nth-child(3) { order: -1; } </style></head><body> <div class="box"> <div>1</div> <div>2</div> <div>3</div> </div></body>
@media mediatype and|not|only (media feature) { CSS樣式;}
其中,mediatype
是媒體類型,常用的有「all」(用于所有設備),「print」(用于打印機和打印預覽)和「screen」(用于電腦屏幕,平板電腦,智能手機等)三種。關鍵字
,同樣也有三個,「and」 指的是可以將多個媒體特性連接到一起,「not」 是排除某個媒體類型,最后一個「only」用于指定某個特定的媒體類型。<head> <style> @media screen and (min-width: 600px) { html { font-size: 20px; background-color: skyblue; } } @media screen and (max-width: 599px) { html { background-color:springgreen; font-size: 10px; } } div { font-size: 2rem; } </style></head><body> <div> 微信公眾號:Robod </div></body>
上面的例子中,當屏幕寬度小于600px時,1rem=10px,文字大小是2rem=40px,當屏幕寬度為600~800px時,1rem=20px,文字大小就變成了2rem=40px,不過在書寫的時候要注意書寫的邏輯,不然有可能由于CSS層疊性的問題導致樣式不生效。<link rel="stylesheet" media="mediatype and|not|only (media fuature)" href="xxx.css">
這樣當符合media屬性中的條件時,就會引入該CSS文件。設備劃分 | 尺寸與寬度設置 |
---|---|
超小屏幕(手機) | <768px;寬度設為100% |
小屏設備(平板) | >=768px,<992px;寬度設為750px |
中等設備(桌面顯示器) | >=992px,<1200px;寬度設為970px |
寬屏設備(大桌面顯示器) | >=1200px;寬度設為1170px |
<head> <style> .container { width: 750px; margin: 0 auto; } .container ul li { float: left; width: 25%; height: 30px; background-color: aqua; list-style: none; } @media screen and (max-width: 768px) { .container { width: 100%; } .container ul li { width: 50%; background-color: skyblue; } } </style></head><body> <div class="container"> <ul> <li>微信公眾號</li> <li>Robod</li> <li>微信公眾號</li> <li>Robod</li> </ul> </div></body>
在 這個例子中,當窗口寬度大于768px時,讓4個 li 浮動在一行顯示,每個 li 的寬度為ul的25%,當窗口寬度小于768px時,通過媒體查詢修改 li 元素的樣式,一行顯示兩個。所以,響應式布局的優(yōu)點就在于可以使用一套CSS代碼實現(xiàn)不同屏幕下的布局,不過缺點就是會導致整個布局文件比較臃腫。/* 第一個參數(shù)是起始方向,不寫默認是top,跟一個表示某一邊,兩個表示某個角,比如左下角,就寫left bottom(中間用空格隔開,不是逗號);后面的顏色可以跟多個,中間用逗號隔開 */background: -webkit-linear-gradient(起始方向, 顏色1, 顏色2...);
需要注意的是,背景漸變的兼容性不是很好,所以必須「添加私有前綴」。<head> <style> div { width: 200px; height: 100px; background: -webkit-linear-gradient(left bottom,red,green,blue); } </style></head><body> <div></div></body>
碼字不易,可以的話,給我來個點贊
,收藏
,關注
如果你喜歡我的文章,歡迎關注微信公眾號 「『 R o b o d 』」
本文已收錄至我的Github倉庫「DayDayUP」:github.com/RobodLee/DayDayUP,歡迎Star
ef="https://blog.csdn.net/weixin_43461520/article/details/113785278">?????轉載請注明出處!?????
鏈接:https://blog.csdn.net/weixin_43461520/article/details/113785278
?????**轉載請注明出處!**?????
關鍵詞:移動
微信公眾號
版權所有? 億企邦 1997-2025 保留一切法律許可權利。