css 选择器学习笔记
網(wǎng)站鏈接:
https://www.w3.org/TR/CSS21/selector.html
CSS支持的所有選擇器:
選擇器 grouping
當幾個選擇器共享同一部分屬性時,選擇器可以放到同一組里。
下列兩種css定義等價:
h1 { font-family: sans-serif } h2 { font-family: sans-serif } h3 { font-family: sans-serif } h1, h2, h3 { font-family: sans-serif }Universal selector
If the universal selector is not the only component of a simple selector, the “*” may be omitted.
下列這些css定義是等價的:
Descendant selectors
<html> <style> p {color: red; }div p {color: blue; } </style> <p>Parent p</p> <div> <span>1</span> <span>2</span> </div><div id = "jerry" actions tool="spa"> <p>3</p> <p>4</p> </div> </html>嵌套在div里的p變成了藍色。
descendent 選擇器和屬性選擇器搭配工作:
div p *[href] {color: blue; }匹配在div和p嵌套中的任意元素,且這些元素要包含href屬性。
Child selectors
匹配直接子節(jié)點。
<html> <style> div>p {color: red; }div p *[href] {color: blue; } </style><p>Parent p</p> <div> <span>1</span> <span>2</span> </div><div id = "jerry" actions tool="spa"> <p href="1"><span href="2">inside p </span> </p> <p>4</p> </div> </html>必須是div的直接子節(jié)點p才行:
Adjacent sibling selectors
Adjacent sibling selectors have the following syntax: E1 + E2, where E2 is the subject of the selector. The selector matches if E1 and E2 share the same parent in the document tree and E1 immediately precedes E2, ignoring non-element nodes (such as text nodes and comments).
例子:
p + div {color: green; }修飾的是 + 號后面的div節(jié)點:
attribute selector
分為以下四種:
- [att]
Match when the element sets the “att” attribute, whatever the value of the attribute.
-
[att=val]
Match when the element’s “att” attribute value is exactly “val”. -
[att~=val]
Represents an element with the att attribute whose value is a white space-separated list of words, one of which is exactly “val”. If “val” contains white space, it will never represent anything (since the words are separated by spaces). If “val” is the empty string, it will never represent anything either. -
[att|=val]
Represents an element with the att attribute, its value either being exactly “val” or beginning with “val” immediately followed by “-” (U+002D). This is primarily intended to allow language subcode matches (e.g., the hreflang attribute on the a element in HTML) as described in BCP 47 ([BCP47]) or its successor. For lang (or xml:lang) language subcode matching, please see the :lang pseudo-class.
例子:
<html> <style>div[att] {color: blue; }div[att=red] {color: red; }div[att~=green] {color: green; }div[att|=lime] {color: lime; }</style><div att>attribute </div><div att="red">red </div><div att="red green">green </div><div att="lime">lime </div><div att="lime-jerry">lime-Jerry </div></html>效果:
另一個例子:
The following rule will match for values of the “l(fā)ang” attribute that begin with “en”, including “en”, “en-US”, and “en-cockney”:
*[lang|="en"] { color : red }class 選擇器
class選擇器和attribute選擇器可以互換:
<html> <style>div.title{color: blue; }div[class='red'] {color: red; }div.green {color: green; }</style><div class='title'>title </div><div class='red'>red </div><div class=green>green </div> </html>效果:
div.green.red {color: lime; }如果出現(xiàn)多個., 這些由多個.連接起來的class名稱是AND的關(guān)系,即必須同時包含這些class才匹配:
- Pseudo-elements create abstractions about the document tree beyond those specified by the document language. For instance, document languages do not offer mechanisms to access the first letter or first line of an element’s content. CSS pseudo-elements allow style sheet designers to refer to this otherwise inaccessible information. Pseudo-elements may also provide style sheet designers a way to assign style to content that does not exist in the source document (e.g., the :before and :after pseudo-elements give access to generated content).
- Pseudo-classes classify elements on characteristics other than their name, attributes or content; in principle characteristics that cannot be deduced from the document tree. Pseudo-classes may be dynamic, in the sense that an element may acquire or lose a pseudo-class while a user interacts with the document. The exceptions are ‘:first-child’, which can be deduced from the document tree, and ‘:lang()’, which can be deduced from the document tree in some cases.
Neither pseudo-elements nor pseudo-classes appear in the document source or document tree. 二者都不會出現(xiàn)在document tree里。
例子:
<html> <style>div > p:first-child { color: red; }</style><div><p>1</p><p>2</p><p>3</p> </div> </html>效果:
:hover, :active, and :focus
如果a標簽沒有href屬性,則無法接受hover或者focus事件:
<html> <style>a:focus { color: yellow } a:focus:hover { color: red }</style><div> <a >1</a> <a >2</a> </div> </html>點擊tab鍵無法focus:
加上href屬性后:
上圖是focus屬性激活的外觀,color:yellow
鼠標放上去后,激活hover,color:red
更多Jerry的原創(chuàng)文章,盡在:“汪子熙”:
總結(jié)
以上是生活随笔為你收集整理的css 选择器学习笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: post请求头中常见content-ty
- 下一篇: 开发“航班查询及预定”系统