Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(css): clarify counter-set vs counter-reset #35258

Merged
merged 4 commits into from
Aug 29, 2024

Conversation

OnkarRuikar
Copy link
Contributor

The PR:

  • Highlights the difference between counter-set and counter-reset properties using an example.
  • Corrects the wrong wording for both properties in various places.
  • Adds example for reversed() counter.

@OnkarRuikar OnkarRuikar requested a review from a team as a code owner July 31, 2024 15:01
@OnkarRuikar OnkarRuikar requested review from bsmth and removed request for a team July 31, 2024 15:01
@github-actions github-actions bot added the Content:CSS Cascading Style Sheets docs label Jul 31, 2024
Copy link
Contributor

github-actions bot commented Jul 31, 2024

@github-actions github-actions bot added the size/m [PR only] 51-500 LoC changed label Jul 31, 2024
Comment on lines +60 to +61
> [!WARNING]
> There is [a difference between `counter-reset` and `counter-set` properties](/en-US/docs/Web/CSS/CSS_counter_styles/Using_CSS_counters#difference_between_counter-set_and_counter-reset). After creating a counter using `counter-reset`, you can adjust its value by using the {{cssxref("counter-set")}} property. This is counterintuitive because, despite its name, the `counter-reset` property is used for creating and initializing counters, while the `counter-set` property is used for resetting the value of an existing counter.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this warning. You can reset a counter using:

h1::before {
    content: "Chapter " counter(chapter) ". ";
    counter-increment: chapter;  /* Add 1 to chapter */
    counter-reset: section;      /* Set section to 0 */
}

See https://drafts.csswg.org/css-lists/#example-838dca3e

So it might be better to say counter-set is good for directly setting values (like skipping increments), and counter reset is for initializing and resetting.

I would keep "After creating a counter using counter-reset, you can adjust its value by using the {{cssxref("counter-set")}} property." but I wouldn't mention the others, especially "counter-set property is used for resetting".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the spec example has a typo, and it should use counter-set instead.

counter reset is for initializing and resetting.

The spec doesn't say counter-reset updates the existing counter if a counter with the same name exists. So counter-reset can't actually reset the same counter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, it's in https://drafts.csswg.org/css-lists/#increment-set

Manipulating Counter Values: the counter-increment and counter-set properties

dear oh dear 😅


{{EmbedLiveSample("Difference between counter-set and counter-reset", "100%", 300)}}

Notice how the first sub-list items start receiving numbers starting from `11`, and the numbering is continued in the parent list. This is because the `counter-set` property updates the same 'item' counter. Then notice how the second sub-list items receive new numbering starting from '1' and the parent list items after it don't carry forward the numbering. This is because the `counter-reset` property created a new counter with the same name so the parent list items kept using the old counter.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow this explanation, specifically "creating a new counter of the same name". Have you had a look at this one: https://drafts.csswg.org/css-lists/#counter-nesting-example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😕 Yes, the spec section does say that, instantiating a new counter on an element, which inherited an identically-named counter from its parent, creates a new counter of the same name, nested inside the existing counter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I re-read and I understand now, sorry for picking on that

@OnkarRuikar OnkarRuikar marked this pull request as draft August 2, 2024 03:52
@OnkarRuikar OnkarRuikar marked this pull request as ready for review August 15, 2024 08:14
@OnkarRuikar OnkarRuikar requested a review from bsmth August 15, 2024 08:14
Copy link
Member

@bsmth bsmth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Onkar, I think we can land the changes 👍🏻

@bsmth bsmth merged commit cdc0015 into mdn:main Aug 29, 2024
8 checks passed
@OnkarRuikar OnkarRuikar deleted the css_counter_reset branch August 29, 2024 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:CSS Cascading Style Sheets docs size/m [PR only] 51-500 LoC changed
Projects
None yet
2 participants