㈠什么是偽元素?
不同的解釋:
⑴偽元素是創造關於文檔語言能夠指定的文檔樹之外的抽象。例如文檔語言不能提供訪問元素內容第一字或者第一行的機制。偽元素允許設計師引用它們,否則這是難以辦到的。偽元素還提供樣式設計師給在源文檔中不存在的內容分配樣式(例如::before和:after能夠訪問產生的內容)。
⑵CSS 在渲染文檔的時候,偽元素可以通過 css 給 HTML 添加一個元素(叫標簽也行),這個元素在文檔樹中是找不到的。偽元素被當做 CSS 的樣式來進行展現,用法和普通的元素用法是一樣的。
⑶偽元素用於創建一些不在文檔樹中的元素,並為其添加樣式。比如說,我們可以通過:before來在一個元素前增加一些文本,並為這些文本添加樣式。雖然用戶可以看到這些文本,但是這些文本實際上不在文檔樹中。
⑷CSS偽元素是用來添加一些選擇器的特殊效果。
㈡::before 和 ::after的定義
⑴::before 創建一個偽元素,作為已選中元素的第一個子元素,常通過 content 屬性來為一個元素添加修飾性的內容。
⑵::after 創建一個偽元素,作為已選中元素的最后一個子元素,常通過 content 屬性來為一個元素添加修飾性的內容。
㈢語法
/* CSS3 語法 */ element::before { 樣式 } /* (單冒號)CSS2 */ element:before { 樣式 }
㈣用法
⑴標准寫法是雙冒號(但考慮兼容性也有人寫單冒號)
⑵::before和::after在被選中元素里面、元素現有內容之前(后)插入內容,需要使用content屬性指定要插入的內容。content必須有值(空值也行)。
⑶content插入的內容默認是 inline 元素,可以通過display:block改變。
㈤content屬性
⑴::before和::after必須配合content屬性來使用,content用來定義插入的內容,content必須有值,至少是空。默認情況下,偽類元素的display是默認值inline,可以通過設置display:block來改變其顯示。
⑵content取值
①string
使用引號包一段字符串,將會向元素內容中添加字符串。如:a:after{content:""}
示例如下:引號括起來的一段字符串,顯示到當前元素的前后:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style type="text/css"> p::before{ content: "《"; color: blue; } p::after{ content: "》"; color: blue; } </style> </head> <body> <p>javascript高級程序設計</p> </body> </html>
效果圖:

②attr()
通過attr()調用當前元素的屬性,比如將圖片alt提示文字或者鏈接的href地址顯示出來。
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> a::after{ content: "(" attr(href) ")"; } </style> </head> <body> <a href="https://www.cnblogs.com/shihaiying/">博客地址</a> </body> </html>
效果圖:

③url()/uri()
用於引用媒體文件。
舉例:“百度”前面給出一張圖片,后面給出href屬性。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> a::before{ content: url("https://www.baidu.com/img/baidu_jgylogo3.gif"); } a::after{ content:"("attr(href)")"; } a{ text-decoration: none; } </style> </head> <body> <a href="http://www.baidu.com">百度</a> </body> </html>
效果圖:

④counter()
調用計數器,可以不使用列表元素實現序號功能。
配合counter-increment和counter-reset屬性使用
示例如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> body { counter-reset: section; } h1 { counter-reset: subsection; } h2 { counter-reset: subsection; } h1:before { counter-increment: section; content: counter(section) "、"; } h2:before { counter-increment: subsection; content: counter(section) "." counter(subsection) "、"; } h3:before { counter-increment: subsection; content: counter(section) "." counter(section) "."counter(subsection) "、"; } </style> </head> <body> <h1>大標題</h1> <h2> 二副</h2> <h2> 二副</h2> <h2> 二副</h2> <h3>  三副</h3> <h3>  三副</h3> <h3>  三副</h3> </body> </html>
效果圖:

⑶content運用
①清除浮動
清除浮動方法有多種,現在最常用的就是下面這種方法,僅需要以下樣式即可在元素尾部自動清除浮動
代碼如下:
.cf:before, .cf:after { content: " "; display: table; } .cf:after { clear: both; } .cf { *zoom: 1; }
②float:居中
我們知道float沒有center這個取值,但是可以通過偽類來模擬實現。
這個效果實現很有意思,左右通過::before float各自留出一半圖片的位置,再把圖片絕對定位上去。
③做出各種圖形效果
案例:六芒星
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <style> #star-six { width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 100px solid red; position: relative; } #star-six::after{ width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid transparent; border-top: 100px solid red; position: absolute; content: ""; top: 30px; left: -50px; } </style> <body> <div id="star-six"></div> </body> </html>
效果圖:

