CSS学习笔记之选择符

2014-11-13  本文已影响32人  suemi

类型,类和ID选择器

th,td {
    padding:0 10px 00;
}
h1 {
    font-family:'Times New Roman',serif;
}
//先选用前者,没有前者,再使用后者
* {
    font-family:Arial;
}
div.planet table td {
    padding:0 10px 0 0;
}
//是类为planet的div元素的后代table的后代的td样式
section > p{
    font-style:Italic;
}
//section的子元素p
section ~ p {
    font-style:Italic;
}
//p在section后面就行
section + p {font-style:Italic;}
//p必须紧跟section
selection * a {
    ***;
}
//所有是selection元素孙子的a元素

属性选择器

img[title="hh jj"]{...}

伪类选择器:动态事件,状态改变

a:visited {
    color:mangenta;
}
//被访问过的链接

UI伪类

链接伪类
focus伪类——鼠标放在上面
input:focus {border:1px solid blue;}
target伪类
<a href="#more_info">More Information</a>  
//位于页面其他地方、ID 为 more_info 的那个元素就是目标。该元素可能是这样的:
<h2 id="more_info">This is the information you are looking for.</h2>
//那么,如下 CSS 规则
#more_info:target {background:#eee;}

结构化伪类

伪元素——似有实无的元素

p.age::before {content:"Age: ";}
p.age::after {content:" years.";}
//在类为age的p元素前面增加一段Age: 
p::first-letter {font-size:300%;}
//放大p的第一个字母

继承和层叠

<div id="cascade_demo">
<p id="inheritance_fact">Inheritance is <em>weak</em> in the Cascade</p>
</div>
和下面的规则
div#cascade_demo p#inheritance_fact {color:blue;}
2 - 0 - 2(高特指度)
会导致单词“weak”变成蓝色,因为它从父元素 p 那里继承了这个颜色值。
但是,只要我们再给 em 添加一条规则
em {color:red;}
0 - 0 - 1 (低特指度)
em 就会变成红色。因为,虽然它的特指度低(0-0-1),但 em 继承的颜色值,会被为它明确(显式)指定的颜色值覆盖,就算(隐式)遗传该颜色值的规则的特指度高(2-0-2)也没有用。

上一篇 下一篇

猜你喜欢

热点阅读