元素选择器
选择器分组
类选择器详解
ID 选择器详解
属性选择器详解
后代选择器
子元素选择器
最常见的选择器就是元素选择器,文档的元素就是最基本的选择器。
例如:h1{} a{}
例子: h1,h2{}
通配符 *{}
类选择器允许以一种独立与文档元素的方式来指定样式
例如: .class{}
综合元素选择器:
例如:a.class{}
多类选择器:
例如:.class.class{}
.html
<body> <p class="p1">this is my web page1</p> <p class="p2">this is my web page2</p> <p class="p1 p2">this is my web page3</p> </body> .css .p1{ color: blueviolet; } .p2{ font-size: 30px; } .p1.p2{ font-family: normal; }即包含了p1,p2的特性,又添加了新的特性。
一,ID选择器:类似于类选择器,不过也有一些重要差别
例如:#id{}
二,类选择器和ID选择器区别:
ID只能在文档中使用一次,而类class可以多次使用
ID选择器不能结合使用。
当使用js时候,需要用到id。
三,
id先找到结构,再找到内容,最后进行渲染。
class先渲染,再找到结构,最后找到具体内容。
一,简单属性选择:
例如:[title]{}
二,根据具体属性值选择;
除了选择拥有某些的元素,还可以进一步缩小选择范围,只选择有特定属性值的元素。
例如:a[href="http://www.baidu.com"]{}
三,属性和属性值必须完全匹配
四,根据部分属性值选择
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>选择器</title> <link rel="stylesheet" href="style.css" type="text/css" /> <!--~包含a单词指定属性--> <style> [title~="a"]{ color: blue; } [title="b"]{ color: red; } a[href="http://www.baidu.com"]{ color: green; font-size: 34px; } </style> </head> <body> <p title="a">hello</p> <p title="a b">hello</p> <a href="http://www.baidu.com">baidu</a> <a href="http://www.souhu.com">souhu</a> </body> </html>
后代选择器可以选择作为某元素后代的元素
.html
<p>this is my <strong>web</strong> page</p>.css p strong{ color: blue; }
与后代选择器相比,子元素选择器只能选择作为某元素子元素的元素
例如:h1>strong{};
.html
<h1>hello <strong>minmin</strong></h1> .css
h1 > strong{ color: blue; }