Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

I want to pin evaluation of a CSS custom property to the element it's defined on #236

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

Closed
aarongustafson opened this issue Dec 4, 2020 · 4 comments
Assignees
Labels
CSS no-activity want Incoming requests from the community

Comments

@aarongustafson
Copy link
Member

aarongustafson commented Dec 4, 2020

As an example, if I need to know the width of a container element to calculate the width of a deeply nested element:

.container {
  --container-width: 100%; 
}

.container__deeply-nested-element {
  // what I need is the actual container width to be able to correctly
  // calculate the desired width of the nested element...
  // but 100% is evaluated here to the element's parent :(
  width: calc(var(--container-width, 0) / 3); 
}
@aarongustafson aarongustafson added the want Incoming requests from the community label Dec 4, 2020
@aarongustafson aarongustafson self-assigned this Dec 4, 2020
@aarongustafson
Copy link
Member Author

@phloe Could you expand on this a bit more? I think I know where you’re coming from, but I want to make sure.

@phloe
Copy link

phloe commented Dec 5, 2020

I want to be able to pin the evaluation of relative units in custom properties to the actual element I defined said custom property on. So every time it is used further down the node tree it will still look at the original element for evaluation (meaning it's stil live/dynamic - not just a static number).

Does that help?

Obviously this would require some new syntax - I haven't given that any thought :D

@captainbrosset
Copy link
Collaborator

This seems like a job for Container Queries: https://developer.mozilla.org/en-US/blog/getting-started-with-css-container-queries/
@phloe container queries appeared in browsers in 2022, so after you posted this Want. Do you think this addresses your need?

@WebWeWant WebWeWant deleted a comment from github-actions bot Apr 10, 2025
@aarongustafson
Copy link
Member Author

This is addressed by the cqi unit, which is part of the CSS Containment Module Level 3:

.container {
  container-type: inline-size;
}
.container__deeply-nested-element {
  width: calc(100cqi / 3);
}

You can view a demo on Codepen.

@WebWeWant WebWeWant locked and limited conversation to collaborators Apr 10, 2025
@aarongustafson aarongustafson converted this issue into discussion #728 Apr 10, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
CSS no-activity want Incoming requests from the community
Projects
None yet
Development

No branches or pull requests

5 participants