一、实例演示相邻选择器与兄弟选择器,并分析异同
实例
<!DOCTYPE html> <html lang="zh_CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>相邻选择器与兄弟选择器</title> <link rel="stylesheet" href="demo03a.css"> </head> <body> <ul> <li class="testclass">1</li> <li id="testid01">2</li> <li class="testclass">3</li> <li class="testclass">4</li> <li id="testid02">5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> </ul> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
/* demo03a.css */ ul { margin: 0; padding-left: 0; border: 1px dashed red; } ul li { list-style-type: none; width: 40px; height: 40px; background-color: wheat; border: 1px solid black; /* 水平和垂直居中 */ text-align: center; line-height: 40px; border-radius: 50%; /* 将块元素转换为内联元素 */ display: inline-block; box-shadow: 2px 2px 1px #777; } /* 相邻选择器 */ #testid01+* { background-color: blue; } /* 兄弟选择器 */ #testid02~* { background-color: red; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
二、实例演示:nth-child() 和 :nth-of-type()选择器,并分析异同
实例
<!DOCTYPE html> <html lang="zh_CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>子类选择器与类型选择器</title> <link rel="stylesheet" href="demo03b.css"> </head> <body> <ul> <li class="testclass">1</li> <li id="testid01">2</li> <li class="testclass">3</li> <li class="testclass">4</li> <li id="testid02">5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> </ul> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
/* demo03b.css */ ul { margin: 0; padding-left: 0; border: 1px dashed red; } ul li { list-style-type: none; width: 40px; height: 40px; /* background-color: wheat; */ border: 1px solid black; /* 水平和垂直居中 */ text-align: center; line-height: 40px; border-radius: 50%; /* 将块元素转换为内联元素 */ display: inline-block; box-shadow: 2px 2px 1px #777; } /* 伪类:子元素选择器 */ ul :first-child { background-color: #ff0; } ul :nth-child(6) { background-color: #0ff; } /* 伪类:类型选择器 */ ul li:nth-of-type(9) { background-color: #0f0; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
三、实例演示:padding 对盒子大小的影响与解决方案, 使用宽度分离或box-sizing
实例
<!DOCTYPE html> <html lang="zh_CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>padding对盒子大小的影响与解决方案</title> <style> /* box-sizing设置为border-box,限定基准为边框 */ .box1 { width: 400px; box-sizing: border-box; padding: 50px; background-color: #f00; border: 1px solid blue; } </style> </head> <body> <div class="box1"> <img src="./test01.png" alt="中秋快乐" "> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
四、实例演示: margin中的同级塌陷, 嵌套传递与自动挤压, 并提出解决方案或应用场景
实例
<!DOCTYPE html> <html lang="zh_CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>margin中的同级塌陷, 嵌套传递与自动挤压</title> <style> /* box1与box2同级塌陷 */ .box1 { width: 200px; height: 200px; background-color: #00f; margin-bottom: 50px; } .box2 { width: 300px; height: 250px; background-color: #f00; margin-top: 30px; } /* box3与box4嵌套传递*/ .box3 { width: 200px; height: 200px; background-color: lightgreen; } .box4 { width: 150px; height: 100px; background-color: lightcoral; margin-top: 30px; } .box5 { width: 250px; height: 100px; background-color: #ffff00; margin: auto; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> <div class="box3"> <div class="box4"></div> </div> <div class="box5"></div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例