-
Notifications
You must be signed in to change notification settings - Fork 677
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
[css-values] enable the use of counter() inside calc() #1026
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? |
A major styling issue front-end developers and designers experience is when they are tasked with dynamically changing styling of repeated elements. As of writing this is normally done either via swaths of Permitting the use of counters within calc() can greatly simplify and condense such verbose code. Let's say we wanted to animate each of the following list items in a cascade. <ol>
<li>Foo</li>
<li>Bar</li>
<li>Bizz</li>
<li>Buzz</li>
</ol> Currently it has to be manually like so: @keyframes reveal { 0% { opacity:0; } }
ol>* { animation: reveal both 3s; }
li:nth-child(1) { animation-delay: 0.5s; }
li:nth-child(2) { animation-delay: 1s; }
li:nth-child(3) { animation-delay: 1.5s; }
li:nth-child(4) { animation-delay: 2s; } When the number of items or timing changes, each element must be revised. However, counter values allow code that is:
@keyframes reveal { 0% { opacity:0; } }
ol>* {
animation: reveal both 3s;
animation-delay:calc( counter-value(list-item) / 0.5s );
} This opens up the world to so many other effects that were limited to JS and annoying manual composition in a clean form... although my creative juices are running low - I bet someone more creative such as @argyleink can come up with many other fun uses. Perhaps limiting use to the special list-item counter to start with could guarantee some sort of baseline to get the rest sorted? My concern as an end user is that other potential methods (Such as the //EDIT// Just realized I commented on the sibling-index() proposal last year, whoops. Hope it gains more traction. |
I too was sad that this did not work. I had to turn something elegant:
into a whole string of hideous and fragile:
|
If you need another example, this can be useful with overlapping grid items. In this example the user wants item1 to span both columns and item2 to be in the second column for each row. Doing this without counters requires explicit row numbers. #grid {
display: grid;
grid-template-columns: repeat(2, 20px);
counter-reset: row;
}
.item1 {
counter-increment: row;
grid-column: 1 / span 2;
grid-row: counter(row);
}
.item2 {
grid-column: 2;
grid-row: counter(row);
} <div id="grid">
<div class="item1">1</div>
<div class="item2">1</div>
<div class="item1">2</div>
<div class="item2">2</div>
</div> |
👍 |
Hi @mnik01 and welcome to the csswg repo! Just a quick note that since these discussions can get quite long, we tend to avoid posting +1s etc unless we also have something else to say in addition to the expression of support and use reactions instead to simply express support. |
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 |
|
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: