Skip to content

add css if guidance - #758

Open
patrickkettner wants to merge 2 commits into
mainfrom
css-if
Open

add css if guidance#758
patrickkettner wants to merge 2 commits into
mainfrom
css-if

Conversation

@patrickkettner

Copy link
Copy Markdown
Contributor

fixes #689

Per the new use-case guide process, step 1 is just the stub guide.md
frontmatter and a demo.html. Removes body of guide.md, expectations.md,
grader.ts, negative-demo.html, and tasks/task.md (those belong to steps
2 and 3).
@rviscomi

Copy link
Copy Markdown
Member

This feature isn't a blocker for I/O, so I'm going to set this PR to draft. After I/O, but before marking it ready for review, could you add any other relevant use cases for the feature? Most features have between 2–5 use cases, but it's ok if there's truly just one.

@rviscomi
rviscomi marked this pull request as draft May 14, 2026 15:36
@rviscomi
rviscomi marked this pull request as ready for review June 15, 2026 18:00
@patrickkettner

Copy link
Copy Markdown
Contributor Author

@rviscomi did we want to do anything with this one?

@patrickkettner

Copy link
Copy Markdown
Contributor Author

@rviscomi Was thinking about use cases, and after looking through the existing guides it seems like the style query ones (child-state-based-styling, design-token-reactivity, and usage-aware-component-variations) cover reacting to state
on an ancestor or child. I don't think if is necessarily better than any of those (at least as they are written), so could the gap a if() guide fills be an element branching on a custom property set on itself? A unique use case that I coudl think of is styling component variants from a single flag without a class per variant, as css/variant-driven-styling. Like a badge that sets --severity: error and uses that to determine its background, border, and icon color (instead of needing a .badge-error class or a wrapper container). Alternatively I can mess with the other guides I mentioned before to fold in if()?

@rviscomi
rviscomi requested a review from LeaVerou July 22, 2026 20:45

@LeaVerou LeaVerou left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Responsive inline styling is a good one, and if() does make things a lot more compact. However, the current Chrome implementation makes it hard to store conditions in variables, which limits utility as you have to repeat the MQ all over the place or pre-bake the values, which is easy to do with a regular MQ too.
See https://codepen.io/leaverou/pen/Kwaxegy?editors=1100 and https://codepen.io/leaverou/pen/XJpPYMy?editors=1100 specifically for MQs.

I'd remove the --theme one as it actively conflicts with the component-specific-light-dark-theme guide. That said, branching based on resolved, local color-scheme is a great use case that combines both if() and the recently launched color-scheme() feature. Basically @bramus' https://www.bram.us/2025/09/30/css-custom-light-dark/ without the @function.

But the guide name is css-if, which sounds like a discipline guide about doing conditionals in CSS. Which is also another use case!

In terms of other use cases, if() shines for cases where:

  • You can branch on the element itself (style queries only let you branch on descendants)
  • The condition is global, so an inline expression simplifies things (at least in theory, see caveat above)

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

Successfully merging this pull request may close these issues.

CSS if()

3 participants