DEV Community

Annoske
Annoske

Posted on

How to use Tailwindcss in React.

About this Article

This is an article about using taillwind in react.

What is Tailwind?

Tailwind is a CSS flamework.
As show below, you can add class to element,and adopt style.
https://tailwindcss.com/
screen.gif

How to install

installing package

npm install -D tailwindcss@npm:@tailwindcss/postcss7-compat @tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9
Enter fullscreen mode Exit fullscreen mode
npm install postcss-cli autoprefixer --save-dev
or
yarn add postcss-cli autoprefixer --save-dev
Enter fullscreen mode Exit fullscreen mode

create styles.css

Create style.css and import @tailwind property.

@tailwind base;

@tailwind components;

@tailwind utilities;
Enter fullscreen mode Exit fullscreen mode

Add script command to build Tailwind in package.json.

"scripts": {
    "start": "react-scripts start",
    "build:tailwind":"tailwind build src/styles.css -o src/tailwind.css",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
Enter fullscreen mode Exit fullscreen mode

And run below.

npm run-script build:tailwind
Enter fullscreen mode Exit fullscreen mode

Then,tailwind.css will be created.

import tailwind.css

import './tailwind.css';
Enter fullscreen mode Exit fullscreen mode

Check importing.

If you write the following,you can confirm tailwind.css

import React from "react";

function App() {
  return (
    <div className="App">
      <div class="max-w-sm rounded overflow-hidden shadow-lg">
        <img
          class="w-full"
          src="https://source.unsplash.com/random/1600x900/"
          alt="Sunset in the mountains"
        ></img>
        <div class="px-6 py-4">
          <div class="font-bold text-xl mb-2">The Coldest Sunset</div>
          <p class="text-gray-700 text-base">
            Lorem ipsum dolor sit amet, consectetur adipisicing elit.
            Voluptatibus quia, nulla! Maiores et perferendis eaque,
            exercitationem praesentium nihil.
          </p>
        </div>
        <div class="px-6 py-4">
          <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2">
            #photography
          </span>
          <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2">
            #travel
          </span>
          <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700">
            #winter
          </span>
        </div>
      </div>
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

The result is as follows.
image.png

参考記事

https://blog.logrocket.com/create-react-app-and-tailwindcss/

Top comments (0)