Developer Guides

Tailwind CSS v4 Tutorial with Responsive Design

Learn Tailwind CSS v4 and master responsive design using its utility-first approach.

4 min read

Learn how to set up and utilize Tailwind CSS v4, focusing on responsive design, configuration changes, and best practices tailored to the latest version.

Prerequisites for Tailwind CSS v4 Setup

To get started with Tailwind CSS v4, ensure you meet the following requirements:

prerequisites

  • Basic HTML & CSS Knowledge: Familiarity with basic web development concepts.
  • Code Editor/IDE: Use tools like Visual Studio Code for writing and editing code.
  • Node.js Environment (Optional): Required for a local Tailwind setup if you're not using the online playground.

Steps to Begin with Tailwind CSS v4

Follow these steps to set up and familiarize yourself with Tailwind CSS v4:

steps

  1. Use the Tailwind CSS Playground: Navigate to the Tailwind CSS Playground for instant experimentation without installation.

  2. Install Tailwind CSS Locally: If working locally, ensure Node.js is installed and run the following:

    bash
    npm install -D tailwindcss postcss autoprefixer
    npx tailwindcss init
  3. Configure Tailwind: Use CSS-first configuration in the tailwind.config.cjs file:

    css
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    @layer components {
        .btn {
            @apply bg-blue-500 text-white font-bold py-2 px-4 rounded;
            &:hover {
                @apply bg-blue-700;
            }
        }
    }
  4. Compile Your Tailwind CSS: Compile the updated Tailwind configuration in development mode:

bash
   npx tailwindcss -i ./src/input.css -o ./dist/output.css --watch
  1. Apply Basic Utility Classes:
    • Add a class to an HTML element:
      html
      <h1 class="text-center bg-blue-400">Hello, Tailwind CSS!</h1>
    • Observe the utility-first behavior for spacing, typography, and colors.

Responsive Design with Tailwind

Tailwind CSS v4 adopts a mobile-first approach to responsive design. Breakpoints allow fine-tuned styling for various screen sizes.

steps

  1. Apply Utility Classes: Use breakpoint prefixes such as md:block and lg:hidden for responsive styling:

    html
    <div class="hidden md:block lg:text-lg">Responsive Text Example</div>
  2. Custom Breakpoints: Define unique breakpoints in your CSS configuration:

    css
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    
    @custom-media --large (min-width: 1024px);
  3. Test Responsiveness: Open the project in a browser, resize the window, and confirm elements adjust as intended.

Advanced Configurations in Tailwind CSS v4

In version 4, customization is now done directly in the CSS file, removing the need for JavaScript-based configurations.

steps

  1. Add Custom Themes: Customize colors directly in your CSS using the @layer directive:

    css
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    
    @layer utilities {
        .bg-custom {
            background-color: #ff9800;
        }
    }
  2. Use the @apply Directive: Create reusable utility-based classes:

    css
    @layer components {
        .btn-primary {
            @apply bg-blue-500 text-white hover:bg-blue-700 rounded py-2 px-4;
        }
    }
  3. Enable Dark Mode: Add dark mode styles by prefixing utilities with dark::

    css
    .card {
        @apply bg-white text-black;
        @apply dark:bg-gray-800 dark:text-white;
    }

    To toggle manually: Use a <button> with JavaScript to switch between dark and light modes. Add the dark class to the html element on toggle.

Tips and Tricks for Efficient Tailwind Development

Conclusion and Next Steps

FAQ

How do I use Tailwind CSS for responsive design?

Tailwind CSS uses mobile-first breakpoints with predefined prefixes like sm:, md:, lg:, and xl:. Add these prefixes to utility classes to define styles for specific screen sizes.

What is CSS-first configuration in Tailwind CSS v4?

In Tailwind CSS v4, configuration changes are written directly in the CSS files using @tailwind and @layer directives. This replaces the older JavaScript-based configuration system.

Can I customize breakpoints in Tailwind CSS v4?

Yes, you can define custom breakpoints in the new CSS-first configuration by adding @custom-media queries for different screen sizes. Use these new breakpoints alongside predefined ones.


Official reference: Tailwind CSS documentation.