Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 4, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      June 4, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 4, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 4, 2025

      Players aren’t buying Call of Duty’s “error” excuse for the ads Activision started forcing into the game’s menus recently

      June 4, 2025

      In Sam Altman’s world, the perfect AI would be “a very tiny model with superhuman reasoning capabilities” for any context

      June 4, 2025

      Sam Altman’s ouster from OpenAI was so dramatic that it’s apparently becoming a movie — Will we finally get the full story?

      June 4, 2025

      One of Microsoft’s biggest hardware partners joins its “bold strategy, Cotton” moment over upgrading to Windows 11, suggesting everyone just buys a Copilot+ PC

      June 4, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      LatAm’s First Databricks Champion at Perficient

      June 4, 2025
      Recent

      LatAm’s First Databricks Champion at Perficient

      June 4, 2025

      Beyond AEM: How Adobe Sensei Powers the Full Enterprise Experience

      June 4, 2025

      Simplify Negative Relation Queries with Laravel’s whereDoesntHaveRelation Methods

      June 4, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Players aren’t buying Call of Duty’s “error” excuse for the ads Activision started forcing into the game’s menus recently

      June 4, 2025
      Recent

      Players aren’t buying Call of Duty’s “error” excuse for the ads Activision started forcing into the game’s menus recently

      June 4, 2025

      In Sam Altman’s world, the perfect AI would be “a very tiny model with superhuman reasoning capabilities” for any context

      June 4, 2025

      Sam Altman’s ouster from OpenAI was so dramatic that it’s apparently becoming a movie — Will we finally get the full story?

      June 4, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»Tailwind’s @apply Feature is Better Than it Sounds

    Tailwind’s @apply Feature is Better Than it Sounds

    April 10, 2025

    By this point, it’s not a secret to most people that I like Tailwind.

    But, unknown to many people (who often jump to conclusions when you mention Tailwind), I don’t like vanilla Tailwind. In fact, I find most of it horrible and I shall refrain from saying further unkind words about it.

    But I recognize and see that Tailwind’s methodology has merits — lots of them, in fact — and they go a long way to making your styles more maintainable and performant.

    Today, I want to explore one of these merit-producing features that has been severely undersold — Tailwind’s @apply feature.

    What @apply does

    Tailwind’s @apply features lets you “apply” (or simply put, copy-and-paste) a Tailwind utility into your CSS.

    Most of the time, people showcase Tailwind’s @apply feature with one of Tailwind’s single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn’t sound promising at all. It sounds downright stupid. So obviously, nobody wants to use it.

    /* Input */
    .selector {
      @apply p-4;
    }
    
    /* Output */
    .selector {
      padding: 1rem;
    }

    To make it worse, Adam Wathan recommends against using @apply, so the uptake couldn’t be worse.

    Confession: The `apply` feature in Tailwind basically only exists to trick people who are put off by long lists of classes into trying the framework.

    You should almost never use it 😬

    Reuse your utility-littered HTML instead.https://t.co/x6y4ksDwrt

    — Adam Wathan (@adamwathan) February 9, 2020

    Personally, I think Tailwind’s @apply feature is better than described.

    Tailwind’s @apply is like Sass’s @includes

    If you have been around during the time where Sass is the dominant CSS processing tool, you’ve probably heard of Sass mixins. They are blocks of code that you can make — in advance — to copy-paste into the rest of your code.

    • To create a mixin, you use @mixin
    • To use a mixin, you use @includes
    // Defining the mixin
    @mixin some-mixin() {
      color: red; 
      background: blue; 
    }
    
    // Using the mixin
    .selector {
      @include some-mixin(); 
    }
    
    /* Output */
    .selector {
      color: red; 
      background: blue; 
    }

    Tailwind’s @apply feature works the same way. You can define Tailwind utilities in advance and use them later in your code.

    /* Defining the utility */
    @utility some-utility {
      color: red; 
      background: blue; 
    }
    
    /* Applying the utility */
    .selector {
      @apply some-utility; 
    }
    
    /* Output */
    .selector {
      color: red; 
      background: blue; 
    }

    Tailwind utilities are much better than Sass mixins

    Tailwind’s utilities can be used directly in the HTML, so you don’t have to write a CSS rule for it to work.

    @utility some-utility {
      color: red; 
      background: blue; 
    }
    <div class="some-utility">...</div> 

    On the contrary, for Sass mixins, you need to create an extra selector to house your @includes before using them in the HTML. That’s one extra step. Many of these extra steps add up to a lot.

    @mixin some-mixin() {
      color: red; 
      background: blue; 
    }
    
    .selector {
      @include some-mixin(); 
    }
    
    /* Output */
    .selector {
      color: red; 
      background: blue; 
    }
    <div class="selector">...</div>

    Tailwind’s utilities can also be used with their responsive variants. This unlocks media queries straight in the HTML and can be a superpower for creating responsive layouts.

    <div class="utility1 md:utility2">…</div> 

    A simple and practical example

    One of my favorite — and most easily understood — examples of all time is a combination of two utilities that I’ve built for Splendid Layouts (a part of Splendid Labz):

    • vertical: makes a vertical layout
    • horizontal: makes a horizontal layout

    Defining these two utilities is easy.

    • For vertical, we can use flexbox with flex-direction set to column.
    • For horizontal, we use flexbox with flex-direction set to row.
    @utility horizontal {
      display: flex;
      flex-direction: row;
      gap: 1rem;
    }
    
    @utility vertical {
      display: flex;
      flex-direction: column;
      gap: 1rem;
    }

    After defining these utilities, we can use them directly inside the HTML. So, if we want to create a vertical layout on mobile and a horizontal one on tablet or desktop, we can use the following classes:

    <div class="vertical sm:horizontal">...</div>

    For those who are new to Tailwind, sm: here is a breakpoint variant that tells Tailwind to activate a class when it goes beyond a certain breakpoint. By default, sm is set to 640px, so the above HTML produces a vertical layout on mobile, then switches to a horizontal layout at 640px.

    Open Live Demo

    If you prefer traditional CSS over composing classes like the example above, you can treat @apply like Sass @includes and use them directly in your CSS.

    <div class="your-layout">...</div> 
    .your-layout {
      @apply vertical; 
    
      @media (width >= 640px) {
        @apply horizontal;
      }
    }

    The beautiful part about both of these approaches is you can immediately see what’s happening with your layout — in plain English — without parsing code through a CSS lens. This means faster recognition and more maintainable code in the long run.

    Tailwind’s utilities are a little less powerful compared to Sass mixins

    Sass mixins are more powerful than Tailwind utilities because:

    1. They let you use multiple variables.
    2. They let you use other Sass features like @if and @for loops.
    @mixin avatar($size, $circle: false) {
      width: $size;
      height: $size;
    
      @if $circle {
        border-radius: math.div($size, 2);
      }
    }

    On the other hand, Tailwind utilities don’t have these powers. At the very maximum, Tailwind can let you take in one variable through their functional utilities.

    /* Tailwind Functional Utility */
    @utility tab-* { 
      tab-size: --value(--tab-size-*);
    }

    Fortunately, we’re not affected by this “lack of power” much because we can take advantage of all modern CSS improvements — including CSS variables. This gives you a ton of room to create very useful utilities.

    Let’s go through another example

    A second example I often like to showcase is the grid-simple utility that lets you create grids with CSS Grid easily.

    We can declare a simple example here:

    @utility grid-simple {
      display: grid;
      grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
      gap: var(--gap, 1rem);
    }

    By doing this, we have effectively created a reusable CSS grid (and we no longer have to manually declare minmax everywhere).

    After we have defined this utility, we can use Tailwind’s arbitrary properties to adjust the number of columns on the fly.

    <div class="grid-simple [--cols:3]"> 
      <div class="item">...</div>
      <div class="item">...</div>
      <div class="item">...</div>
    </div>

    To make the grid responsive, we can add Tailwind’s responsive variants with arbitrary properties so we only set --cols:3 on a larger breakpoint.

    <div class="grid-simple sm:[--cols:3]"> 
      <div class="item">...</div>
      <div class="item">...</div>
      <div class="item">...</div>
    </div>
    Open Live Demo

    This makes your layouts very declarative. You can immediately tell what’s going on when you read the HTML.

    Now, on the other hand, if you’re uncomfortable with too much Tailwind magic, you can always use @apply to copy-paste the utility into your CSS. This way, you don’t have to bother writing repeat and minmax declarations every time you need a grid that grid-simple can create.

    .your-layout {
      @apply grid-simple; 
      @media (width >= 640px) {
        --cols: 3;
      }
    }
    <div class="your-layout"> ... </div>

    By the way, using @apply this way is surprisingly useful for creating complex layouts! But that seems out of scope for this article so I’ll be happy to show you an example another day.

    Wrapping up

    Tailwind’s utilities are very powerful by themselves, but they’re even more powerful if you allow yourself to use @apply (and allow yourself to detach from traditional Tailwind advice). By doing this, you gain access to Tailwind as a tool instead of it being a dogmatic approach.

    To make Tailwind’s utilities even more powerful, you might want to consider building utilities that can help you create layouts and nice visual effects quickly and easily.

    I’ve built a handful of these utilities for Splendid Labz and I’m happy to share them with you if you’re interested! Just check out Splendid Layouts to see a subset of the utilities I’ve prepared.

    By the way, the utilities I showed you above are watered-down versions of the actual ones I’m using in Splendid Labz.

    One more note: When writing this, Splendid Layouts work with Tailwind 3, not Tailwind 4. I’m working on a release soon, so sign up for updates if you’re interested!


    Tailwind’s @apply Feature is Better Than it Sounds originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleRilasciato OpenSSH 10: Un aggiornamento significativo per la sicurezza e la crittografia
    Next Article Distribution Release: Pardus 23.4

    Related Posts

    News & Updates

    Players aren’t buying Call of Duty’s “error” excuse for the ads Activision started forcing into the game’s menus recently

    June 4, 2025
    News & Updates

    In Sam Altman’s world, the perfect AI would be “a very tiny model with superhuman reasoning capabilities” for any context

    June 4, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    CVE-2025-44861 – TOTOLINK CA300-POE Command Injection

    Common Vulnerabilities and Exposures (CVEs)

    Look, no patches! Why Chainguard OS might be the most secure Linux ever

    News & Updates

    OpenAI Releases a Technical Playbook for Enterprise AI Integration

    Machine Learning

    Microsoft will retire Bing Search APIs on August 11, 2025

    Operating Systems

    Highlights

    Want a free Samsung Music Frame? Buy one of these TVs or soundbars to get one

    April 17, 2025

    Purchasing one of Samsung’s select OLED or QLED TVs or Q-Series soundbars comes with a…

    LACT configures and monitors AMD, NVIDIA and Intel GPUs

    April 18, 2025

    How To Build Confidence In Your UX Work

    March 19, 2025

    The Impact of React.js and AI in Web Application Development

    May 31, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.