Heim  >  Artikel  >  Web-Frontend  >  Wie erstelle ich Zahlen mit der Eigenschaft „Inverses Inkrement“ in CSS?

Wie erstelle ich Zahlen mit der Eigenschaft „Inverses Inkrement“ in CSS?

王林
王林nach vorne
2023-09-06 12:29:021079Durchsuche

如何使用 CSS 中的反增量属性创建编号?

CSS 中给出的“counter-increment”属性用于增加或减少我们可以使用 CSS 在网页上显示的计数器值。当我们想要计算网页中 HTML 元素的出现次数时,CSS 计数器非常有用。

我们还将在此处使用“counter-reset”CSS 属性,这有助于我们将 CSS 计数器值重置或初始化为所需的数字。

语法

  • 反自增 -

css-counter increment-value;

这里,css-counter是指CSS中声明的计数器变量,increment-value是指要增加或减少CSS计数器的值。

  • 计数器重置:css 计数器重置值。

这里,css-counter是指在CSS中声明的计数器变量,reset-value是指您想要将计数器重置为的值。

示例 1

在此示例中,我们将创建一个 CSS 计数器来计算“p”标签的出现次数并在网页上显示其计数。我们将初始计数设置为 0,并在每次出现“p”标签时将计数器增加 1。

<html lang="en">
<head>
   <title>How to create numbering using counter-increment property in CSS?</title>
   <style>
      body {
         counter-reset: p-count;
      }
      p {
         counter-increment: p-count;
      }

      p::before {
         content: counter(p-count) ". ";
      }
   </style>
</head>
<body>
   <h3>How to create numbering using counter-increment property in CSS?</h3>
   <p>First paragraph</p>
   <p>Second paragraph</p>
</body>
</html>

示例 2

在此示例中,我们将创建 2 个 CSS 计数器来计算“p”标签的出现次数并将其计数显示在网页上。我们将创建一个嵌套列表结构,首先我们将以数字形式显示“p”标签的数量,然后以罗马数字格式显示“子”类“p”标签的数量。

<html lang="en">
<head>
   <title>How to create numbering using counter-increment property in CSS?</title>
   <style>
      body {
         counter-reset: p-count;
      }
      .sub {
         counter-reset: sub-count;
         margin-left: 10px;
      }
      .par {
         counter-increment: p-count;
      }
      .child {
         counter-increment: sub-count;
      }

      .par::before {
         content: counter(p-count) ". ";
      }
      .child::before {
         content: counter(p-count, lower-roman) ". ";
      }
   </style>
</head>
<body>
   <h3>How to create numbering using counter-increment property in CSS?</h3>
   <p class="par">First paragraph</p>
   <p class="par">Second paragraph</p>
   <div class="sub">
      <p class="child">Sub first</p>
      <p class="child">Sub second</p>
   </div>
</body>
</html>

结论

在本文中,我们学习了如何使用 CSS 提供的“计数器重置”和“计数器增量”属性来创建编号并将其显示在网页上。 “couter-reset”帮助我们将计数器重置为特定值,“counter-increment”帮助我们将计数器增加或减少特定值。

Das obige ist der detaillierte Inhalt vonWie erstelle ich Zahlen mit der Eigenschaft „Inverses Inkrement“ in CSS?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:tutorialspoint.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen
Vorheriger Artikel:CSS-RasterlayoutNächster Artikel:CSS-Rasterlayout