@@ -6,14 +6,15 @@ author: Pentakota Avilash
6
6
lastmod : 2024-05-16T13:12:12-07:00
7
7
tags :
8
8
- react
9
- - tailwind
9
+ - tailwind-css
10
10
draft : false
11
11
---
12
12
13
13
** Authors** :
14
14
15
15
- [ Kulsoom Nisha] ( /authors/kulsoom-nisha/ )
16
16
- [ Pentakota Avilash] ( /authors/pentakota-avilash/ )
17
+ - [ Geeth Sowri] ( /authors/geeth-sowri/ )
17
18
18
19
---
19
20
@@ -82,68 +83,69 @@ React and Tailwind CSS have become highly popular in the web development communi
82
83
- ** PurgeCSS** : Enable PurgeCSS in your Tailwind configuration to remove unused CSS in production, reducing file size.
83
84
- ** JIT Mode** : Use Just-in-Time (JIT) mode for faster build times and on-demand generation of styles.
84
85
85
- ### Integrating React with Tailwind CSS
86
+ ## Integrating React with Tailwind CSS
86
87
87
- #### Setup
88
+ ### Setup
88
89
89
- 1 . ** Install Tailwind CSS** using npm or yarn.
90
+ 1 . Install Tailwind CSS using npm or yarn.
90
91
91
- ``` bash
92
- npm install tailwindcss
93
- npx tailwindcss init
94
- ```
92
+ ``` sh
93
+ npm install tailwindcss
94
+ npx tailwindcss init
95
+ ```
95
96
96
- 2. ** Configuration** :
97
- Configure ` tailwind.config.js` and ` postcss.config.js` to include Tailwind’s directives. Import Tailwind styles in your main CSS file.
97
+ 2 . Configuration:
98
+ Configure tailwind.config.js and postcss.config.js to include Tailwind’s directives.
99
+ Import Tailwind styles in your main CSS file.
98
100
99
- ` ` ` css
100
- @tailwind base;
101
- @tailwind components;
102
- @tailwind utilities;
103
- ` ` `
104
-
105
- # ### Usage in Components
101
+ ``` css
102
+ @tailwind base;
103
+ @tailwind components;
104
+ @tailwind utilities;
105
+ ```
106
106
107
+ 3 . Usage in Components:
107
108
Apply Tailwind’s utility classes directly in React components.
108
109
109
- ` ` ` javascript
110
- import React from ' react' ;
110
+ ``` javascript
111
111
112
- const Button = () => (
113
- < button className=" bg-blue-500 text-white font-bold py-2 px-4 rounded" >
114
- Click Me
115
- < /button>
116
- );
112
+ import React from ' react' ;
117
113
118
- export default Button;
119
- ` ` `
114
+ const Button = () => (
115
+ < button className= " bg-blue-500 text-white font-bold py-2 px-4 rounded" >
116
+ Click Me
117
+ < / button>
118
+ );
120
119
121
- # ### Custom Components
120
+ export default Button ;
121
+ ```
122
+
123
+ ## Custom Components
122
124
123
125
Create reusable components with Tailwind CSS.
124
126
125
127
``` javascript
128
+
126
129
const Card = ({ title, description }) => (
127
130
< div className= " max-w-sm rounded overflow-hidden shadow-lg" >
128
131
< div className= " px-6 py-4" >
129
132
< div className= " font-bold text-xl mb-2" > {title}< / div>
130
- < p className=" text-gray-700 text-base" >
131
- {description}
132
- < /p>
133
+ < p className= " text-gray-700 text-base" > {description}< / p>
133
134
< / div>
134
135
< / div>
135
136
);
136
137
137
138
export default Card ;
138
139
```
139
140
140
- # ### More Examples of React Components Styled with Tailwind CSS
141
+ Here are more examples of React components styled with Tailwind CSS. These examples cover various common UI elements like buttons, forms, modals, and navigation bars.
141
142
142
- # #### Example 1: Button Component
143
+ 1 . Example 1: Button Component
143
144
144
145
A simple button component with different styles for primary and secondary buttons.
145
146
146
147
``` javascript
148
+
147
149
import React from ' react' ;
148
150
149
151
const Button = ({ type = ' primary' , children, onClick }) => {
@@ -163,11 +165,12 @@ const Button = ({ type = 'primary', children, onClick }) => {
163
165
export default Button ;
164
166
```
165
167
166
- # #### Example 2: Form Component
168
+ 2 . Example 2: Form Component
167
169
168
170
A form component with input fields styled using Tailwind CSS.
169
171
170
172
``` javascript
173
+
171
174
import React , { useState } from ' react' ;
172
175
173
176
const Form = () => {
@@ -220,7 +223,7 @@ const Form = () => {
220
223
export default Form ;
221
224
```
222
225
223
- # #### Example 3: Modal Component
226
+ 3 . Example 3: Modal Component
224
227
225
228
A modal component that can be toggled open and closed.
226
229
@@ -264,11 +267,12 @@ const App = () => {
264
267
export default App ;
265
268
```
266
269
267
- # #### Example 4: Navigation Bar Component
270
+ 4 . Example 4: Navigation Bar Component
268
271
269
272
A responsive navigation bar component.
270
273
271
274
``` javascript
275
+
272
276
import React from ' react' ;
273
277
274
278
const NavBar = () => {
@@ -277,18 +281,10 @@ const NavBar = () => {
277
281
< div className= " container mx-auto flex justify-between items-center" >
278
282
< div className= " text-white text-lg font-semibold" > MyApp< / div>
279
283
< div className= " hidden md:flex space-x-4" >
280
- < a href=" #home" className=" text-gray-300 hover:text-white" >
281
- Home
282
- < /a>
283
- < a href=" #about" className=" text-gray-300 hover:text-white" >
284
- About
285
- < /a>
286
- < a href=" #contact" className=" text-gray-300 hover:text-white" >
287
- Contact
288
- < /a>
289
-
290
-
291
- < /div>
284
+ < a href= " #home" className= " text-gray-300 hover:text-white" > Home< / a>
285
+ < a href= " #about" className= " text-gray-300 hover:text-white" > About< / a>
286
+ < a href= " #contact" className= " text-gray-300 hover:text-white" > Contact< / a>
287
+ < / div>
292
288
< / div>
293
289
< / nav>
294
290
);
@@ -297,11 +293,12 @@ const NavBar = () => {
297
293
export default NavBar ;
298
294
```
299
295
300
- # #### Example 5: Card Component
296
+ 1 . Example 5: Card Component
301
297
302
298
A card component for displaying content in a structured format.
303
299
304
300
``` javascript
301
+
305
302
import React from ' react' ;
306
303
307
304
const Card = ({ title, description }) => {
@@ -314,8 +311,6 @@ const Card = ({ title, description }) => {
314
311
< / div>
315
312
);
316
313
};
317
-
318
- export default Card;
319
314
```
320
315
321
316
### Component-Driven Development
0 commit comments