定义
Cascading Style Sheets,层叠样式表,是一种用来为结构化文档添加样式的计算机语言
存在的价值
通过 **选择器** 来选中元素并设置其样式
选择器
元素选择器
作用:根据标签名来选中元素 语法:标签名{}
例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> p{ color: red; } </style> </head> <body> <p>我是元素选择器的例子</p> </body> </html>
ID选择器
作用:根据元素的id属性值选中元素 语法:#id属性值{} 例子:#value{}
例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> #value{ color: red; } </style> </head> <body> <p id="value">我是id选择器的例子</p> </body> </html>
类选择器
作用:根据元素的class属性来设置一组元素 语法:.class属性值{}
例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .value{ color: red; } </style> </head> <body> <p class="value">我是元素选择器的例子</p> </body> </html>
复合选择器
交集选择器
作用:选中同时符合多个条件的元素 语法:选择器1选择器2选择器3选择器4{}
例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> p.value1{ color: red; } </style> </head> <body> <p class="value1">我是元素选择器的例子1</p> <p class="value2">我是元素选择器的例子2</p> </p> </body> </html>
并集选择器
作用:同时选择多个选择器对应的元素 语法:选择器1,选择器2,选择器3,选择器4{}
例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div,p{ color: red; } </style> </head> <body> <div>我是元素选择器的例子1</div> <p>我是元素选择器的例子2</p> </p> </body> </html>
关系选择器
子元素选择器
作用:选中指定父元素的指定子元素 语法:父元素>子元素{}
例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div>p{ color: red; } </style> </head> <body> <div> <p>我是子元素选择器的例子</p> </div> </body> </html>
后代选择器
作用:选中指定元素的后代元素 语法:祖先元素 后代元素{}
例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div span{ color: red; } </style> </head> <body> <div> <p>我是<span>子元素选择器</span>的例子1</p> <span>我是</span> </div> </body> </html>
兄弟选择器
+ 作用:选择一个指定元素的 下 一个 兄弟元素 语法:前一个+后一个{} ~ 作用:选择一个指定元素 下面 所有的 兄弟元素 语法:兄~弟{}
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> p+div{ color: orange; } p~span{ color: blue; } </style> </head> <body> <p>我是兄弟元素的例子1</p> <div>我是兄弟元素的例子2</div> <div>我是兄弟元素的例子3</div> <span>我是兄弟元素的例子4</span> <span>我是兄弟元素的例子5</span> </body> </html>
属性选择器
作用:通过属性名和属性值选择选择元素 用法: 选择含有指定属性的元素: 标签名[属性名]{} 选择指定的属性和属性值的元素: 标签名[属性名=属性值]{} 选择属性值以指定值开头的元素: 标签名[属性名^=属性值]{} 选择属性值以指定值结尾的元素: 标签名[属性名$=属性值]{} 选择属性值含有指定值的元素: 标签名[属性名*=属性值]{}
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> p[title]{ color: red; } p[title=a]{ color: orange; } p[title^=ab]{ color:yellow; } p[title$=da]{ color:green; } p[title*=e]{ color:blue; } </style> </head> <body> <p title>我是属性选择器例子1</p> <p title="a">我是属性选择器例子2</p> <p title="abcd">我是属性选择器例子3</p> <p title="bcda">我是属性选择器例子4</p> <p title="abecd">我是属性选择器例子5</p> </body> </html>
伪类选择器
作用:通过元素的特殊状态选择元素 用法: 标签名:first-child{} (第一个元素) 标签名:last-child (最后一个元素) 标签名:nth-child() (选中第n个元素) 特殊值: n 第n个 n的范围 0 到 正无穷 2n或even表示选中偶数位的元素 2n+1或odd便是选中奇数位的元素 标签名:first-of-type (第一个此标签类型的元素) 标签名:last-of-type (最后一个此标签类型的元素) 标签名:nth-of-type() (选中第n个此标签类型的元素) 标签名:not()否定伪类 将符合条件的元素从选择其中去除
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> /* ul > li:first-of-type{ color: red; } ul > span:first-child{ color: orange; } ul > li:nth-child(2n+1){ color: blue; } ul > li:not(li:nth-of-type(3)){ color: red; } */ </style> </head> <body> <ul> <span>我是一个span</span> <li>第一个</li> <li>第二个</li> <li>第三个</li> <li>第四个</li> <li>第五个</li> </ul> </body> </html>
伪元素选择器
作用:通过特殊位置选择伪元素(不存在的元素) 用法: 标签名::first-letter (选择第一个字母) 标签名::first-line (选择第一行) 标签名::selection (选择鼠标选中的内容) 标签名::before (选择元素的开始) 标签名::after (选择元素的最后) before和after 必须结合content属性来使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> /* p::first-letter{ font-size: 50px; } p::first-line{ background-color: yellow; } p::selection{ background-color: aqua; } p::before{ content: '😊'; } p::after{ content: 'haha'; color: green; } */ </style> </head> <body> <p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ad fuga amet fugit obcaecati. Corrupti iste deleniti consectetur labore est ea sapiente ut deserunt sunt! Exercitationem nemo illum labore omnis earum?</p> <!-- lorem 创建一段随机的英文单词 --> </body> </html>