views
Tailwind With React
If you want to learn more about Node.js, then The Complete Node.js Developer Course is a great course, to begin with. Tailwind is a CSS library in a similar area to such things as Bootstrap or Bulma. Tailwind is distinctive that as opposed to providing CSS for complete components, it affords low-stage utility training. What this indicates is that instead of the use of class=“button” or “card” or something else, you’ll define your button using composing Tailwind’s utility instructions.
For instance, we’ll look at some HTML of a card created with Bootstrap after which a card created with Tailwind.
Bootstrap -
<!-- from the Bootstrap documentation https://getbootstrap.com/docs/4.0/components/card/--><div class="card" style="width: 18rem;"> <img class="card-img-top" src="https://www.fillmurray.com/300/300" alt="Card image cap" /> <div class="card-body"> <h5 class="card-title">Card title</h5> <p class="card-text"> Some quick example text to build on the card title and make up the bulk of the card's content. </p> <a href="#" class="btn btn-primary">Go somewhere</a> </div></div>
Tailwind -
<div class="w-64 rounded overflow-hidden shadow-lg"> <img class="w-full" src="https://www.fillmurray.com/300/300" alt="Bill Murray Placeholder" /> <div class="px-6 py-4"> <div class="font-bold text-xl mb-2">Card Title</div> <p class="text-gray-700 text-base"> Some quick example text </p> </div> <div class="px-6 py-4"> <button class="bg-blue-800 py-2 px-2 rounded text-white"> Go Somewhere </button> </div></div>
you can have noticed that the Tailwind card has greater verbose CSS magnificence properties. but, we will now modify how our component appears without converting CSS at once.
for instance, if we wanted to give the cardboard a history, we could follow a bg-color class to our starting div:... The bg-indigo-three hundred magnificence is one instance of a Tailwind software magnificence.
Follow for more info at Reactjs Online Training
With React
We’re going, to begin with, a default Create React task.
> npx create-react-app react-tailwind-example
Next, we'll add a couple of dependencies
> yarn add tailwindcss tailwind.macro@next @emotion/core @emotion/styled
If you prefer styled-components, you could include that instead of @emotion/core @emotion/styled
- Tailwind is the Tailwind library.
- Tailwind. Macro is a Babel plugin macro for using a babel-plugin-tailwind-components package without additional configuration. Please see kentcdodds/babel-plugin-macros if you're interested in learning more about Babel plugin macros.
- @emotion/core | @emotion/styled - The css-in-js library we're using in this example.
We’re ready to start writing some example code that uses these libraries. We'll replace App.js with the following code:
import React from "react";import styled from "@emotion/styled";import tw from "tailwind.macro";const Button = styled.button` ${tw`bg-gray-300 text-yellow-900 px-8 m-8 rounded h-20 text-3xl`}`;export default function() { return <Button>Testing</Button>;}
Example output:
The styled component, Button, is using the Tailwind Macro two to apply utility classes for things like background color, rounded corners, font-size, etc. Combining Tailwind with Emotion or styled-components allows us to build flexible components quickly.
Now, we can finally use some CSS classes from Tailwind in our React application.
In src/App.js file:
1. Change className=”App” to className=”text-center”.
2. Change className=”App-header” to className=”bg-purple-darker m-6 p-6 rounded shadow-lg”
3. Change className=”App-intro” to className=”text-base”
Your render() method in App.js file should look like this:
<div className="text-center">
<header className="bg-purple-darker m-6 p-6 rounded shadow-lg">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="text-white text-3xl">Welcome to React</h1>
</header>
<p className="text-base">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
If you want to learn more about Reactjs, then The Complete Recatjs Certification Course is a great course, to begin with.