Tailwind CSS: What you need to know about it and how to use it with HTML, Reactjs, and NextJs.

Tailwind CSS: What you need to know about it and how to use it with HTML, Reactjs, and NextJs.

Introduction

Tailwind CSS is a utility-first CSS framework that allows developers to build responsive and modern web designs easily. It provides a wide range of pre-designed classes that can be used to style HTML elements without writing any CSS code. This article will cover everything you need to know about Tailwind CSS, including how to use it with HTML, Reactjs, and NextJs.

What is Tailwind CSS?

Tailwind CSS is a CSS framework that focuses on providing pre-designed classes for building user interfaces. Unlike other CSS frameworks that focus on a predefined set of styles, Tailwind CSS provides a set of utility classes that can be used to style any HTML element. This approach makes it highly customizable and flexible, allowing developers to build designs that match their specific needs.

Tailwind CSS is built using PostCSS, which is a CSS preprocessor that allows you to use modern CSS features such as variables and nesting. It also supports features like PurgeCSS, which removes any unused CSS from your final build, resulting in a smaller file size.

How to use Tailwind CSS with HTML?

Using Tailwind CSS with HTML is straightforward. All you need to do is add the Tailwind CSS stylesheet to your HTML file and start using the pre-designed classes to style your elements. Here's an example:

<!DOCTYPE html>
<html>
<head>
  <title>My Tailwind CSS website</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/tailwindcss@2.1.4/dist/tailwind.min.css">
</head>
<body>
  <div class="bg-gray-200 p-4">
    <h1 class="text-3xl font-bold text-center">Hello, world!</h1>
    <p class="text-lg mt-4">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  </div>
</body>
</html>

In the example above, we added the Tailwind CSS stylesheet to the head section of our HTML file using a CDN link. We then used the pre-designed classes to style our elements. The bg-gray-200 Class sets the background color to gray; the p-4 class adds a padding of 4 units; the text-3xl class sets the font size to 3xl; the font-bold class sets the font weight to bold, and so on.

How to use Tailwind CSS with Reactjs?

Using Tailwind CSS with Reactjs requires a few extra steps. First, you need to install the Tailwind CSS package using npm or yarn, but we will use npm here**.** You also need to configure PostCSS to process your CSS files. Here's how to do it:

  • Install the Tailwind CSS package:
npm install tailwindcss
  • Create a tailwind.config.js file in the root of your project:
module.exports = {
  mode: 'jit',
  purge: [
    './src/**/*.{js,jsx,ts,tsx}',
    './public/index.html',
  ],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}

In the tailwind.config.js file, we set the mode to jit to enable just-in-time compilation, which improves the build time. We also configure PurgeCSS to remove any unused CSS from our final build.

  • Create a postcss.config.js file in the root of your project:
module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

In the postcss.config.js file, we configure PostCSS to use the Tailwind CSS and Autoprefixer plugins.

  • Import the Tailwind CSS styles into your React component:
import React from 'react';
import './styles.css';

function App() {
  return (
    <div className="bg-gray-200 p-4">
      <h1 className="text-3xl font-bold text-center">Hello, world!</h1>
      <p className="text-lg mt-4">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  );
}

export default App;

In the example above, we imported the styles.css file that contains the Tailwind CSS classes we want to use in our component.

  • Start your application and see the changes:
npm start

How to use Tailwind CSS with NextJs?

Using Tailwind CSS with NextJs requires a similar setup to using it with Reactjs. However, NextJs provides some built-in features that make it easier to integrate with Tailwind CSS.

  • Install the Tailwind CSS and PostCSS packages:
npm install tailwindcss postcss autoprefixer
  • Create a tailwind.config.js file in the root of your project:
module.exports = {
  mode: 'jit',
  purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
};

In the tailwind.config.js file, we set the mode to jit to enable just-in-time compilation, which improves the build time. We also configure PurgeCSS to remove any unused CSS from our final build.

  • Create a postcss.config.js file in the root of your project:
module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
};

In the postcss.config.js file, we configure PostCSS to use the Tailwind CSS and Autoprefixer plugins.

  • Create an _app.js file in the pages directory:
import '../styles/globals.css';
import 'tailwindcss/tailwind.css';

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />;
}

export default MyApp;

In the _app.js file, we import the global styles and the Tailwind CSS styles.

  • Start your application and see the changes:
npm run dev

Conclusion

Tailwind CSS is a powerful CSS framework that can help developers easily build modern and responsive web designs. This guide covered everything you need to know about Tailwind CSS, including how to use it with HTML, Reactjs, and NextJs. With Tailwind CSS, you can write less CSS and focus more on building great user interfaces.