★解釋:star-six的div是一個正三角行,#star-six::after是一個倒三角形,通過絕對定位,調整其位置即可實現六角星的效果。
★擴展:CSS繪制圖形:https://css-tricks.com/the-shapes-of-css/
④不使用圖片創建小圖標
舉例:比如一個電話
很巧妙的應用一個div左border加圓角當機身,::before和::after配合圓角當聽筒。
示例如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style type="text/css"> #phone{ width:50px; height:50px; border-left:6px solid #EEB422; border-radius:20%; transform:rotate(-30deg); -webkit-transform:rotate(-30deg); margin:20px; margin-right:0px; position:relative; display: inline-block; top: -5px; } #phone:before{ width:15px; height:15px; background:#EEB422; border-radius: 20%; content: ""; position: absolute; left:-2px; top: 1px; } #phone:after{ width:15px; height:15px; background:#EEB422; border-radius: 20%; content: ""; position: absolute; left:-3px; top: 34px; } </style> </head> <body> <div id="wraper"> <div id="phone"></div> </div> </body> </html>
效果圖:

擴展:用偽元素創建了84種小圖標:http://nicolasgallagher.com/p...
⑤顯示打印網頁的URL
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> @media print { a[href]:after { content: " (" attr(href) ") "; } } </style> </head> <body> <a href="http://www.baidu.com">百度</a> </body> </html>
效果圖:

⑥給blockquote添加引號
經常用到給blockquote 引用段添加巨大的引號作為背景,可以用 ::before 來代替 background 。好處是即可以給背景留下空間,還可以直接使用文字而非圖片:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style type="text/css"> blockquote::before { content: open-quote; color: #ddd; z-index: -1; font-size:80px; } </style> </head> <body> <blockquote>引用一個段落,雙引號用::before偽元素實現</blockquote> </body> </html>
效果圖:

⑦超鏈接特效
舉例:配合 CSS定位實現一個鼠標移上去,超鏈接出現方括號的效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style type="text/css"> body{ background-color: #425a6c; } a { position: relative; display: inline-block; outline: none; color: #fff; text-decoration: none; font-size: 32px; padding: 5px 20px; } a:hover::before, a:hover::after { position: absolute; } a:hover::before { content: "\5B"; left: -10px; } a:hover::after { content: "\5D"; right: -10px; } </style> </head> <body> <a href="http://www.baidu.com">鼠標移上去出現方括號</a> </body> </html>
效果圖:

⑷content的值什么時候加引號,什么時候不加?
①動態的(會變的值)不加引號。
②媒體不加引號。
③固定的值、字符串需要加引號。
㈥:before詳細解釋
⑴如同對偽元素的名稱一樣,:before 是用來給指定的元素的內容前面插入新的內容
⑵示例:
.before:before{ content:'you before'; color:red; } <div class="before"> me</div>
⑶在這里我們給偽元素 :before 添加了屬性 content,並賦值為 you before。效果如下:

⑷//在指定元素的內容 me 前添加了新內容 you before
⑸這里通過偽元素 :before 添加的新內容區域默認的 display 屬性值為 inline,那么我們可不可以修改新內容區域的屬性,答案是肯定的。
⑹你可以像修改其他元素一樣修改它的樣式,我們來將它的 display 屬性值來改為 block。
.before:before{ content:'you before'; display:block; color:red; } <div class="before"> me</div>
⑺現在我們再來看下效果:

⑻//由偽元素 :before 生成新內容區域果然變為了塊元素
㈦具體示例
<html> <head> <meta charset="utf-8" /> <title>css偽元素:before和:after</title> <style type="text/css"> a.button { background-color: #F0F0F0 ; border: 1px solid #CCCCCC ; border-radius: 4px 4px 4px 4px ; color: #333333 ; display: block ; font-size: 30px ; padding: 6px 30px 4px 58px ; position: relative ; text-decoration: none ; width: 100px ; } a.button:before { background-color: #FF0066 ; border-radius: 3px 3px 3px 3px ; color: #FFFFFF ; content: "NEW" ; font-family: sans-serif ; font-size: 15px ; left: 9px ; line-height: 15px ; margin-top: -10px ; padding: 3px 3px 2px 3px ; position: absolute ; text-align: center ; top: 50% ; } a.button:after { background-color: #FFFFFF ; border: 1px solid #DADADA ; border-radius: 22px 22px 22px 22px ; color: #333333 ; content: "\00BB" ; display: none ; font-size: 30px ; height: 28px ; line-height: 26px ; margin-top: -14px ; position: absolute ; right: 6px ; text-align: center ; text-indent: 1px ; top: 50% ; width: 28px ; } a.button:hover:after { display: block ; } </style> </head> <body> <h1> css偽元素:before和:after </h1> <a href="#" class="button"> Button </a> </body> </html>
★代碼介紹:
⑴根據html標記,只有一個帶有文本“button”的按鈕。但是,當呈現頁面時,可以看到“new”標記:

⑵ 這個“new”標記是由:before偽元素提供的。但是,我們的css還有一個:after偽元素,初始顯示為“none”;這個偽元素設置為僅顯示在:hover:

參考:https://segmentfault.com/a/1190000000474414
https://segmentfault.com/a/1190000016236943#articleHeader5
https://www.bennadel.com/blog/2445-using-css-pseudo-elements-before-and-after.htm
