-
Notifications
You must be signed in to change notification settings - Fork 719
[css-values] enable the use of counter() inside calc() #1026
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
Comments
Relevant section of the spec, which includes an inline issue discussing possible use cases. But getting an issue on GitHub will hopefully provide an easier forum for other people to mention their use cases and/or possible implementation issues. |
Hi, ran into a case today where this would have helped, so posting a comment here. I'm setting up some CSS for an image/logo slider. While I know right now that there are 8 images, I don't know how many there will be in the future (nor do I wish to have to know). I'm using only CSS to animate it, and it would be useful to be able to say "for each one of the items that exist (, increment this property by x."
|
The problem with this proposal is that So I think we need some way to get the numeric value of a counter, either by adding some parameter to |
JFTR, I would hate to see |
Yeah, an explicit parsing function is probably bad. No real need for it. But having something that can retrieve a counter value as a number (rather than going to the extra effort of formatting it as a string) would work. (Same as how |
Wouldn't allowing |
@upsuper Fair point, counter values are inherited from the immediately preceding element in document order instead of from the parent element as usual. So maybe it would be better to add a However, note that the CSS Lists draft currently allows |
Firstly, there are a lot fewer places where Also, allowing |
It may not be as bad, but it could still be very bad. One issue comes to my mind is that I guess we should start this topic in a separate issue, now. |
I would really, really love to have counter's value available inside |
Two more use cases:
Regarding syntax, I like @tabatkins's proposal for a new formatting argument, if an explicit syntax is required. |
My sad excuse of a utility without this feature... ...
.count--3 { --count: 3 }
.count--4 { --count: 4 }
.count--5 { --count: 5 }
...
.count > :nth-child(1) { --count-current: 1 }
.count > :nth-child(2) { --count-current: 2 }
.count > :nth-child(3) { --count-current: 3 }
...
.el {
animation-delay: calc((var(--count, 0) - var(--count-current, 0)) * 0.1s);
} |
I like this in general and I'd love to see it implemented too. But I'd really, really hate implementing it. All of the paged media layout engines have the same problem with counters, in particular "page" and "pages", although technically it's any counters incremented in the @page margin areas (still a largely theoretical constraint at the time of writing). The moment you hit one of these you have to consider a second layout pass - it's not always required, but for something like You need the first layout pass to count the pages, or to work out with certainty which page your element is on (that's more of an issue with It's not quite as awful for the "page" counter, or any counter other than "pages". But it's still a little complex: So although I'm not exactly against this, I just wanted to flag that it is almost unfeasibly hairy for some cases. Think of the multiple passes required to stabilise layout for ::first-line, except it's a whole document that needs to be stabilised. These would need to be considered if this goes anywhere. |
Won’t we get this for free once we have a function to convert between types, the likes of which has been discussed multiple times? |
Hey all, as the scope of this issue has changed quite a bit from its original concept/spec, it may be worth considering changing the name on the ticket, so it can be found more easily. I essentially duplicated the more recent evolution of this issue in #8981. @tabatkins made a good point in my related ticket that using One possibility would be to introduce a second optional argument to element:nth-child(n + 1, --n) {
transition: /* ... */;
--delay: calc(var(--n) * 200ms);
transition-delay: var(--delay);
} In either case, we should still discuss all the pros/cons/implications of allowing selectors to also be property-declarative in this way, as this would set a very new precedent not before used in CSS afaik. Another consideration of using a second argument in those pseudo-selector expressions for this is that it would also beg the question, "Would each element:nth-child(n, --m):nth-child(n + 1, --n) {
/* are `m` and `n` equal?
it's a bit unclear. */
} Alternatively, should an iterator require a completely different syntax altogether, such as the following example, where the custom property name is used completely outside the expression itself, after the selector and before the style block? element:nth-child(n):nth-child(n + 1) --n {
transition: /* ... */;
--delay: calc(var(--n) * 200ms);
transition-delay: var(--delay);
} |
@brandonmcconnell, custom properties are intentionally left untouched by the language, so if provide this constant as a variable, then it should be done at the property declaration level: for example, some special value or function |
@rthrejheytjyrtj545 That makes sense. In that case, it would probably make the most sense as a |
Counters can be modified within all the subtree in abitrary ways. If you just want the element index and sibling count, refer to #4559 instead. |
@Loirooriol Thanks for pointing me to that issue. Adding my thoughts there. 🙂 |
It's not possible for Background: I wanted to initialize a counter using the Related: Has there been discussion anywhere about permitting |
@tavin |
@Loirooriol we can debate whether |
Doesn't seem much likely to happen due to #1026 (comment) |
Could something akin to setting up a particular scope for a certain counter help with that? Similar to how we can use |
Alternatively maybe some sort of nth-usage inside calc?
|
Thanks for the update re: |
@alystair For randomness, see https://drafts.csswg.org/css-values-5/#randomness For |
|
Quite recently we got an upgrade to the counter(counterName type( Reading through the comments of this issue, I understand that sometimes you can end up in a loop which is undesirable. animation-delay: calc( counter(item) type( Here the returned value of Personally, I would prefer the first option. |
Chrome intends to ship https://groups.google.com/a/chromium.org/g/blink-dev/c/opsqx1cBPyc/m/pJaRwh7UAAAJ |
The feature is still some ways of reaching its full potential. Whenever the style queries start processing the values of custom properties, we could have something like this with the new ternary operator <body>
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
body> body {
div {
--number:sibling-index();
--min: 5;
--max: 7;
--value: clamp(0, min(
calc((var(--number) - var(--min)) * 1000),
calc((var(--max) - var(--number)) * 1000)
), 1);
background:if(style(--value: 1): blue; else: red);
}
} And you could style ranges of pages "relatively" easily. This doesn't work because right now style() queries do not compute the custom property, just evaluate the value as a string, so they evaluate the clamp as a string vs the integer as a string. I hope this is changed in the future. |
@5e-Cleric This already works in Chromium Canary, just register the property as |
Damn, spent 2h yesterday figuring this out and trying different combinations, i was using counters before i found sibling-index() and custom properties were messing assignment somehow, so i figured they would not allow for the interpretation! Thanks for that, then, it is indeed a great addition with lots of potential. (This example in particular was thought because i create documents with markdown+css, and i wanted an easy programatic way to get sections styled) |
Feature request.
It would be nice to be able to use the counter() function inside of calc() function.
That would enable new possibilities on layouts.
I copy here the link to a thread which proposed it last august.
https://lists.w3.org/Archives/Public/www-style/2016Aug/0073.html
The text was updated successfully, but these errors were encountered: