DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> DIV十CSS布局 >> 布局實例
CSS3 content 屬性實戰:插入文字、圖片、屬性與編號
編輯:布局實例     

CSS 中主要的偽元素有四個:before、after、first-letter、first-line。在 before/after 偽元素選擇器裡,有一個 content 屬性,能夠實現頁面內容的插入。

插入純文字

content:"插入的文章",或者 content:none 表示不插入內容。

HTML:

  1. <h1>這是h1</h1>
  2. <h2>這是h2</h2>

CSS:

  1. h1::after{
  2. content:"h1後插入內容"
  3. }
  4. h2::after{
  5. content:none
  6. }

運行結果:https://jsfiddle.net/dwqs/Lmm1r08x/

嵌入文字符號

可以使用 content 屬性的 open-quote 和 close-quote 值,在字符串兩邊添加諸如括號、單引號、雙引號之類的嵌套符號。open-quote 添加開始符號,close-quote 添加結束符號。修改上面的 CSS:

  1. h1{
  2. quotes:"(" ")"; /*利用元素的 quotes 屬性指定文字符號*/
  3. }
  4. h1::before{
  5. content:open-quote;
  6. }
  7. h1::after{
  8. content:close-quote;
  9. }
  10. h2{
  11. quotes:""" """; /*添加雙引號要轉義*/
  12. }
  13. h2::before{
  14. content:open-quote;
  15. }
  16. h2::after{
  17. content:close-quote;
  18. }

運行結果:https://jsfiddle.net/dwqs/p8e3qvv4/

插入圖片

content 屬性也可以直接在元素前後插入圖片。

HTML:

  1. <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:

  1. <a href="https:///www.ido321.com">這是鏈接 </a>

CSS:

  1. a:after{
  2. content:attr(href);
  3. }

運行結果:https://jsfiddle.net/dwqs/m220nzan/

插入項目編號

利用 content 的 counter 屬性,可針對多個項目追加連續編號。

HTML:

  1. <h1>大標題</h1>
  2. <p>文字</p>
  3. <h1>大標題</h1>
  4. <p>文字</p>
  5. <h1>大標題</h1>
  6. <p>文字</p>
  7. <h1>大標題</h1>
  8. <p>文字</p>

CSS:

  1. h1:before{
  2. content:counter(my)'.';
  3. }
  4. h1{
  5. counter-increment:my;
  6. }

運行結果:https://jsfiddle.net/dwqs/2ueLg3uj/

項目編號修飾

默認插入的編號是 1、2、3…… 自動遞增的數字,也能給編號追加文字和樣式。沿用上面的 HTML,CSS 改為:

  1. h1:before{
  2. content:'第'counter(my)'章';
  3. color:red;
  4. font-size:42px;
  5. }
  6. h1{
  7. counter-increment:my;
  8. }

運行結果:https://jsfiddle.net/dwqs/17hqznca/

指定編號種類

利用 content:counter(計數器名, 編號種類) 的語法指定編號種類,種類參考 ul 的 list-style-type 屬性值。CSS 改為:

  1. h1:before{
  2. content:counter(my,upper-alpha);
  3. color:red;
  4. font-size:42px;
  5. }
  6. h1{
  7. counter-increment:my;
  8. }

運行結果:https://jsfiddle.net/dwqs/4nsrtxup/

編號嵌套

大編號中嵌套中編號,中編號中嵌套小編號。

HTML:

  1. <h1>大標題</h1>
  2. <p>文字1</p>
  3. <p>文字2</p>
  4. <p>文字3</p>
  5. <h1>大標題</h1>
  6. <p>文字1</p>
  7. <p>文字2</p>
  8. <p>文字3</p>
  9. <h1>大標題</h1>
  10. <p>文字1</p>
  11. <p>文字2</p>
  12. <p>文字3</p>

CSS:

  1. h1::before{
  2. content:counter(h)'.';
  3. }
  4. h1{
  5. counter-increment:h;
  6. }
  7. p::before{
  8. content:counter(p)'.';
  9. margin-left:40px;
  10. }
  11. p{
  12. counter-increment:p;
  13. }

運行結果:https://jsfiddle.net/dwqs/2k5qbz51/

可以看到 p 的編號是連續的。如果希望每個 h1 後面的三個 p 重新編號,可以用 counter-reset 重置,把 h1 的 CSS 改為:

  1. h1{
  2. counter-increment:h;
  3. counter-reset:p;
  4. }

這樣編號就重置了,看看結果:https://jsfiddle.net/dwqs/hfutu4Lq/

還可以實現更複雜的三層嵌套。

HTML:

  1. <h1>大標題</h1>
  2. <h2>中標題</h2>
  3. <h3>小標題</h3>
  4. <h3>小標題</h3>
  5. <h2>中標題</h2>
  6. <h3>小標題</h3>
  7. <h3>小標題</h3>
  8. <h1>大標題</h1>
  9. <h2>中標題</h2>
  10. <h3>小標題</h3>
  11. <h3>小標題</h3>
  12. <h2>中標題</h2>
  13. <h3>小標題</h3>
  14. <h3>小標題</h3>

CSS:

  1. h1::before{
  2. content:counter(h1)'.';
  3. }
  4. h1{
  5. counter-increment:h1;
  6. counter-reset:h2;
  7. }
  8. h2::before{
  9. content:counter(h1) '-' counter(h2);
  10. }
  11. h2{
  12. counter-increment:h2;
  13. counter-reset:h3;
  14. margin-left:40px;
  15. }
  16. h3::before{
  17. content:counter(h1) '-' counter(h2) '-' counter(h3);
  18. }
  19. h3{
  20. counter-increment:h3;
  21. margin-left:80px;
  22. }

運行結果:https://jsfiddle.net/dwqs/wuuckquy/

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved