偽類與偽元素,很常讓人突然想不起誰是誰?好像是雙胞胎似的。剛開始學的時候真的頭很大,後來發現這兩位很不同功能啊,也聽說,2.2版本時,他們都是(:
),都只有一個冒號,如果你偽元素不小心寫成(:
)瀏覽器還是會認得出來呦,今天就來認識一下這兩位仁兄吧!本次搭配:
:
)Pseudo-classes由以下目錄可見,偽類有這七種分類。而偽類屬於6. Simple selectors。
Level3 6.6 Pseudo-classes 偽類
6.6.1.Dynamic pseudo-classes 動態偽類
6.6.2.The target pseudo-class 目標偽類:target
6.6.3.The language pseudo-class 語言偽類 :lang
6.6.4.The UI element states pseudo-classesUI 元素聲明偽類
6.6.5.Structural pseudo-classes 結構偽類
6.6.6.Blank
6.6.7.The negation pseudo-class 否定偽類
level3
- The pseudo-class concept is introduced to permit selection based on information that lies outside of the document tree or that cannot be expressed using the other simple selectors.
- A pseudo-class always consists of a "colon" (
:
) followed by the name of the pseudo-class and optionally by a value between parentheses.- Pseudo-classes are allowed in all sequences of simple selectors contained in a selector.
- Pseudo-classes are allowed anywhere in sequences of simple selectors, after the leading type selector or universal selector (possibly omitted).
- Pseudo-class names are case-insensitive.
- Some pseudo-classes are mutually exclusive, while others can be applied simultaneously to the same element.
-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.
由規範得知,
::
)Pseudo-elements由目錄可見,偽元素有這四種分類。
Level3 7 Pseudo-elements 偽元素
7.1.The ::first-line pseudo-element
7.2.The ::first-letter pseudo-element
7.3.Blank
7.4.The ::before and ::after pseudo-elements
- 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.- Pseudo-elements allow authors to refer to this otherwise inaccessible information.
- Pseudo-elements may also provide authors a way to refer to content that does not exist in the source document (e.g., the
::before
and::after
pseudo-elements give access to generated content).- A pseudo-element is made of two colons (
::
) followed by the name of the pseudo-element.- This
::
notation is introduced by the current document in order to establish a discrimination between pseudo-classes and pseudo-elements.- Only one pseudo-element may appear per selector, and if present it must appear after the sequence of simple selectors that represents the subjects of the selector. Note: A future version of this specification may allow multiple pseudo-elements per selector.
由規範得知,
他們都是在選擇器這篇章下的內容,但分不同組啊,一個是在simple selector,一個像是組合器那樣自己獨立一小節,功能各不同,簡單分類:
選擇
DOM tree 之外的信息,或者使用其他簡單選擇器不能表達的信息
。例如:設定滑鼠滑過(:hover)、被訪問過的連結樣式(:visited)。創造
關於 DOM tree的抽象內容,提供方法來引用源文檔中『不存在』的內容
,較常用的是 ::before
::after
,來做些特殊效果或裝飾,不管是線線,還是點點,任君挑選。