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

      10 Ways Node.js Development Boosts AI & Real-Time Data (2025-2026 Edition)

      August 18, 2025

      Looking to Outsource React.js Development? Here’s What Top Agencies Are Doing Right

      August 18, 2025

      Beyond The Hype: What AI Can Really Do For Product Design

      August 18, 2025

      BrowserStack launches Chrome extension that bundles 10+ manual web testing tools

      August 18, 2025

      How much RAM does your Linux PC really need in 2025?

      August 19, 2025

      Have solar at home? Supercharge that investment with this other crucial component

      August 19, 2025

      I replaced my MacBook charger with this compact wall unit – and wish I’d done it sooner

      August 19, 2025

      5 reasons to switch to an immutable Linux distro today – and which to try first

      August 19, 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

      Sentry Adds Logs Support for Laravel Apps

      August 19, 2025
      Recent

      Sentry Adds Logs Support for Laravel Apps

      August 19, 2025

      Efficient Context Management with Laravel’s Remember Functions

      August 19, 2025

      Laravel Devtoolbox: Your Swiss Army Knife Artisan CLI

      August 19, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      From plateau predictions to buggy rollouts — Bill Gates’ GPT-5 skepticism looks strangely accurate

      August 18, 2025
      Recent

      From plateau predictions to buggy rollouts — Bill Gates’ GPT-5 skepticism looks strangely accurate

      August 18, 2025

      We gave OpenAI’s open-source AI a kid’s test — here’s what happened

      August 18, 2025

      With GTA 6, next-gen exclusives, and a console comeback on the horizon, Xbox risks sitting on the sidelines — here’s why

      August 18, 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

    How much RAM does your Linux PC really need in 2025?

    August 19, 2025
    News & Updates

    Have solar at home? Supercharge that investment with this other crucial component

    August 19, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    React Native & the Future of Mobile: Why Now Is the Time for Businesses to Invest📱

    Web Development

    Anti-Aliasing In CSS A Powerful Concept

    Web Development

    Atomic Design, Tokens, AI and the Future of Design Systems – Experience Designed Podcast

    Web Development

    Velocity: AI user testing for prototypes

    Web Development

    Highlights

    Apache SeaTunnel Vulnerability Allows Unauthorized Users to Perform Deserialization Attack

    June 20, 2025

    Apache SeaTunnel Vulnerability Allows Unauthorized Users to Perform Deserialization Attack

    Apache SeaTunnel, the widely used distributed data integration platform, has disclosed a significant security vulnerability that enables unauthorized users to execute arbitrary file read operations an …
    Read more

    Published Date:
    Jun 20, 2025 (1 hour, 44 minutes ago)

    Vulnerabilities has been mentioned in this article.

    CVE-2025-32896

    CVE-2025-4094 – “Acunetix DIGITS WordPress OTP Brute Force Vulnerability”

    May 22, 2025

    CVE-2025-7092 – Belkin F9K1122 Web WPS Enrolee Pin Stack Buffer Overflow

    July 7, 2025

    Opsera Raises $20M to Drive AI-Powered DevOps Platform Innovation, Accelerating AI Agent Adoption and Developer Efficiency

    April 3, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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