1.實(shí)例演示相鄰選擇器與兄弟選擇器,并分析異同
#id + * {} 是相鄰選擇器, 指當(dāng)前元素后面緊挨著的相鄰元素會(huì)被選中.
#id ~ * {} 是兄弟選擇器,指當(dāng)前元素后面所有的元素會(huì)被選中.
實(shí)現(xiàn)效果如下:
實(shí)現(xiàn)代碼如下:
<style> div { width: 40px; height: 40px; background-color: red; border: 1px dashed black; display: inline-block; text-align: center; line-height: 40px; margin: 20px 4px; /* 設(shè)置DIV的外邊距 */ } ul { margin: 0; padding-left: 0; /* 取消左側(cè)圓點(diǎn) */ border: 0px dashed red; /* 邊框?yàn)樘摼€,紅色,1個(gè)像素 */ } ul li { list-style-type: none; width: 50px; height: 50px; background-color: wheat; text-align: center; line-height: 50px; /* 水平和垂直的居中 */ border-radius: 50%; display: inline-block; /* 將一個(gè)塊級(jí)元素轉(zhuǎn)為內(nèi)聯(lián)元素 */ box-shadow: 2px 2px 1px #888; } /* 相鄰選擇器 */ #bg-blue+* { background-color: yellow; } /* 兄弟選擇器 */ #green~* { background-color: green; } </style> </head> <body> <ul> <li>1</li> <li id=bg-blue>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ul> <div>1</div> <div id="green">2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> </body>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
2.實(shí)例演示:nth-child() 和 :nth-of-type()選擇器,并分析異同
偽類: 子元素選擇器:nth-child(n) 選擇器匹配屬于其父元素的第 N 個(gè)子元素,不論元素的類型。n 可以是數(shù)字、關(guān)鍵詞或公式。
實(shí)現(xiàn)效果如下:
偽類: 類型選擇器,:nth-of-type(n) 選擇器匹配屬于父元素的特定類型的第 N 個(gè)子元素的每個(gè)元素.n 可以是數(shù)字、關(guān)鍵詞或公式。
實(shí)現(xiàn)效果如下:
實(shí)現(xiàn)代碼如下:
<style> ul { margin: 0; padding-left: 0; /* 取消左側(cè)圓點(diǎn) */ border: 0px dashed red; /* 邊框?yàn)樘摼€,紅色,1個(gè)像素 */ } ul li { list-style-type: none; width: 50px; height: 50px; background-color: wheat; text-align: center; line-height: 50px; /* 水平和垂直的居中 */ border-radius: 50%; display: inline-block; /* 將一個(gè)塊級(jí)元素轉(zhuǎn)為內(nèi)聯(lián)元素 */ box-shadow: 2px 2px 1px #888; } /* 偽類:子元素選擇器 */ ul :first-child { background-color: coral; } /* 選擇第一個(gè)元素 */ ul :last-child { background-color: coral; } /* 選擇最后一個(gè)元素 */ ul :nth-child(2) { background-color: green; } /* 選擇第二個(gè)元素 */ ul :nth-last-child(2) { background-color: blue; } /* 選擇倒數(shù)第二個(gè)元素 */ ul li:first-of-type { background-color: darkorange; color: white; } ul li:last-of-type { background-color: darkorange; color: white; } ul li:nth-of-type(2) { background-color: darkgreen; color: white; </style> </head> <body> <ul> <li>1</li> <li id=bg-blue>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ul> </body>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
3. 實(shí)例演示:padding 對(duì)盒子大小的影響與解決方案, 使用寬度分離或box-sizing將圖片顯示在容器中間,演示內(nèi)邊距對(duì)盒子大小的影響與解決方案(使用寬度分離或box-sizing)
實(shí)現(xiàn)效果如下:
實(shí)現(xiàn)代碼如下:
<style> .box1 { width: 240px; border: 1px solid black; background-color: rgb(235, 53, 108); } .box1 { padding: 20px; width: 200px; } /* 使用寬度分離 */ .wrap { width: 242px; } .box2 { padding: 20px; background-color: lightsalmon; border: 1px solid black; } /* box-sizing */ .box3 { width: 242px; box-sizing: border-box; padding: 20px; background-color: pink; border: 1px solid black; } </style> </head> <body> <!-- 將圖片顯示在容器中間,演示內(nèi)邊距對(duì)盒子大小的影響與解決方案(使用寬度分離或box-sizing) --> <div class="box1"> <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1567562883900&di=df325af2d0000f36c7d42a6cf9792408&imgtype=0&src=http%3A%2F %2Fi.shangc.net%2F2017%2F0627%2F20170627125731351.jpg" alt="如花" width="200"> </div> <div class="wrap"> <div class="box2"> <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1567562883900&di=df325af2d0000f36c7d42a6cf9792408&imgtype=0&src=http%3A %2F%2Fi.shangc.net%2F2017%2F0627%2F20170627125731351.jpg" alt="如花" width="200"> </div> </div> <div class="box3"> <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1567562883900&di=df325af2d0000f36c7d42a6cf9792408&imgtype=0&src=http%3A%2F %2Fi.shangc.net%2F2017%2F0627%2F20170627125731351.jpg" alt="如花" width="200"> </div>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
4. 實(shí)例演示: margin中的同級(jí)塌陷, 嵌套傳遞與自動(dòng)擠壓, 并提出解決方案或應(yīng)用場(chǎng)景
(1)盒子外邊框同級(jí)塌陷效果 :同級(jí)塌陷,誰大向誰擠壓
(2) 父級(jí)盒子會(huì)隨內(nèi)部盒子一起移動(dòng),為了解決,讓內(nèi)部盒子內(nèi)邊距增加,寬度再減去內(nèi)邊距,或使用box-sizing: border-box屬性
padding和border被包含在定義的width和height之內(nèi)。對(duì)象的實(shí)際寬度就等于設(shè)置的width值,即使定義有border和padding也不會(huì)改變對(duì)象的實(shí)際寬度,即 ( Element width = width )
實(shí)現(xiàn)效果如下:
(3)自動(dòng)擠壓的效果:
<style> .box1 { width: 100px; height: 100px; background-color: limegreen; margin-bottom: 50px; } .box2 { width: 100px; height: 100px; background-color: red; margin-top: 30px; } .box3 { box-sizing: border-box; width: 200px; height: 200px; background-color: rgb(245, 125, 125); } .box4 { width: 100px; height: 100px; background-color: blue; } /* .box4 { margin-top: 50px; } */ /* 父級(jí)盒子會(huì)隨內(nèi)部盒子一起移動(dòng),為了解決,讓內(nèi)部盒子內(nèi)邊距增加,寬度再減去內(nèi)邊距 */ .box3 { padding-top: 50px; /* height: 150px; */ } .box5 { /* 自動(dòng)擠壓 */ width: 150px; height: 150px; background-color: cornflowerblue; } .box5 { margin: 30px auto; } </style> </head> <body> <!-- 1.同級(jí)塌陷,誰大向誰擠壓 --> <div class="box1"></div> <div class="box2"></div> <!-- 嵌套傳遞 --> <hr> <div class="box3"> <div class="box4"></div> </div> <!-- 自動(dòng)擠壓 --> <div class="box5"></div> </body>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
今日總結(jié): 給老師提個(gè)優(yōu)化建議,這個(gè)文章編輯器真的很爛,插入代碼后,繼續(xù)輸入的內(nèi)容就變成代碼模式, 一邊保存,一遍預(yù)覽,重新排版花了30分鐘.也許是我不會(huì)用吧.
微信掃碼
關(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)