一、CSS相邻选择器
‘+’选择器则表示某元素后相邻的兄弟元素,也就是紧挨着的,是单个的
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title></title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> div+p{ background-color: aqua; } </style> </head> <body> <div> <p>第零个段落</p> <div> <p>第一个段落</p> </div> </div> <p>第二个段落</p> <p>第三个段落</p> <p>第四个段落</p> </body> </html>
二、CSS兄弟选择器
‘~’选择器则表示某元素后所有同级的指定元素,强调所有的
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <style> .h3 ~ p{ color: red; } </style> </head> <body> <p>这是段落标签</p> <p>这是段落标签</p> <p>这是段落标签</p> <h3>这是标题标签</h3> <p>这是段落标签</p> <p>这是段落标签</p> <p>这是段落标签</p> <h3>这是标题标签</h3> <p>这是段落标签</p> <p>这是段落标签</p> <p>这是段落标签</p> </body> </html>
三、CSS :nth-child() 选择器
:nth-child(n) 选择器匹配属于其父元素的第 N 个子元素,不论元素的类型。
<!DOCTYPE html> <html> <head> <style> p:nth-child(2){ background:#ff0000; } </style> </head> <body> <h1>这是标题</h1> <p>第一个段落。</p> <p>第二个段落。</p> <p>第三个段落。</p> <p>第四个段落。</p> <p><b>注释:</b>Internet Explorer 不支持 :nth-child() 选择器。</p> </body> </html>
四、CSS :nth-of-type() 选择器
:nth-of-type(n)选择器匹配同类型中的第n个同级兄弟元素。(n可以是一个数字,一个关键字,或者一个公式。)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> <style> p:nth-of-type(2) { background:#ff0000; } </style> </head> <body> <h1>This is a heading</h1> <p>The first paragraph.</p> <p>The second paragraph.</p> <p>The third paragraph.</p> <p>The fourth paragraph.</p> </body> </html>
五,padding对盒子宽度的影响
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .box{ width: 200px; height: 200px; border: 1px solid #ddd; } .box2{ width: 200px; height: 100px; padding: 12px; background: rgba(0,0,0,0.5); color: #fff; } </style> </head> <body> <div class="box"> <div class="box2"> 设置了padding,会在原有的宽度上撑大 </div> </div> </body> </html>
解决方法:
1,减去padding值
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .box{ width: 200px; height: 200px; border: 1px solid #ddd; } .box2{ width: 170px; height: 100px; padding: 15px; background: rgba(0,0,0,0.5); color: #fff; } </style> </head> <body> <div class="box"> <div class="box2"> 设置宽度时减去padding值 </div> </div> </body> </html>
2,使用 box-sizing
box-sizing: content-box;看着不就是W3C的标准盒模型的计算方式(盒子宽度=宽度+padding+margin+border)
box-sizing: border-box;就是IE盒模型的计算方式(盒子宽度=padding+margin)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .box{ width: 200px; height: 200px; border: 1px solid #ddd; } .box2{ width: 200px; height: 100px; padding: 15px; background: rgba(0,0,0,0.5); color: #fff; box-sizing: border-box; } </style> </head> <body> <div class="box"> <div class="box2"> 设置了 box-sizing: border-box; 宽度不用重新计算 </div> </div> </body> </html>
六、 margin中的同级塌陷, 嵌套传递与自动挤压
同级塌陷:同级元素设置了对各自设置了margin距离,谁小塌陷到大的里面
<!DOCTYPE html> <html> <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>Document</title> <style> .box1 { width: 100px; height: 100px; background-color: lightblue; margin-bottom: 10px; } .box2 { width: 100px; height: 100px; background-color: lightcoral; margin-top: 30px; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> </body> </html>
嵌套传递:给盒子内的元素设置margin,最后作用只会到外层的盒子,盒子内部之间设置间距需要用padding
<!DOCTYPE html> <html> <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>Document</title> <style> .box3 { width: 200px; height: 200px; background-color: lightblue; } .box4 { width: 100px; height: 100px; background-color: lightcoral; margin-top: 50px; } </style> </head> <body> <div class="box3"> <div class="box4"></div> </div> </body>
自动挤压:通过设置auto值实现居中显示
<!DOCTYPE html> <html> <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>Document</title> <style> .box3 { width: 200px; height: 200px; background-color: lightblue; margin: 0 auto; } </style> </head> <body> <div class="box3"> </div> </body>