{
  "version": "https://jsonfeed.org/version/1",
  "title": "Css on LLBBL Blog",
  "icon": "https://avatars.micro.blog/avatars/2023/40/125738.jpg",
  "home_page_url": "https://llbbl.blog/",
  "feed_url": "https://llbbl.blog/feed.json",
  "items": [
      {
        "id": "http://llbbl.micro.blog/2026/05/11/css-finally-got-inline-conditionals.html",
        "title": "CSS Finally Got Inline Conditionals",
        "content_html": "<p>I&rsquo;ve been digging into some of the newer features landing in CSS, and inline conditionals with the <code>if()</code> function jumped out as one of the more interesting ones. The idea of writing a condition right next to the property it affects, instead of opening a separate <code>@media</code> block ten lines away, sounds useful. So this post is my attempt at summarizing what I found. I hope it helps if you&rsquo;re trying to figure out what <code>if()</code> actually does and where it fits.</p>\n<h2 id=\"what-it-actually-does\">What It Actually Does</h2>\n<p>The shape is condition, colon, value, with an optional <code>else</code> branch:</p>\n<div class=\"highlight\"><pre tabindex=\"0\" style=\"color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-css\" data-lang=\"css\"><span style=\"color:#f92672\">property</span><span style=\"color:#f92672\">:</span> <span style=\"color:#f92672\">if</span><span style=\"color:#f92672\">(&lt;</span><span style=\"color:#f92672\">condition</span><span style=\"color:#f92672\">&gt;:</span> <span style=\"color:#f92672\">&lt;</span><span style=\"color:#f92672\">value</span><span style=\"color:#f92672\">&gt;;</span> <span style=\"color:#f92672\">else</span><span style=\"color:#f92672\">:</span> <span style=\"color:#f92672\">&lt;</span><span style=\"color:#f92672\">fallback</span><span style=\"color:#f92672\">&gt;;);</span>\n</code></pre></div><p>You evaluate something, you get back a value, and the property uses that value. The <code>else:</code> branch is optional. If you leave it off and the condition fails, the property falls back to its inherited or initial value.</p>\n<p>That&rsquo;s it.</p>\n<p>What makes it powerful is <em>what</em> you can put in that condition slot. Three kinds of queries are supported: <code>media()</code> for viewport and device conditions, <code>style()</code> for custom property values on the element, and <code>supports()</code> for feature detection. The same things you&rsquo;ve always been able to ask CSS, but now you can ask them inline, right next to the property they affect.</p>\n<h2 id=\"theming-without-the-yo-yo\">Theming Without the Yo-Yo</h2>\n<p>Dark mode is the most obvious win.</p>\n<div class=\"highlight\"><pre tabindex=\"0\" style=\"color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-css\" data-lang=\"css\">.<span style=\"color:#a6e22e\">card</span> {\n  <span style=\"color:#66d9ef\">background-color</span>: <span style=\"color:#66d9ef\">white</span>;\n  <span style=\"color:#66d9ef\">color</span>: <span style=\"color:#66d9ef\">black</span>;\n}\n\n@<span style=\"color:#66d9ef\">media</span> <span style=\"color:#f92672\">(</span><span style=\"color:#f92672\">prefers-color-scheme</span><span style=\"color:#f92672\">:</span> <span style=\"color:#f92672\">dark</span><span style=\"color:#f92672\">)</span> {\n  .<span style=\"color:#a6e22e\">card</span> {\n    <span style=\"color:#66d9ef\">background-color</span>: <span style=\"color:#ae81ff\">#333</span>;\n    <span style=\"color:#66d9ef\">color</span>: <span style=\"color:#66d9ef\">white</span>;\n  }\n}\n</code></pre></div><p>And here&rsquo;s the same thing with <code>if()</code>:</p>\n<div class=\"highlight\"><pre tabindex=\"0\" style=\"color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-css\" data-lang=\"css\">.<span style=\"color:#a6e22e\">card</span> {\n  <span style=\"color:#66d9ef\">background-color</span>: <span style=\"color:#a6e22e\">if</span>(<span style=\"color:#a6e22e\">media</span>(prefers<span style=\"color:#f92672\">-</span><span style=\"color:#66d9ef\">color</span><span style=\"color:#f92672\">-</span>scheme<span style=\"color:#960050;background-color:#1e0010\">:</span> dark)<span style=\"color:#960050;background-color:#1e0010\">:</span> <span style=\"color:#ae81ff\">#333</span><span style=\"color:#960050;background-color:#1e0010\">;</span> else<span style=\"color:#960050;background-color:#1e0010\">:</span> <span style=\"color:#66d9ef\">white</span><span style=\"color:#960050;background-color:#1e0010\">;</span>);\n  <span style=\"color:#66d9ef\">color</span>: <span style=\"color:#a6e22e\">if</span>(<span style=\"color:#a6e22e\">media</span>(prefers<span style=\"color:#f92672\">-</span><span style=\"color:#66d9ef\">color</span><span style=\"color:#f92672\">-</span>scheme<span style=\"color:#960050;background-color:#1e0010\">:</span> dark)<span style=\"color:#960050;background-color:#1e0010\">:</span> <span style=\"color:#66d9ef\">white</span><span style=\"color:#960050;background-color:#1e0010\">;</span> else<span style=\"color:#960050;background-color:#1e0010\">:</span> <span style=\"color:#66d9ef\">black</span><span style=\"color:#960050;background-color:#1e0010\">;</span>);\n}\n</code></pre></div><p>The logic lives next to the property. You don&rsquo;t have to hunt down a separate block to figure out what happens in dark mode. When you change the light color, the dark color is right there staring at you. That&rsquo;s a real maintenance win, especially in a stylesheet that&rsquo;s been touched by five different people.</p>\n<h2 id=\"variants-without-modifier-classes\">Variants Without Modifier Classes</h2>\n<p>This is the example that makes the case best. Component libraries are drowning in modifier classes. <code>.btn-primary</code>, <code>.btn-danger</code>, <code>.btn-success</code>, on and on. Every one of them is just a different background color and maybe a border.</p>\n<p>With <code>if()</code> and a custom property, you can collapse the whole thing:</p>\n<div class=\"highlight\"><pre tabindex=\"0\" style=\"color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-css\" data-lang=\"css\">.<span style=\"color:#a6e22e\">button</span> {\n  <span style=\"color:#66d9ef\">padding</span>: <span style=\"color:#ae81ff\">10</span><span style=\"color:#66d9ef\">px</span> <span style=\"color:#ae81ff\">20</span><span style=\"color:#66d9ef\">px</span>;\n  <span style=\"color:#66d9ef\">border-radius</span>: <span style=\"color:#ae81ff\">6</span><span style=\"color:#66d9ef\">px</span>;\n  <span style=\"color:#66d9ef\">color</span>: <span style=\"color:#66d9ef\">white</span>;\n  <span style=\"color:#66d9ef\">background-color</span>: <span style=\"color:#a6e22e\">if</span>(\n    <span style=\"color:#a6e22e\">style</span>(<span style=\"color:#f92672\">--</span>variant<span style=\"color:#960050;background-color:#1e0010\">:</span> danger)<span style=\"color:#960050;background-color:#1e0010\">:</span> <span style=\"color:#66d9ef\">red</span><span style=\"color:#960050;background-color:#1e0010\">;</span>\n    <span style=\"color:#a6e22e\">style</span>(<span style=\"color:#f92672\">--</span>variant<span style=\"color:#960050;background-color:#1e0010\">:</span> success)<span style=\"color:#960050;background-color:#1e0010\">:</span> <span style=\"color:#66d9ef\">green</span><span style=\"color:#960050;background-color:#1e0010\">;</span>\n    else<span style=\"color:#960050;background-color:#1e0010\">:</span> <span style=\"color:#66d9ef\">blue</span><span style=\"color:#960050;background-color:#1e0010\">;</span>\n  );\n}\n</code></pre></div><div class=\"highlight\"><pre tabindex=\"0\" style=\"color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-html\" data-lang=\"html\">&lt;<span style=\"color:#f92672\">button</span> <span style=\"color:#a6e22e\">class</span><span style=\"color:#f92672\">=</span><span style=\"color:#e6db74\">&#34;button&#34;</span>&gt;Submit&lt;/<span style=\"color:#f92672\">button</span>&gt;\n&lt;<span style=\"color:#f92672\">button</span> <span style=\"color:#a6e22e\">class</span><span style=\"color:#f92672\">=</span><span style=\"color:#e6db74\">&#34;button&#34;</span> <span style=\"color:#a6e22e\">style</span><span style=\"color:#f92672\">=</span><span style=\"color:#e6db74\">&#34;--variant: danger;&#34;</span>&gt;Delete&lt;/<span style=\"color:#f92672\">button</span>&gt;\n</code></pre></div><p>No JavaScript reading props and toggling class names. No BEM modifier soup. You set a variable, the CSS reacts. The component owns its own logic.</p>\n<h2 id=\"feature-detection-inline\">Feature Detection Inline</h2>\n<p>The same pattern works for graceful degradation when you want a newer feature with a fallback:</p>\n<div class=\"highlight\"><pre tabindex=\"0\" style=\"color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-css\" data-lang=\"css\">.<span style=\"color:#a6e22e\">element</span> {\n  <span style=\"color:#66d9ef\">display</span>: <span style=\"color:#a6e22e\">if</span>(<span style=\"color:#a6e22e\">supports</span>(display<span style=\"color:#960050;background-color:#1e0010\">:</span> grid)<span style=\"color:#960050;background-color:#1e0010\">:</span> grid<span style=\"color:#960050;background-color:#1e0010\">;</span> else<span style=\"color:#960050;background-color:#1e0010\">:</span> <span style=\"color:#66d9ef\">flex</span><span style=\"color:#960050;background-color:#1e0010\">;</span>);\n  <span style=\"color:#66d9ef\">color</span>: <span style=\"color:#a6e22e\">if</span>(\n    <span style=\"color:#a6e22e\">supports</span>(<span style=\"color:#66d9ef\">color</span><span style=\"color:#960050;background-color:#1e0010\">:</span> <span style=\"color:#a6e22e\">lch</span>(<span style=\"color:#ae81ff\">75</span><span style=\"color:#66d9ef\">%</span> <span style=\"color:#ae81ff\">0</span> <span style=\"color:#ae81ff\">0</span>))<span style=\"color:#960050;background-color:#1e0010\">:</span> <span style=\"color:#a6e22e\">lch</span>(<span style=\"color:#ae81ff\">75</span><span style=\"color:#66d9ef\">%</span> <span style=\"color:#ae81ff\">0</span> <span style=\"color:#ae81ff\">0</span>)<span style=\"color:#960050;background-color:#1e0010\">;</span>\n    else<span style=\"color:#960050;background-color:#1e0010\">:</span> rgb(<span style=\"color:#ae81ff\">185</span> <span style=\"color:#ae81ff\">185</span> <span style=\"color:#ae81ff\">185</span>)<span style=\"color:#960050;background-color:#1e0010\">;</span>\n  );\n}\n</code></pre></div><p>This used to require a dedicated <code>@supports</code> block with the property repeated inside. Now it doesn&rsquo;t.</p>\n<h2 id=\"observations\">Observations</h2>\n<p>A few things are happening here that go deeper than syntactic sugar.</p>\n<p>The first is bloat reduction. A component that used to need a base class plus four modifier classes plus a media query block can be one selector. Multiply that across a design system and the savings get real.</p>\n<p>The second is encapsulation. A component&rsquo;s stylesheet can carry its own logic without relying on a JavaScript framework to compute the right class name and shove it into the DOM. The CSS engine is doing the work, natively, and it&rsquo;s a lot faster at this than your render loop is.</p>\n<p>If the logic lives where the value lives, you don&rsquo;t context-switch to figure out why a property has the value it does. You read the property, you read the condition, you understand it, you move on.</p>\n<p>That&rsquo;s the same reason inline error handling beats a separate try/catch block ten lines away. Co-location is a maintenance superpower.</p>\n<h2 id=\"browser-support\">Browser Support</h2>\n<p>Browser support is still narrow. Chrome shipped <code>if()</code> in version 137, but Firefox and Safari haven&rsquo;t followed yet, so this isn&rsquo;t something to drop into a production stylesheet without a fallback. The pattern is to declare a plain value first and then override with <code>if()</code>, so non-supporting browsers ignore the line they don&rsquo;t understand:</p>\n<div class=\"highlight\"><pre tabindex=\"0\" style=\"color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-css\" data-lang=\"css\">.<span style=\"color:#a6e22e\">card</span> {\n  <span style=\"color:#66d9ef\">background-color</span>: <span style=\"color:#66d9ef\">white</span>;\n  <span style=\"color:#66d9ef\">background-color</span>: <span style=\"color:#a6e22e\">if</span>(<span style=\"color:#a6e22e\">media</span>(prefers<span style=\"color:#f92672\">-</span><span style=\"color:#66d9ef\">color</span><span style=\"color:#f92672\">-</span>scheme<span style=\"color:#960050;background-color:#1e0010\">:</span> dark)<span style=\"color:#960050;background-color:#1e0010\">:</span> <span style=\"color:#ae81ff\">#333</span><span style=\"color:#960050;background-color:#1e0010\">;</span> else<span style=\"color:#960050;background-color:#1e0010\">:</span> <span style=\"color:#66d9ef\">white</span><span style=\"color:#960050;background-color:#1e0010\">;</span>);\n}\n</code></pre></div><p>The syntax has also shifted through CSS Working Group drafts, so older articles you find about <code>if()</code> may show a comma-ternary form that no longer works. Check MDN before copying anything.</p>\n<p>But the direction is right. CSS has been quietly absorbing more and more of the work we used to push to JavaScript, and <code>if()</code> is one of the bigger steps in that direction. I&rsquo;ll probably keep poking at it.</p>\n<h2 id=\"sources\">Sources</h2>\n<ul>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/if\">if() CSS function — MDN</a></li>\n<li><a href=\"https://developer.chrome.com/blog/if-article\">CSS conditionals with the new if() function — Chrome for Developers</a></li>\n<li><a href=\"https://css-tricks.com/lightly-poking-at-the-css-if-function-in-chrome-137/\">Lightly Poking at the CSS if() Function in Chrome 137 — CSS-Tricks</a></li>\n<li><a href=\"https://caniuse.com/css-if\">CSS if() function — Can I use</a></li>\n</ul>\n<blockquote>\n<p>I&rsquo;d appreciate a follow. You can subscribe with your email below. The emails go out once a week, or you can find me on Mastodon at <a href=\"https://micro.blog/llbbl?remote_follow=1\">@logan@llbbl.blog</a>.</p>\n</blockquote>\n",
        "date_published": "2026-05-11T10:00:00-05:00",
        "url": "https://llbbl.blog/2026/05/11/css-finally-got-inline-conditionals.html",
        "tags": ["Web development","Css","Frontend"]
      }
  ]
}
