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>