搜尋
首頁web前端css教學如何使用 @counter-style 規則使用 CSS 自訂清單項目?

如何使用 @counter-style 规则使用 CSS 自定义列表项?

清單是Web開發的基本部分,用於以有組織和結構化的方式呈現資訊。在HTML中,有3種類型的清單:有序列表、無序列表和定義列表。然而,當我們需要根據要求設計清單時,樣式化這些清單可能會具有挑戰性。 CSS提供了@counter-style規則,它允許我們以更靈活和創造性的方式自訂清單項目標記。

在本文中,我們將學習如何使用@counter-style規則來使用CSS自訂清單項目。 @counter-style規則用於定義不屬於預先定義樣式集的計數器樣式,並定義如何將計數器值轉換為字串表示。

什麼是@counter-style?

The @counter-style rule is used to define a counter style that can be used in conjunction with the CSS counter property. This rule is used to define a custom list item marker style. The counter properused to define a custom list item marker style. The counter property allows us tocrement increment a counter, which is used to generate content for pseudo-elements like :before and :after.

文法

@counter-style name {
   // write all the CSS styles properties and values
}

The name parameter is used to specify the name of the counter style that we are defining. Within the curly braces, we can define a set of properties and values that control the appearance of the counter. Some of the properties that use include −

  • System − 它指定要使用的編號系統,例如十進位、小寫字母、大寫羅馬數字等。

  • 符號 - 它指定了計數器每個層級使用的符號。

  • 後綴 − 它指定在計數器值之後添加的後綴。

  • Prefix − It specifies the prefix to add before the counter value.

  • Pad − 它指定在顯示計數器值時要使用的最小位元數。

Steps to use @counter-styles Rule in CSS

以下是CSS中使用@counter-styles規則的步驟 -

Step 1: Create an Ordered List

第一步是建立一個有序列表,並使用我們自己的列表項目標記進行自訂。在下面的範例中,我們希望使用羅馬數字而不是預設的編號系統。以下是我們清單的HTML程式碼 −

<ol>
   <li>Learn to code in python</li>
   <li>Learn to code in java</li>
   <li>Learn to code in c++</li>
</ol>

Step 2: Define the @counter-style

@counter-style my-numerals {
   system: upper-roman;
   symbols: I II III IV V VI VII VIII IX X;
}

In the above code, we have defined a counter style named my-numerals and set the system property to upper-roman which means the counter will be set to use the uppercase Roman numerpartals in the list. Apart from this, wewet also set the symbol's property to a string of Roman numerals from I to X.

Step 3: CSS Styles

ol {
   counter-reset: section;
}
li {
   counter-increment: section;
   list-style: none;
}
li:before {
   content: counter(section, my-numerals) ". ";
   margin-right: 10px;
}

在上述程式碼中,counter-reset屬性被設定為ol元素的section,這表示計數器將從0開始。在這裡,我們也為每個li元素設定了counter-increment屬性的section。

Example 1

的翻譯為:

範例1

#
<html>
<head>
   <title>Example to use @counter-style to customize the List Item Markers using CSS</title>
   <style>
      /* Defining a custom counter style to have the list as upper roman numerals */
      @counter-style roman-numerals {
         system: upper-roman;
         symbols: I II III IV V VI VII VIII IX X;
      }
      /* applying the custom counter style to the ordered list */
      ol {counter-reset: section; }
      /* incrementing the counter for each list item */
      li {counter-increment: section; list-style: none;}
      /* using the counter value to display the custom list item marker */
      li:before {
         content: counter(section, roman-numerals) ". ";
         margin-right: 8px;
         color: green;
         font-size: 15px;
      }
   </style>
</head>
<body>
   <h3 id="Example-to-use-counter-style-to-customize-the-List-Item-Markers-using-CSS">Example to use @counter-style to customize the List Item Markers using CSS</h3>
   <p>In this example, we have used the @counter-style rule to customize the list item markers of an ordered list.</p>
   <ol>
      <li>Learn to code in python</li>
      <li>Learn to code in java</li>
      <li>Learn to code in c++</li>
   </ol>
</body>
</html>

In the example above, we have defined a custom counter style named my-roman using the @counter-style rule. Here, we have set the system property to upper-roman to use the uppercase Roman numerals andymso set the use the uppercase Roman numerals andymso set the use. property to a string of Roman numerals from I to X.

在此之後,我們使用counter-reset屬性將自訂計數器樣式套用至有序列表,並使用counter-increment屬性為每個列表項目遞增計數器,並使用list-style屬性移除了預設的列表項標記。

