CSS 偽元素
CSS 偽元素
CSS偽元素是用來添加一些選擇器的特殊效果。
語法
偽元素的語法:
selector:pseudo-element {property:value;}
CSS類也可以使用偽元素:
selector.class:pseudo-element {property:value;}
:first-line 偽元素
"first-line" 偽元素用于向文本的首行設置特殊樣式。
在下面的例子中,瀏覽器會根據(jù) "first-line" 偽元素中的樣式對 p 元素的第一行文本進行格式化:
<!DOCTYPE html> <html> <head> <style> p:first-line { color:#ff0000; font-variant:small-caps; } </style> </head> <body> <p>您可以使用:線偽元素添加特殊效果給第一行文本。</p> </body> </html>
運行程序嘗試一下
注意:"first-line" 偽元素只能用于塊級元素。
注意: ?下面的屬性可應用于 "first-line" 偽元素:
- font properties
- color properties?
- background properties
- word-spacing
- letter-spacing
- text-decoration
- vertical-align
- text-transform
- line-height
- clear
:first-letter 偽元素
"first-letter" 偽元素用于向文本的首字母設置特殊樣式:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> p:first-letter { color:#ff0000; font-size:xx-large; } </style> </head> <body> <p>You can use the :first-letter pseudo-element to add a special effect to the first character of a text!</p> </body> </html>
運行程序嘗試一下
注意: "first-letter" 偽元素只能用于塊級元素。
注意: 下面的屬性可應用于 "first-letter" 偽元素:?
- font properties
- color properties?
- background properties
- margin properties
- padding properties
- border properties
- text-decoration
- vertical-align (only if "float" is "none")
- text-transform
- line-height
- float
- clear
偽元素和CSS類
偽元素可以結合CSS類:?
p.article:first-letter {color:#ff0000;}
<p class="article">A paragraph in an article</p>
<p class="article">A paragraph in an article</p>
上面的例子會使所有 class 為 article 的段落的首字母變?yōu)榧t色。
Multiple Pseudo-elements
可以結合多個偽元素來使用。
在下面的例子中,段落的第一個字母將顯示為紅色,其字體大小為 xx-large。第一行中的其余文本將為藍色,并以小型大寫字母顯示。
段落中的其余文本將以默認字體大小和顏色來顯示:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> p:first-letter { color:#ff0000; font-size:xx-large; } p:first-line { color:#0000ff; font-variant:small-caps; } </style> </head> <body> <p>You can combine the :first-letter and :first-line pseudo-elements to add a special effect to the first letter and the first line of a text!</p> </body> </html>
運行程序嘗試一下
CSS - :before 偽元素
":before" 偽元素可以在元素的內(nèi)容前面插入新內(nèi)容。
下面的例子在每個 <h1>元素前面插入一幅圖片:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> h1:before {content:url(https://img.php.cn/upload/course/000/000/006/5809800b44336872.jpg);} </style> </head> <body> <h1>This is a heading</h1> <p>The :before pseudo-element inserts content before an element.</p> <h1>This is a heading</h1> <p><b>注意:</b>僅當 !DOCTYPE 已經(jīng)聲明 IE8 支持這個內(nèi)容屬性</p> </body> </html>
運行程序嘗試一下
CSS - :after 偽元素
":after" 偽元素可以在元素的內(nèi)容之后插入新內(nèi)容。
下面的例子在每個 <h1> 元素后面插入一幅圖片:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> h1:after {content:url(https://img.php.cn/upload/course/000/000/006/5809800b44336872.jpg);} </style> </head> <body> <h1>This is a heading</h1> <p>The :after pseudo-element inserts content after an element.</p> <h1>This is a heading</h1> <p><b>注意:</b>僅當!DOCTYPE 已經(jīng)聲明 IE8支持這個內(nèi)容屬性.</p> </body> </html>
運行程序嘗試一下
所有CSS偽類/元素
選擇器 | 示例 | 示例說明 |
---|---|---|
:link | a:link | 選擇所有未訪問鏈接 |
:visited | a:visited | 選擇所有訪問過的鏈接 |
:active | a:active | 選擇正在活動鏈接 |
:hover | a:hover | 把鼠標放在鏈接上的狀態(tài) |
:focus | input:focus | 選擇元素輸入后具有焦點 |
:first-letter | p:first-letter | 選擇每個 元素的第一個字母 |
:first-line | p:first-line | 選擇每個 元素的第一行 |
:first-child | p:first-child | 選擇器匹配屬于任意元素的第一個子元素的 <]p> 元素 |
:before | p:before | 在每個 元素之前插入內(nèi)容 |
:after | p:after | 在每個 元素之后插入內(nèi)容 |
:lang(language) | p:lang(it) | 為 元素的lang屬性選擇一個開始值 |