content搭配:before和:after這兩個偽元素,可以插入指定的內容
為了相容於IE8,要使用單冒號的:before,不能使用雙冒號::before
可插入的 Property Values
normal
預設值。可以將原本有插入的content,還原為normal,也就是預設的“none”(nothing 什麼都沒有)
html:
1 | <p>My name is Niga Higa</p> |
css:
1 | p:before{ |
results:

none
將原本有插入的內容移除掉
html:
1 | <p>My name is Niga Higa</p> |
css:
1 | p:before{ |
results:

counter
Sets the content as a counter
[作法]
-
先建立一個變數(myNumber),讓它隨著p元素每一次出現而遞增
-
然後在每個p元素前,插入這個
counter
html:
1 | <p>First make a variable (myNumber) and make it increase every time a p element occurs.</p> |
css:
1 | p{ |
results:

attr(attribute)
將「屬性」作為插入的內容
範例一
html:
1 | <a href="https://www.airbnb.com.tw">(Airbnb)</a> |
css:
1 | a:after{ |
results:

範例二
html:
1 | <a href="https://www.tripadvisor.com.tw">TripAdvisor</a> |
css:
1 | a:after{ |
results:

文字string
插入指定的文字
html:
1 | <p>My name is Ryan</p> |
css:
1 | p:before{ |
results:

前引號open-quote
插入前引號
html:
1 | <p>My name is Ryan</p> |
css:
1 | p:before{ |
results:

後引號close-quote
插入後引號
沒有「前引號」,就不能插入「後引號」
html:
1 | <p>My name is Ryan</p> |
css:
1 | p:before{ |
results:

移除前後引號
將插入的前、後引號移除
html:
1 | <p>My name is Ryan</p> |
css:
1 | p:before{ |
results:

url(url)
可以用網址插入多媒體(圖片、聲音檔、影片等)
html:
1 | <p>My name is Zoe</p> |
css:
1 | p:after{ |
results:

自訂bullets的顏色[額外補充]
先移除ul或ol預設的bullets後,再自己插入一個看起來像bullets的點點(•),並自訂顏色:
html:
1 | <ul> |
css:
1 | /* 先移除預設的bullets */ |
results:

教學網址
https://www.w3schools.com/cssref/pr_gen_content.asp
我的codepen練習
https://codepen.io/saffranwang/pen/PgKpmR