-
Notifications
You must be signed in to change notification settings - Fork 22.5k
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
Conversation
Preview URLs
(comment last updated: 2024-08-29 13:54:47) |
files/en-us/web/css/css_counter_styles/using_css_counters/index.md
Outdated
Show resolved
Hide resolved
> [!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. |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 😅
files/en-us/web/css/css_counter_styles/using_css_counters/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/css/css_counter_styles/using_css_counters/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/css/css_counter_styles/using_css_counters/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/css/css_counter_styles/using_css_counters/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/css/css_counter_styles/using_css_counters/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/css/css_counter_styles/using_css_counters/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/css/css_counter_styles/using_css_counters/index.md
Outdated
Show resolved
Hide resolved
|
||
{{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. |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Co-authored-by: Brian Thomas Smith <brian@smith.berlin>
5d652d2
to
cb6099c
Compare
There was a problem hiding this 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 👍🏻
The PR:
counter-set
andcounter-reset
properties using an example.reversed()
counter.