標籤:
最近看到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屬性