僞元素::before和::after的詳細介紹

㈠什麼是僞元素?javascript

不一樣的解釋:css

⑴僞元素是創造關於文檔語言可以指定的文檔樹以外的抽象。例如文檔語言不能提供訪問元素內容第一字或者第一行的機制。僞元素容許設計師引用它們,不然這是難以辦到的。僞元素還提供樣式設計師給在源文檔中不存在的內容分配樣式(例如::before和:after可以訪問產生的內容)。html

⑵CSS 在渲染文檔的時候,僞元素能夠經過 css 給 HTML 添加一個元素(叫標籤也行),這個元素在文檔樹中是找不到的。僞元素被當作 CSS 的樣式來進行展示,用法和普通的元素用法是同樣的。java

⑶僞元素用於建立一些不在文檔樹中的元素,併爲其添加樣式。好比說,咱們能夠經過:before來在一個元素前增長一些文本,併爲這些文本添加樣式。雖然用戶能夠看到這些文本,可是這些文本實際上不在文檔樹中。web

⑷CSS僞元素是用來添加一些選擇器的特殊效果。segmentfault

 

㈡::before 和 ::after的定義ui

⑴::before 建立一個僞元素,做爲已選中元素的第一個子元素,常經過 content 屬性來爲一個元素添加修飾性的內容。url

⑵::after 建立一個僞元素,做爲已選中元素的最後一個子元素,常經過 content 屬性來爲一個元素添加修飾性的內容。spa

 

㈢語法設計

/* 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>&emsp;二副</h2> <h2>&emsp;二副</h2> <h2>&emsp;二副</h2> <h3>&emsp;&emsp;三副</h3> <h3>&emsp;&emsp;三副</h3> <h3>&emsp;&emsp;三副</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:

 

參考:http://www.javashuo.com/article/p-agoyyisg-hg.html

           https://segmentfault.com/a/1190000016236943#articleHeader5

           https://www.bennadel.com/blog/2445-using-css-pseudo-elements-before-and-after.htm

相關文章