Skip to content

Topics

Jump to a topic.

Search questions

Find a question by title, tag, or summary.

How do you ship new CSS safely — forgiving parsing and @supports?

Questions in this chapter (7)
  • Draft
  • medium
  • Askedoften
  • Verified October 2026

Answer

CSS is designed so new syntax degrades instead of breaking the page. Error recovery (CSS Syntax 3) throws away the smallest piece it can:

  • A declaration with an unknown property or value is dropped, up to the next ;. Earlier declarations in the block still apply, so writing the fallback first works: width: 100px; width: stretch;.
  • A style rule is dropped whole if its selector list is invalid. One unknown pseudo-class in .a, .b:new-state drops .a too. :is() and :where() take a forgiving list, so :is(.a, .b:new-state) keeps .a. :has() doesn't.
  • An at-rule the browser doesn't know is skipped with its whole block.

When a change needs several declarations to move together, or the fallback would conflict with the new value, use a feature query:

@supports (width: stretch) or (width: -webkit-fill-available) { … }
@supports selector(:has(a)) { … }

@supports tests whether the browser accepts a declaration as it would in a style rule. selector() (Conditional 4) tests a selector; newer functions test fonts (font-tech(), font-format()) and at-rules (at-rule(@scope), Chrome 148 and Firefox 157 only). and, or and not need parentheses, and and and or can't be mixed without them; an invalid condition drops the whole @supports rule. JavaScript has CSS.supports().

Three gotchas:

  • Unknown syntax is false, not invalid. @supports not (wobble(3)) is true, so a not query matches in browsers that don't understand what it tests.
  • Parsing isn't working. A buggy or partial implementation still passes, and (color: var(--x)) is always true because var() defers checking.
  • selector() is strict. selector(:is(:new-state)) is false even though a rule using it would be kept.

The pattern: write the baseline unconditionally, then layer the enhancement with fallback declarations, forgiving selectors, or @supports.

Example

<style>
  .box { width: 100px; width: wobble(3); }
  .a, .b:new-state { color: red; }
  :is(.c, .d:new-state) { color: red; }
  @supports selector(:has(a)) { .e { color: green; } }
  @supports not (wobble(3)) { .f { color: green; } }
</style>
<div class="box"></div>
<p class="a">a</p><p class="c">c</p><p class="e">e</p><p class="f">f</p>
<script>
  const style = (name) => getComputedStyle(document.querySelector(`.${name}`));
  console.log(style("box").width);
  // → 100px
  console.log(style("a").color);
  // → rgb(0, 0, 0)
  console.log(style("c").color);
  // → rgb(255, 0, 0)
  console.log(style("e").color);
  // → rgb(0, 128, 0)
  console.log(style("f").color);
  // → rgb(0, 128, 0)
  console.log(CSS.supports("selector(:is(:new-state))"));
  // → false
</script>

Chrome 156 and Firefox 159 agreed on every value. They differed in the CSSOM: Chrome stored the forgiving rule's selectorText as :is(.c), while Firefox kept :is(.c, .d:new-state).

Pitfalls

  • Adding a new pseudo-class to an existing selector list. The whole rule disappears in browsers that don't know it; give it its own rule or wrap the list in :is().
  • Using @supports not (new-syntax) as "old browsers only". Browsers that predate @supports itself skip the block, and ones that can't parse the test treat it as false, so not makes it true.
  • Trusting @supports (--x: value) to test a registered type. Custom properties accept almost anything at parse time, so it's true for any value.
  • Writing selector(.a, .b). selector() takes one complex selector; a list returned false.

Follow-ups

Why doesn't an unknown selector just match nothing while the rest of the list applies?
Selectors 4 keeps the legacy rule: a selector list with an invalid selector is invalid. Forgiving parsing was added only to :is() and :where(), so wrapping a list in :is() opts into it.
What happens to unknown ::-webkit- pseudo-elements?
For compatibility they're valid but match nothing, so .d, .e::-webkit-made-up kept the rule and styled .d. selector(::-webkit-made-up) still returned false.
Does an unknown at-rule nested in a style rule break the rule?
No. In .g { color: red; @frobnicate { … } background-color: … } only the at-rule was dropped; both declarations applied.

Exercises

Which paragraphs turn green?

<style>
  @supports (display: grid) and (not (display: wobble)) { .x { color: green; } }
  @supports display: grid { .y { color: green; } }
  @supports (display: grid) and (display: flex) or (display: block) { .z { color: green; } }
</style>
<p class="x">x</p><p class="y">y</p><p class="z">z</p>

Comments

Comments are GitHub Discussions: you sign in with GitHub to post or react.