Saltar al contenido

Propiedad CSS counter-increment

La propiedad counter-increment define cuánto deben aumentar o disminuir los valores de los contadores. Esta propiedad se utiliza junto con las propiedades content y counter-reset.

La propiedad counter-increment se especifica mediante dos valores: none y números de identificador (id). "None" es el valor predeterminado de esta propiedad. Permite usar valores negativos en el caso del valor "id number". El incremento predeterminado es 1. Si el identificador del contador no se inicializa con counter-reset, el valor predeterminado es 0. El valor del contador se puede establecer en un número arbitrario con la propiedad counter-reset.

Valor inicialnone
Se aplica aTodos los elementos.
HeredableNo.
AnimableDiscreto.
VersiónCSS2
Sintaxis DOMobject.style.counterIncrement = "subsection";

Sintaxis

Sintaxis de la propiedad CSS counter-increment

css
counter-increment: none | id number | initial | inherit;

Ejemplo de la propiedad counter-increment:

Ejemplo de la propiedad CSS counter-increment

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      body {
        /* "my-counter" to 0 */
        counter-reset: my-counter;
      }
      h2::before {
        /* "my-counter" by 1 */
        counter-increment: my-counter;
        content: "Section " counter(my-counter) ". ";
      }
    </style>
  </head>
  <body>
    <h2>HTML Tutorial</h2>
    <h2>CSS Tutorial</h2>
    <h2>JavaScript Tutorial</h2>
    <h2>PHP Tutorial</h2>
  </body>
</html>

Resultado

Propiedad CSS counter-increment

Ejemplo de la propiedad counter-increment con secciones y subsecciones numeradas:

Propiedad CSS counter-increment con valor de subsección

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      body {
        /* Set "section" to 0 */
        counter-reset: section;
      }
      h2 {
        /* Set "subsection" to 0 */
        counter-reset: subsection;
      }
      h2::before {
        counter-increment: section;
        content: "Book " counter(section) ": ";
      }
      h3::before {
        counter-increment: subsection;
        content: counter(section) "." counter(subsection) " ";
      }
    </style>
  </head>
  <body>
    <h2>HTML</h2>
    <h3>HTML Basics</h3>
    <h3>HTML Templates</h3>
    <h3>HTML References</h3>
    <h3>HTML Tags</h3>
    <h2>CSS</h2>
    <h3>CSS Basics</h3>
    <h3>CSS References</h3>
    <h3>CSS Advanced</h3>
    <h3>CSS Guides</h3>
    <h3>CSS Selectors</h3>
    <h3>CSS Properties</h3>
  </body>
</html>

Ejemplo de la propiedad counter-increment con números romanos:

Ejemplo de la propiedad counter-increment con números romanos:

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      body {
        /* "my-counter" to 0 */
        counter-reset: my-counter;
      }
      h2::before {
        /* "my-counter" by 1 */
        counter-increment: my-counter;
        content: counter(my-counter, upper-roman) ". ";
      }
    </style>
  </head>
  <body>
    <h2>HTML Tutorial</h2>
    <h2>CSS Tutorial</h2>
    <h2>JavaScript Tutorial</h2>
    <h2>PHP Tutorial</h2>
  </body>
</html>

Valores

ValorDescripción
noneLos contadores no se incrementan. Este es el valor predeterminado.
id numberId define qué contador incrementar. Number define cuánto se incrementará el contador.
initialEstablece la propiedad en su valor predeterminado.
inheritHereda la propiedad de su elemento padre.

Práctica

¿Cuál es la función de la propiedad 'counter-increment' en CSS?

¿Te resulta útil?

Vista previa dual-run — compárala con las rutas Symfony en producción.