Skip to main content
HTML Validation

CSS: Unrecognized at-rule @if

About This CSS Issue

The CSS @if rule is not part of any stable CSS specification and is not recognized by the W3C validator.

CSS Conditional Rules (like @media and @supports) are the standard way to apply styles conditionally in CSS. The @if rule exists only as a proposal in the CSS Conditional Rules Module Level 5 draft and has no browser support. If you're seeing @if in your stylesheets, it likely came from a CSS preprocessor like Sass or Less, where @if is a compile-time directive. Preprocessor syntax should never appear in the final CSS output sent to the browser.

If you're writing Sass or Less, make sure your build process compiles the source files into plain CSS before including them in your HTML. If you need conditional logic in standard CSS, use @media for viewport or device conditions, or @supports for feature detection.

HTML example with the issue

<style>
  .banner {
    background: blue;
  }
  @if (width > 600px) {
    .banner {
      background: red;
    }
  }
</style>

Fixed example using @media

<style>
  .banner {
    background: blue;
  }
  @media (min-width: 600px) {
    .banner {
      background: red;
    }
  }
</style>

Fixed example using @supports

If the condition is about feature support rather than viewport size:

<style>
  .layout {
    display: flex;
  }
  @supports (display: grid) {
    .layout {
      display: grid;
    }
  }
</style>

Find issues like this automatically

Rocket Validator scans thousands of pages in seconds, detecting HTML issues across your entire site.

Help us improve our guides

Was this guide helpful?
๐ŸŒ Trusted by teams worldwide

Validate at scale.
Ship accessible websites, faster.

Automated HTML & accessibility validation for large sites. Check thousands of pages against WCAG guidelines and W3C standards in minutes, not days.

Scheduled Reports
API Access
Open Source Standards
$7 / 7 days

Pro Trial

Full Pro access. Cancel anytime.

Start Pro Trial โ†’

Join teams across 40+ countries