最後,為了使用羅馬數字顯示自訂清單項目標記,我們使用了:before偽元素,透過content屬性和counter函數(有兩個參數:計數器的名稱(在本例中為section)和計數器樣式的名稱(在本例中為roman-numerals))。

Example 2

的中文翻譯為:

範例2

<html>
<head>
   <title>Example to use @counter-style to customize the List Item Markers using CSS</title>
   <style>
      /* removing the default list item markers */
      ul { list-style: none;}
      /* using images as list item markers */
      li:before {
         content: "";
         display: inline-block;
         width: 25px;
         height: 25px;
         background-image: url("yourimage.png");
         background-repeat: no-repeat;
         margin-right: 8px;
      }
   </style>
</head>
<body>
   <h3 id="Example-to-use-counter-style-to-customize-the-List-Item-Markers-using-CSS">Example to use @counter-style to customize the List Item Markers using CSS</h3>
   <p>In this example, we have used the @counter-style rule to customize the list item markers of an ordered list.</p>
   <ol>
      <li>Learn to code in python</li>
      <li>Learn to code in java</li>
      <li>Learn to code in c++</li>
   </ol>
</body>
</html>

在上面的範例中,我們使用了list-style屬性來刪除無序列表元素的預設清單項目標記。此外,我們也使用了:before偽元素來顯示列表項,借助content屬性和空字串。

我們已將display屬性設為inline-block,以確保圖像已正確顯示,並將寬度和高度屬性設為標記圖像的大小。我們使用background-image屬性指定標記影像的URL,並使用background-repeat屬性防止影像重複。最後,我們使用margin-right屬性在圖像右側添加了一些邊距。

Conclusion

在處理HTML清單時,可以使用CSS中的@counter-style規則來自訂清單項目標記的外觀。這個規則提供了一種簡單而靈活的方式來定義有序列表的自訂樣式。 @counter-style規則的語法包括幾個參數,如system、symbols、suffix、prefix和pad。這些參數允許對清單項目標記的外觀進行修改。使用@counter-style規則,可以更輕鬆地建立與目前專案設計需求相符的清單項目標記。

以上是如何使用 @counter-style 規則使用 CSS 自訂清單項目?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:tutorialspoint。如有侵權,請聯絡admin@php.cn刪除
使用GraphQL緩存使用GraphQL緩存Mar 19, 2025 am 09:36 AM

如果您最近開始使用GraphQL或審查了其優點和缺點,那麼您毫無疑問聽到了諸如“ GraphQl不支持緩存”或

優雅且酷的自定義CSS捲軸:展示櫃優雅且酷的自定義CSS捲軸:展示櫃Mar 10, 2025 am 11:37 AM

在本文中,我們將深入研究滾動條。我知道,這聽起來並不魅力,但請相信我,一個精心設計的頁面是齊頭並進的

使您的第一個自定義苗條過渡使您的第一個自定義苗條過渡Mar 15, 2025 am 11:08 AM

Svelte Transition API提供了一種使組件輸入或離開文檔(包括自定義Svelte Transitions)時動畫組件的方法。

展示,不要說展示,不要說Mar 16, 2025 am 11:49 AM

您花多少時間為網站設計內容演示文稿?當您撰寫新的博客文章或創建新頁面時,您是在考慮

使用Redwood.js和Fauna構建以太坊應用使用Redwood.js和Fauna構建以太坊應用Mar 28, 2025 am 09:18 AM

隨著最近比特幣價格超過20k美元的攀升,最近打破了3萬美元,我認為值得深入研究創建以太坊

NPM命令是什麼?NPM命令是什麼?Mar 15, 2025 am 11:36 AM

NPM命令為您運行各種任務,無論是一次性或連續運行的過程,例如啟動服務器或編譯代碼。

讓我們使用(x,x,x,x)來談論特殊性讓我們使用(x,x,x,x)來談論特殊性Mar 24, 2025 am 10:37 AM

前幾天我只是和埃里克·邁耶(Eric Meyer)聊天,我想起了我成長時代的埃里克·邁耶(Eric Meyer)的故事。我寫了一篇有關CSS特異性的博客文章,以及

您如何使用CSS創建文本效果,例如文本陰影和漸變?您如何使用CSS創建文本效果,例如文本陰影和漸變?Mar 14, 2025 am 11:10 AM

文章討論了使用CSS來獲得陰影和漸變等文本效果,優化它們以進行性能並增強用戶體驗。它還列出了初學者的資源。(159個字符)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中