CSS 中主要的偽元素有四個:before、after、first-letter、first-line。在 before/after 偽元素選擇器裡,有一個 content 屬性,能夠實現頁面內容的插入。
插入純文字
content:"插入的文章",或者 content:none 表示不插入內容。
HTML:
- <h1>這是h1</h1>
- <h2>這是h2</h2>
CSS:
- h1::after{
- content:"h1後插入內容"
- }
- h2::after{
- content:none
- }
運行結果:https://jsfiddle.net/dwqs/Lmm1r08x/
嵌入文字符號
可以使用 content 屬性的 open-quote 和 close-quote 值,在字符串兩邊添加諸如括號、單引號、雙引號之類的嵌套符號。open-quote 添加開始符號,close-quote 添加結束符號。修改上面的 CSS:
- h1{
- quotes:"(" ")"; /*利用元素的 quotes 屬性指定文字符號*/
- }
- h1::before{
- content:open-quote;
- }
- h1::after{
- content:close-quote;
- }
- h2{
- quotes:""" """; /*添加雙引號要轉義*/
- }
- h2::before{
- content:open-quote;
- }
- h2::after{
- content:close-quote;
- }
運行結果:https://jsfiddle.net/dwqs/p8e3qvv4/
插入圖片
content 屬性也可以直接在元素前後插入圖片。
HTML:
- <h3>這是h3</h3>
CSS:
h3::after{
content:url(https://ido321.qiniudn.com/wp-content/themes/yusi1.0/img/new.gif)
}
運行結果:https://jsfiddle.net/dwqs/c6qk6pkv/
插入元素的屬性值
content 屬性可以直接用 attr 獲取元素屬性,把它插入到對應位置。
HTML:
- <a href="https:///www.ido321.com">這是鏈接 </a>
CSS:
- a:after{
- content:attr(href);
- }
運行結果:https://jsfiddle.net/dwqs/m220nzan/
插入項目編號
利用 content 的 counter 屬性,可針對多個項目追加連續編號。
HTML:
- <h1>大標題</h1>
- <p>文字</p>
- <h1>大標題</h1>
- <p>文字</p>
- <h1>大標題</h1>
- <p>文字</p>
- <h1>大標題</h1>
- <p>文字</p>
CSS:
- h1:before{
- content:counter(my)'.';
- }
- h1{
- counter-increment:my;
- }
運行結果:https://jsfiddle.net/dwqs/2ueLg3uj/
項目編號修飾
默認插入的編號是 1、2、3…… 自動遞增的數字,也能給編號追加文字和樣式。沿用上面的 HTML,CSS 改為:
- h1:before{
- content:'第'counter(my)'章';
- color:red;
- font-size:42px;
- }
- h1{
- counter-increment:my;
- }
運行結果:https://jsfiddle.net/dwqs/17hqznca/
指定編號種類
利用 content:counter(計數器名, 編號種類) 的語法指定編號種類,種類參考 ul 的 list-style-type 屬性值。CSS 改為:
- h1:before{
- content:counter(my,upper-alpha);
- color:red;
- font-size:42px;
- }
- h1{
- counter-increment:my;
- }
運行結果:https://jsfiddle.net/dwqs/4nsrtxup/
編號嵌套
大編號中嵌套中編號,中編號中嵌套小編號。
HTML:
- <h1>大標題</h1>
- <p>文字1</p>
- <p>文字2</p>
- <p>文字3</p>
- <h1>大標題</h1>
- <p>文字1</p>
- <p>文字2</p>
- <p>文字3</p>
- <h1>大標題</h1>
- <p>文字1</p>
- <p>文字2</p>
- <p>文字3</p>
CSS:
- h1::before{
- content:counter(h)'.';
- }
- h1{
- counter-increment:h;
- }
- p::before{
- content:counter(p)'.';
- margin-left:40px;
- }
- p{
- counter-increment:p;
- }
運行結果:https://jsfiddle.net/dwqs/2k5qbz51/
可以看到 p 的編號是連續的。如果希望每個 h1 後面的三個 p 重新編號,可以用 counter-reset 重置,把 h1 的 CSS 改為:
- h1{
- counter-increment:h;
- counter-reset:p;
- }
這樣編號就重置了,看看結果:https://jsfiddle.net/dwqs/hfutu4Lq/
還可以實現更複雜的三層嵌套。
HTML:
- <h1>大標題</h1>
- <h2>中標題</h2>
- <h3>小標題</h3>
- <h3>小標題</h3>
- <h2>中標題</h2>
- <h3>小標題</h3>
- <h3>小標題</h3>
- <h1>大標題</h1>
- <h2>中標題</h2>
- <h3>小標題</h3>
- <h3>小標題</h3>
- <h2>中標題</h2>
- <h3>小標題</h3>
- <h3>小標題</h3>
CSS:
- h1::before{
- content:counter(h1)'.';
- }
- h1{
- counter-increment:h1;
- counter-reset:h2;
- }
- h2::before{
- content:counter(h1) '-' counter(h2);
- }
- h2{
- counter-increment:h2;
- counter-reset:h3;
- margin-left:40px;
- }
- h3::before{
- content:counter(h1) '-' counter(h2) '-' counter(h3);
- }
- h3{
- counter-increment:h3;
- margin-left:80px;
- }
運行結果:https://jsfiddle.net/dwqs/wuuckquy/