csstailwind-cssresponsivetailwind-css-4

TailwindCSS v4 responsive and custom CSS not applied


I have app.css like this:

@import 'tailwindcss';

@source '../../vendor/laravel/framework/src/Illuminate/Pagination/resources/views/*.blade.php'; @source '../../storage/framework/views/*.php'; @source '../**/*.blade.php'; @source '../**/*.js';

@import 'tailwindcss/preflight'; @tailwind utilities;

.catpc {
  width: 100px;   height: 125px;   margin: 0 auto;   object-fit: contain;   background-image: url('https://res.cloudinary.com/cloudinarymen/image/upload/v1743358609/makotodecor/backgrounds/catpc_y5dnxd.png');
  -webkit-animation: moveXpc 1s steps(32) infinite;   -moz-animation: moveXpc 1s steps(32) infinite;   -o-animation: moveXpc 1s steps(32) infinite;   animation: moveXpc 1s steps(32) infinite;
}

.catmobile {
  width: 50px;   height: 63px;   margin: 0 auto;   object-fit: contain;   background-image: url('https://res.cloudinary.com/cloudinarymen/image/upload/v1743358607/makotodecor/backgrounds/catmobile_jmmbjw.png');
  -webkit-animation: moveXmobile 1s steps(32) infinite;   -moz-animation: moveXmobile 1s steps(32) infinite;   -o-animation: moveXmobile 1s steps(32) infinite;   animation: moveXmobile 1s steps(32) infinite;
}

and component like this:

import React from 'react'

const Cat = () => {
  return <div className="catmobile sm:catpc"></div>
}

export default Cat

Solution

  • What's layers

    Since v4, CSS layers play a significant role.

    The @layer CSS at-rule is used to declare a cascade layer and can also be used to define the order of precedence in case of multiple cascade layers.

    Unlayered styles like .catpc and .catmobile will always be stronger than the TailwindCSS classes. Place them in the base/components layer to make them weaker.

    @import 'tailwindcss';
    
    @layer base {
      .catpc {
        /* ... */
      }
      
      .catmobile {
        /* ... */
      }
    }
    

    Utilities in TailwindCSS

    I don't know the purpose of these two classes. But yes, you can also use the new @utility directive mentioned by @leodev, which is intended to replace @layer components and @layer utilities. With the @utility directive, TailwindCSS can automatically place your classes into the appropriate place in the compiled CSS.

    @import 'tailwindcss';
    
    @utility catpc {
      /* ... */
    }
      
    @utility catmobile {
      /* ... */
    }