初識 css3中counter屬性

來源:互聯網
上載者:User

標籤:

最近看到counter屬性,好奇是做什麼用的,於是去查了查。

1.簡單介紹

counter是為css中插入計數器。【註明】在CSS2.1中counter()只能被使用在content屬性上。
關於瀏覽器安全色性可以看[這裡]

2.counter的作用

實際上是代替了javascript作為一種計數器工具,在css中使用。元素出現了幾次就預設增加多少對應值。增加值大小還可以由自己設定。

可以想象當我們設定好一次規矩之後,以後無論添加多少標籤,計數工具自動幫我們算計數,不必手動輸入那些值。而且這些不必藉助javascript,這是件多麼美妙的事情。

3.自我小例子

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>css3中 counter-increment counter-reset屬性</title>    <style type="text/css">        body        {            counter-reset:section;        }        h1        {            counter-reset:subsection;        }        h1:before        {            content:"Section " counter(section) ". ";            counter-increment:section;        }        h2:before        {            counter-increment:subsection;            content:counter(section) "." counter(subsection) " ";        }                h2{            counter-reset:subsubsection;        }        h3:before{            counter-increment: subsubsection;            content: counter(section) "." counter(subsection) "." counter(subsubsection) " " ;        }        h3{            counter-reset: subxsection;        }        h4:before{            counter-increment: subxsection;            content: counter(section) "." counter(subsection) "." counter(subsubsection) "." counter(subxsection);        }    </style></head><body>    <h1>上衣</h1>    <h2>T恤</h2>    <h2>襯衫</h2>    <h3>polo衫</h3>    <h1>褲子</h1>    <h2>牛仔褲</h2>    <h2>休閑褲</h2>    <h2>七分褲</h2>    <h3>按材質分</h3>    <h3>按牌子分</h3>    <h4>美特斯邦威</h4>    <h4>傑克瓊斯</h4>    <h4>裡維斯</h4>    <h4>優衣庫</h4></body></html>
View Code

 

初識 css3中counter屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.