Skip to content

[Proposal][css-2025] CSS if() and CSS custom properties nested inside CSS function #12198

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

Open
Luko248 opened this issue May 15, 2025 · 1 comment

Comments

@Luko248
Copy link

Luko248 commented May 15, 2025

I would like to share an idea and ask a question regarding the future of CSS functions, especially in the context of relative colors, CSS types, and conditional logic (such as if()).

Idea: Contrast Text CSS Function
I'm interested in a native CSS function that would calculate a contrasting text color based on a given background color. Here is a conceptual example of how such a function might look if CSS functions supported variables, relative colors, and conditional logic:

@function --contrast-text(--color <color>, CanvasText) {
  --l: lch(from var(--color) l);
  --c: lch(from var(--color) c);
  --h: lch(from var(--color) h);
 
  --result-color: if(style(--l < 50%):
    lch(from var(--color) calc(var(--l) + 40%) var(--c) var(--h));
  else:
    lch(from var(--color) calc(var(--l) - 40%) var(--c) var(--h)));
 
  result var(--result-color);
}
 
button {
  --bg-color: #336699;
  background-color: var(--bg-color);
  color: --contrast-text(var(--bg-color));
}

Question
Will it be possible in the future CSS specification to define and use custom CSS properties (variables or constants) directly inside CSS functions, similar to the example above? Specifically, can we expect support for:

  • Declaring local variables within a function's scope
  • Using relative color syntax and CSS types within functions
  • Conditional logic (like if()) in CSS functions

I believe this would greatly enhance the expressiveness and power of CSS, especially for design systems and accessibility.

@Luko248 Luko248 changed the title [Proposal][css-2025] [Proposal][css-2025] CSS if() and CSS custom properties nested inside CSS function May 15, 2025
@andruud
Copy link
Member

andruud commented May 16, 2025

I think the only thing missing is a way to extract e.g. the l from a color as a percentage? Everything else you're asking for is either already possible, or coming soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants