1.CSS 多类选择器
This paragraph is a very important and urgent warning.</p>
用一下代码都可以匹配
.important.urgent.warning {background:silver;}
2.CSS ID选择器
<p id="intro ">This is a paragraph of introduction.</p>
大小写敏感
3.CSS 属性选择器
1)属性和属性值必须完全匹配
//p[class="important "] {color: red;} error
<p class="important warning ">This paragraph is a very important warning.</p>
2)根据部分属性值选择
<p class="important warning ">This paragraph is a very important warning.</p>
3)子串匹配选择器
<a href="http://www.w3school.com.cn/css/">CSS</a>
4)特定属性选择器
<p lang="en ">Hello!</p>
<p lang="en -us">Greetings!</p>
<p lang="en -au">G'day!</p>
<p lang="fr">Bonjour!</p>
<p lang="cy-en">Jrooana!</p>
选择 lang 属性等于 en 或以 en- 开头的所有元素
4.CSS后代选择器
<ul>
<li>List item 1
<ol>
<li>List item 1-1</li>
<li>List item 1-2</li>
<li>List item 1-3
<ol>
<li>List item 1-3-1</li>
<li>List item <em>1-3-2</em>
</li>
<li>List item 1-3-3</li>
</ol>
</li>
<li>List item 1-4</li>
</ol>
</li>
<li>List item 2</li>
<li>List item 3</li>
</ul>
5.CSS子元素选择器
不希望选择任意的后代元素,而是希望缩小范围,只选择某个元素的子元素,自己的第一代
<h1>This is <strong>very</strong><strong>very2</strong> important.</h1>
<h1>This is <em>really <strong>very</strong></em> important.</h1>
6.CSS相邻兄弟元素
<h1>This is a heading.</h1>
<p>This is paragraph. </p>
<p>This is paragraph.</p>
7.CSS伪类
1)锚伪类
a:visited {color: #00FF00} /* 已访问的链接 */
a:hover {color: #FF00FF} /* 鼠标移动到链接上 */
a:active {color: #0000FF} /* 选定的链接 */
2)first-child伪类
first-child 伪类来选择元素的第一个子元素
li:first-child {text-transform:uppercase;}
<div>
<p>These are the necessary steps:</p>
<ul>
<li>Intert Key</li>
<li>Turn key <strong>clockwise</strong></li>
<li>Push accelerator</li>
</ul>
<p>Do <em>not</em> push the brake at the same time as the accelerator.</p>
</div>
在上面的例子中,作为第一个元素的元素包括第一个 p、第一个 li 和 strong 和 em 元素 。
最常见的错误是认为 p:first-child 之类的选择器会选择 p 元素的第一个子元素。
练习时间到啦
eg1:
下面的选择器会选择作为 td 元素子元素的所有 p 元素 ,这个 td 元素本身从 table 元素继承,该 table 元素有一个包含 company 的 class 属性
eg2:
选择紧接在 table 元素后出现的所有兄弟 ul 元素,该 table 元素包含在一个 body 元素中,body 元素本身是 html 元素的子元素。
eg3:匹配第一个 <p> 元素
<head>
<style type="text/css">
p:first-child {
color: red;
}
</style>
</head>
<body>
<p>some text</p>
<p>some text</p>
</body>
</html>
eg4:匹配所有 <p> 元素中的第一个 <i> 元素
<head>
<style type="text/css">
p > i:first-child {
color:red;
}
</style>
</head>
<body>
<p>some <i>text</i> . some <i>text</i>.</p>
<p>some <i>text</i> . some <i>text</i>.</p>
<p>some<strong><i> test</i></strong></p>
</body>
</html>
选择器匹配所有 <p> 元素中的子类为<i>的第一个 <i> 元素
eg5:匹配所有作为第一个子元素的 <p> 元素中的所有 <i> 元素
<head>
<style type="text/css">
p:first-child i {
color:blue;
}
</style>
</head>
<body>
<p>some <i>text</i> . some <i>text</i> .</p>
<p>some <i>text</i>. some <i>text</i>.</p>
</body>
</html>