Skip to content

Commit

Permalink
Counter descriptor: pad (#29820)
Browse files Browse the repository at this point in the history
* Counter descriptor: pad

* Apply suggestions from code review

Co-authored-by: Dipika Bhattacharya <dipika@foss-community.org>

* Update files/en-us/web/css/@counter-style/pad/index.md

* Update index.md

---------

Co-authored-by: Dipika Bhattacharya <dipika@foss-community.org>
  • Loading branch information
estelle and dipikabh authored Oct 27, 2023
1 parent fdb4929 commit de1b7bb
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions files/en-us/web/css/@counter-style/pad/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,31 @@ browser-compat: css.at-rules.counter-style.pad

{{CSSRef}}

The **`pad`** descriptor can be used with custom counter style definitions when you need the marker representations to have a minimum length.
The **`pad`** descriptor of the {{cssxref("@counter-style")}} at-rule is used to set a minimum length for marker representations.

## Syntax

```css
pad: 3 "0";
pad: "+" 5;
```

### Values

- `<integer> && <symbol>`
- : The `<integer>` specifies a minimum length that all counter representations must reach. The value must be non-negative. If the minimum length is not reached, the representation will be padded with the specified `<symbol>`.
The descriptor accepts the following two values, which are separated by a space and can be specified in any order:

- {{cssxref("integer")}}

- : Specifies the minimum length that all marker representations must reach. The value must be non-negative. In the case of the `pad` descriptor, this value is also known as the _pad length_.

- [`<symbol>`](/en-US/docs/Web/CSS/@counter-style/symbols#symbol)
- : Specifies the symbol to be used for padding if the minimum length defined by the `<integer>` is not reached. In the case of the `pad` descriptor, this value is also known as the _padding symbol_.

## Description

If a marker representation is smaller than the specified pad length, then the marker will be padded with the specified pad symbol. Marker representations longer than the pad length are constructed as normal. Pad descriptor takes the minimum marker length as an integer and a symbol to be used for padding as the second parameter. A common usage of the pad descriptor is when you need your list to start numbering from 01 and go through 02, 03 and so on, instead of just 1, 2, 3…
Use the `pad` descriptor when you need the marker representations to be of a minimum length. If a marker representation is shorter than the specified pad length, then the marker representation will be padded with the specified padding symbol. Marker representations longer than the pad length are displayed without any additional padding.

The `pad` descriptor takes an `<integer>` for the minimum marker length and a `<symbol>` for the padding. A common use case of the `pad` descriptor is when you need a list to start numbering from `01` and go through `02`, `03`, `04`, and so on, instead of just `1`, `2`, `3`, and `4`. By specifying the `pad` descriptor as `pad: 2 "0"` in this case, the browser ensures to make the counter at least two characters long and adds a padding with `0` to reach the minimum two-character length where needed. Counters that are already two or more characters long in this example will be displayed normally, without padding.

## Formal definition

Expand All @@ -36,35 +45,40 @@ If a marker representation is smaller than the specified pad length, then the ma

### Padding a counter

This example extends the `decimal` {{cssxref("@counter-style/system","system")}} to create a counter that is at least three characters long, padding shorter counters with `0` to reach that minimum length. A {{cssxref("@counter-style/suffix", "suffix")}} descriptor has been added to make the output more legible.

#### HTML

```html
<ul class="list">
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
<li value="40">Forty</li>
<li>Forty-one</li>
<li value="200">Two hundred</li>
<li value="3000">Three thousand</li>
<li>and so on</li>
</ul>
```

#### CSS

```css
@counter-style pad-example {
system: numeric;
symbols: "0" "1" "2" "3" "4" "5";
pad: 2 "0";
system: extends decimal;
suffix: ": ";
pad: 3 "0";
}

.list {
ul {
list-style: pad-example;
}
```

#### Result

{{ EmbedLiveSample('Padding a counter') }}
{{ EmbedLiveSample('Padding a counter', '100', '200') }}

## Specifications

Expand All @@ -76,5 +90,7 @@ If a marker representation is smaller than the specified pad length, then the ma

## See also

- {{Cssxref("list-style")}}, {{Cssxref("list-style-image")}}, {{Cssxref("list-style-position")}}
- {{cssxref("symbols", "symbols()")}}, the functional notation creating anonymous counter styles.
- {{cssxref("@counter-style")}} descriptors: {{cssxref("@counter-style/system","system")}}, {{cssxref("@counter-style/symbols", "symbols")}}, {{cssxref("@counter-style/additive-symbols", "additive-symbols")}}, {{cssxref("@counter-style/negative", "negative")}}, {{cssxref("@counter-style/prefix", "prefix")}}, {{cssxref("@counter-style/suffix", "suffix")}}, {{cssxref("@counter-style/range", "range")}}, {{cssxref("@counter-style/speak-as", "speak-as")}}, {{cssxref("@counter-style/fallback", "fallback")}}
- List style properties: {{Cssxref("list-style")}}, {{Cssxref("list-style-image")}}, {{Cssxref("list-style-position")}}
- {{cssxref("symbols", "symbols()")}} function to create anonymous counter styles
- [CSS counter styles](/en-US/docs/Web/CSS/CSS_counter_styles) module

0 comments on commit de1b7bb

Please sign in to comment.