选择器
通配符选择器
元素选择器(type selectors)
后代选择器
用空格分隔
<!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>
<link rel="stylesheet" href="./text.css">
</head>
<body>
<ul>
<li>111
<h1>111</h1>
<h1>111
<p>我是个p</p>
</h1>
</li>
<li>111</li>
<li>111</li>
<li>111<h1>111</h1>
</li>
<h1>111</h1>
<h1>111</h1>
<h1>111</h1>
</ul>
</body>
</html>
li h1 {
color: red;
}
li p {
color: pink;
}
子选择器
用大于号来链接
后代选择器可以跨多个级别
子选择器只能选择他的亲孩子
<!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>
<link rel="stylesheet" href="./text.css">
</head>
<body>
<ul>
<li>111
<h1>被选中</h1>
<h1>被选中
<p>我是p
<---这里p会变颜色是因为继承->
<a href="#">aaa
</a>
</p>
</h1>
</li>
<li>111</li>
<li>111</li>
<li>111<h1>被选中</h1>
</li>
<h1>111</h1>
<h1>111</h1>
<h1>111</h1>
</ul>
</body>
</html>
li > h1 {
color: red;
}
li > a {
color: pink;/*没有被选中了*/
}
相邻兄弟选择器
语法是e1 + e2
- e2是被选择的
- 如果e1是e2他俩父级是一个标签,并且e1在e2前面
<!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>
<link rel="stylesheet" href="./text.css">
</head>
<body>
<ul>
<li>111</li>
<li>111</li>
<li>111</li>
<li>111<h1>111</h1>
</li>
<h1>被选中</h1>
<li>111</li>
<h1>被选中</h1>
<h1>111</h1>
<h1>111</h1>
</ul>
</body>
</html>
li + h1 {
color: red;
}
属性选择器
-
[att] <--选择所有的带这个属性的元素-->
<!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> <link rel="stylesheet" href="./text.css"> </head> <body> <a href="">aaa</a> </body> </html>
[href] { color: red; }
-
[att=val]
<!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> <link rel="stylesheet" href="./text.css"> </head> <body> <a href="www.baidu.com">aaa</a> <a href="#" class="example">aaa</a> </body> </html>
[href] { color: red; } [class = example] { color: pink; }
-
[att~=val]
<!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> <link rel="stylesheet" href="./text.css"> </head> <body> <a href="www.baidu.com">aaa</a> <a href="#" class="example text">aaa</a> </body> </html>
[href] { color: red; } [class ~= example]{ color: pink; }
[att|=val]
暂时还不懂
类选择器
.cls
id选择器
伪元素和伪类选择器
伪类选择器
:first-child pseudo-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>
<link rel="stylesheet" href="./text.css">
</head>
<body>
<div>
<p>The :first-child pseudo-class matches an element that is the first child element of some other element.
In the following example, the selector matches any P element that is the first child of a DIV element. The
rule suppresses indentation for the first paragraph of a DIV:</p>
<p>第二段:The :first-child pseudo-class matches an element that is the first child element of some other
element.
In the following example, the selector matches any P element that is the first child of a DIV element.
The rule suppresses indentation for the first paragraph of a DIV:</p>
</div>
</body>
</html>
div > p:first-child {
color: red;
}/*只有第一段变成红色了*/
这样的话,第二个p就不会被选中了
<!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>
<link rel="stylesheet" href="./text.css">
</head>
<body>
<P> The last P before the note.
<DIV class="note">
<H2>Note</H2>
<P> The first P inside the note.
</DIV>
</body>
</html>
div > p:first-child {
color: red;
}
例子3
<!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>
<link rel="stylesheet" href="./text.css">
</head>
<body>
<P>abc <EM>default</EM></P>
</body>
</html>
p:first-child em {
font-weight: bold
}
/*
p em:first-child {
font-weight: bold
}
这两个效果一样
*/
The link pseudo-classes: :link and :visited
-
a:link表示未被访问的链接;a:visited表示已访问的链接
<!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> <link rel="stylesheet" href="./text.css"> </head> <body> <A class="external" href="http://out.side/">external link</A> </body> </html>
a.external:link { color: red } a.external:visited { color: pink; }
The dynamic pseudo-classes: :hover, :active, and :focus
- a:hover 鼠标悬停上面的
a.external:link {
color: red
}
a.external:visited {
color: pink;
}
a:hover {
background-color: #484848;
}
- a:active选择活动的链接
a.external:link {
color: red
}
a.external:visited {
color: pink;
}
a:active {
background-color: #484848;
}
3.:lang
<!DOCTYPE html>
<html>
<head>
<style>
p:lang(en)
{
background:yellow;
}
</style>
</head>
<body>
<p>我是唐老鸭。</p>
<p lang="en">I live in Duckburg.</p>
<p><b>注释:</b>对于在 IE8 中工作的 :lang,必须声明 DOCTYPE。</p>
</body>
</html>
伪元素
:first-line
<!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>
<link rel="stylesheet" href="./text.css">
</head>
<style>
p:first-line {
text-transform: uppercase
}
</style>
<body>
<P>This is a somewhat long HTML
paragraph that will be broken into several
lines. The first line will be identified
by a fictional tag sequence. The other lines
will be treated as ordinary lines in the
paragraph.</P>
</body>
</html>
:first-letter
<!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>
<link rel="stylesheet" href="./text.css">
</head>
<style>
p {
line-height: 1.1
}
p:first-letter {
font-size: 3em;
font-weight: normal
}
span {
font-weight: bold
}
</style>
<body>
<p><span>Het hemelsche</span> gerecht heeft zich ten lange lesten<br>
Erbarremt over my en mijn benaeuwde vesten<br>
En arme burgery, en op mijn volcx gebed<br>
En dagelix geschrey de bange stad ontzet.
</body>
</html>
The :before and :after pseudo-elements
会在选择标签后面插入内容
<!DOCTYPE html>
<html>
<head>s
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
p:after
{
content:"- 注意我";
}
</style>
</head>
<body>
<p>我的名字是 Donald</p>
<p>我住在 Ducksburg</p>
<p><b>注意:</b> :after在IE8中运行,必须声明 !DOCTYPE </p>
</body>
</html>