日期:2014-05-17  浏览次数:20756 次

css选择器、伪类易错点总结

1.CSS 多类选择器

<p class="important urgent warning ">
This paragraph is a very important and urgent warning.</p>

?? 用一下代码都可以匹配

.important.warning {background:silver;}
.important.urgent.warning {background:silver;}

?2.CSS ID选择器

#intro {font-weight:bold;}
<p id="intro ">This is a paragraph of introduction.</p>

?? 大小写敏感

3.CSS 属性选择器

??? 1)属性和属性值必须完全匹配

p[class="important warning "] {color: red;}
//p[class="important "] {color: red;} error
<p class="important warning ">This paragraph is a very important warning.</p>

? 2)根据部分属性值选择

p[class~ ="important "] {color: red;}
<p class="important warning ">This paragraph is a very important warning.</p>

?3)子串匹配选择器

a[href* ="w3school.com.cn"]
<a href="http://www.w3school.com.cn/css/">CSS</a>

4)特定属性选择器

*[lang| ="en"] {color: red;}
<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 em {color:red; font-weight:bold;}
<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 > strong {color:red;}
<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 + p {margin-top:50px;}
<h1>This is a heading.</h1>
<p>This is paragraph. </p>
<p>This is paragraph.</p>

?7.CSS伪类

?? 1)锚伪类