部分来自456bereastreet
an overview of the syntax for all CSS 2.1 selectors (based on the table inCSS 2.1, 5.1 Pattern matching):
Selector type | Pattern | Description |
---|---|---|
Universal | * | Matches any element. |
Type | E | Matches any E element. |
Class | .info | Matches any element whoseclass attribute contains the valueinfo . |
ID | #footer | Matches any element with anid equal tofooter . |
Descendant | E F | Matches any F element that is a descendant of an E element. |
Child | E > F | Matches any F element that is a child of an E element. |
Adjacent | E + F | Matches any F element immediately preceded by a sibling element E. |
Attribute | E[att] | Matches any E element that has anatt attribute, regardless of its value. |
Attribute | E[att=val] | Matches any E element whoseatt attribute value is exactly equal toval . |
Attribute | E[att~=val] | Matches any E element whoseatt attribute value is a list of space-separated values, one of which is exactly equal toval . |
Attribute | E[att|=val] | Matches any E element whoseatt attribute has a hyphen-separated list of values beginning withval . |
The :first-child pseudo-class | E:first-child | Matches element E when E is the first child of its parent. |
The link pseudo-classes | E:link E:visited |
Matches not yet visited (:link) or already visited (:visited) links. |
The dynamic pseudo-classes | E:active E:hover E:focus |
Matches E during certain user actions. |
The :language pseudo-class | E:lang(c) | Matches elements of type E that are in language c. |
The :first-line pseudo-element | E:first-line | Matches the contents of the first formatted line of element E. |
The :first-letter pseudo-element | E:first-letter | Matches the first letter of element E. |
The :before and :after pseudo-elements | E:before E:after |
Used to insert generated content before or after an element’s content. |
I’ll explain each of these selector types in more detail in this two part article, so keep reading. A few terms used in that table and in the rest of this article may need some clarification:
An element that has the same parent as the current element.
元素也可以基于它们的类而被选择:
td.fancy
{
color: #f60;
background: #666;
}
在上面的例子中,类名为 fancy 的表格单元将是带有灰色背景的橙色。
You can assign multiple class names to an element – theclass
attribute can hold a space separated list of class names. Class selectors can then be used to target only elements which have several class names. This rule will matchp
elements which haveboth “info” and “error” in their list of class names:
p.info.error { color:#900; font-weight:bold; }
Multiple attribute selectors can be used in the same selector. This makes it possible to match against several different attributes for the same element. The following rule would apply to allblockquote
elements that have aclass
attribute whose value is exactly “quote”, and acite
attribute (regardless of its value):
blockquote[class=quote][cite] { color:#f00; }
An element may match several pseudo-classes at the same time. An element could have focus and be hovered over, for instance:
input[type=button]:focus {
color:#000;
background:#ffe;
}
input[type=text]:focus:hover {
background:#fff;
}
The first rule will match single lineinput
elements that have focus, and the second rule will match the same elements when they are also hovered over.
Each of the selectors that form a descendant selector can be a simple selector of any form. The selector in the following rule matches allp
elements with a class name ofinfo
that are contained by anli
element that is contained by adiv
element with the idmyid
.
div#myid li p.info { color:#f00; }
The mistake many make is to not list the full selectors. Assume that you have the following markup:
-
News
-
-
- Item 1
-
- Item 2
-
-
Now, say you want to apply the same amount of margin to level 3 headings and unordered lists, but only if they are in thediv
element whoseid
is “news”. Here is thewrongway:
div#news h3, ul {
margin:0 2em;
}
Thiswillaffect bothh3
andul
elements indiv#news
. The problem is that it will targetallul
elements in the document, not only those indiv#news
.
Here is thecorrectway of grouping the selectors in this case:
div#news h3,
div#news ul {
margin:0 2em;
}
So, when grouping selectors, remember to fully specify each selector.
关于!important
HTML
这个例子应该是大家经常见到的important的用法了,在IE6环境下,这行字是蓝色,在IE7及firefox下,为红色。这是因为IE6不认important(即不认 !importmant 但是还是认!important前面的color:red),并且color:blue放在color:red的后面(后面的css定义覆盖了前面的color:red),所以在IE6下字为蓝色;而在IE7及firefox下important级别优先,所以color:red !important;和color:blue;的前后顺序没有关系,在IE7及firefox下red跟blue谁先谁后都没有关系。
我们所认为的“字体”可能有许多字体变形组成,分别用来描述粗体、斜体文本,等等。例如,你可能已经对字体 Times 很熟悉。不过,Times 实际上是多种变形的一个组合,包括 TimesRegular、TimesBold、TimesItalic、TimesOblique、TimesBoldItalic、TimesBoldOblique,等等。Times 的每种变形都是一个具体的字体风格(font face),而我们通常认为的 Times 是所有这些变形字体的一个组合。换句话说,Times 实际上是一个字体系列(font family),而不只是单个的字体,尽管我们大多数人都认为字体就是某一种字体。
除了各种特定字体系列外(如 Times、Verdana、Helvetica 或 Arial),CSS 还定义了 5 种通用字体系列:
这些字体无法用任何特征来定义,只有一点是确定的,那就是我们无法很容易地将其规划到任何一种其他的字体系列当中。这样的字体包括 Western、Woodblock 和 Klingon。
看了这些,终于知道Chrome的字体设置里面那么多选项是什么意思了,哈哈!