當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


CSS counter-reset用法及代碼示例


CSS中的counter-reset屬性用於為元素創建或重置CSS計數器。該屬性與counter-increment屬性和content屬性一起使用。

用法:

counter-reset = none|name number|initial|inherit;

屬性值:


  • none:它是默認值。此值不會重置計數器。
  • name number:每次出現元素時重置計數器的值。如果未指定,則默認值為0。
  • initial:它將counter-reset屬性設置為其默認值。
  • inherit:元素從其父元素繼承屬性。

範例1:本示例使用counter-reset屬性創建部分。

<!DOCTYPE html> 
<html> 
      
<head> 
      
    <!-- CSS property to set counter-reset property -->
    <style> 
          
        /* set chapter counter to 0*/ 
        body { 
            counter-reset:chapter;      
        } 
        .chapter:before { 
            content:counter(chapter) ". "; 
            display:inline; 
        } 
        .chapter { 
              
            /* Increment the chapter counter by 1, 
            same as counter-increment:chapter 1; */ 
            counter-increment:chapter; 
              
            /* set section counter to 0 */ 
            counter-reset:section;      
            font-size:20px; 
            font-weight:bold; 
        } 
    </style> 
</head> 
  
<body> 
    <h1>GeeksforGeeks</h1> 
      
    <div class = "chapter">HTML</div> 
    <div class = "chapter">CSS</div> 
    <div class = "chapter">PHP</div> 
</body>                     
  
</html>

輸出:

範例2:本示例使用counter-reset屬性創建節和小節。

<!DOCTYPE html> 
<html> 
      
<head> 
      
    <!-- CSS style to create counter-reset property -->
    <style> 
        body { 
            counter-reset:section; 
        } 
        h1 { 
            counter-reset:category; 
        } 
        h1:before { 
            counter-increment:section; 
            content:"Section " counter(section) ". "; 
        } 
        h3:before { 
            counter-increment:category; 
            content:counter(section) "." counter(category) " "; 
        } 
    </style> 
</head> 
  
<body> 
    <h1>GeeksforGeeks</h1> 
      
    <h3>HTML</h3> 
    <h3>CSS </h3> 
      
    <h1>References</h1> 
      
    <h3>HTML Tags</h3> 
    <h3>CSS Properties</h3> 
</body> 
  
</html>                    

輸出:

支持的瀏覽器:下麵列出了counter-reset屬性支持的瀏覽器:

  • 穀歌瀏覽器4.0
  • Internet Explorer 8.0
  • Firefox 2.0
  • Safari 3.1
  • Opera 9.6


相關用法


注:本文由純淨天空篩選整理自ChinkitManchanda大神的英文原創作品 CSS | counter-reset Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。