A tu propio ritmo

Explora nuestra extensa colección de cursos diseñados para ayudarte a dominar varios temas y habilidades. Ya seas un principiante o un aprendiz avanzado, aquí hay algo para todos.

Bootcamp

Aprende en vivo

Únete a nosotros en nuestros talleres gratuitos, webinars y otros eventos para aprender más sobre nuestros programas y comenzar tu camino para convertirte en desarrollador.

Próximos eventos en vivo

Catálogo de contenidos

Para los geeks autodidactas, este es nuestro extenso catálogo de contenido con todos los materiales y tutoriales que hemos desarrollado hasta el día de hoy.

Tiene sentido comenzar a aprender leyendo y viendo videos sobre los fundamentos y cómo funcionan las cosas.

Buscar en lecciones


← Regresar a lecciones

Styling React with Tailwind CSS

Core Concepts

Let's see a quick example of a Tailwind-styled button:

1function Button() { 2 return ( 3 <button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"> 4 Click me 5 </button> 6 ); 7}

Core Concepts

Utility Classes

Tailwind provides classes for nearly everything:

1<div className="flex items-center justify-between p-4 bg-white shadow rounded-lg"> 2 <h2 className="text-xl font-bold text-gray-800">User Profile</h2> 3 <button className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"> 4 Edit 5 </button> 6</div>

Responsive Design

Use breakpoint prefixes for responsive layouts:

1<div className="w-full md:w-1/2 lg:w-1/3 p-4"> 2 {/* Full width on mobile, half on tablet, third on desktop */} 3</div>

Common breakpoints:

  • sm: 640px
  • md: 768px
  • lg: 1024px
  • xl: 1280px

State Variants

Style different states with variant prefixes:

1<button className="bg-blue-500 hover:bg-blue-700 focus:ring-2 focus:ring-blue-300"> 2 Hover and focus effects 3</button>

Building Components

Let's create a card component with Tailwind:

1function Card({ title, description, imageUrl }) { 2 return ( 3 <div className="max-w-sm rounded overflow-hidden shadow-lg"> 4 <img className="w-full" src={imageUrl} alt={title} /> 5 <div className="px-6 py-4"> 6 <div className="font-bold text-xl mb-2">{title}</div> 7 <p className="text-gray-700 text-base">{description}</p> 8 </div> 9 </div> 10 ); 11}

Managing Complex UIs

As your components grow more complex, consider these strategies:

1. Extract Component Classes

1function Button({ color = "blue", children }) { 2 const colorVariants = { 3 blue: "bg-blue-500 hover:bg-blue-700 text-white", 4 red: "bg-red-500 hover:bg-red-700 text-white", 5 green: "bg-green-500 hover:bg-green-700 text-white" 6 }; 7 8 return ( 9 <button className={`font-bold py-2 px-4 rounded ${colorVariants[color]}`}> 10 {children} 11 </button> 12 ); 13}

2. Use the @apply Directive

Extract repeated class patterns in your CSS file:

1@tailwind base; 2@tailwind components; 3 4@layer components { 5 .btn-primary { 6 @apply py-2 px-4 bg-blue-500 text-white font-bold rounded hover:bg-blue-700; 7 } 8} 9 10@tailwind utilities;

Then use your custom class:

1<button className="btn-primary">Click me</button>

3. Use Libraries Like clsx

Manage conditional classes elegantly:

1import clsx from 'clsx'; 2 3function Alert({ type, children }) { 4 return ( 5 <div className={clsx( 6 'p-4 rounded border', 7 { 8 'bg-blue-100 border-blue-500 text-blue-700': type === 'info', 9 'bg-green-100 border-green-500 text-green-700': type === 'success', 10 'bg-red-100 border-red-500 text-red-700': type === 'error' 11 } 12 )}> 13 {children} 14 </div> 15 ); 16}

Customizing Tailwind

Extend Tailwind's default theme in tailwind.config.js:

1module.exports = { 2 theme: { 3 extend: { 4 colors: { 5 brand: { 6 light: '#FFDD94', 7 DEFAULT: '#FFC045', 8 dark: '#E0A62C', 9 }, 10 }, 11 spacing: { 12 '128': '32rem', 13 } 14 } 15 } 16}

Then use your custom values:

1<div className="text-brand bg-brand-light p-128"> 2 Custom styling 3</div>

Performance Optimization

Tailwind can generate thousands of utility classes, but you'll only use a fraction. The content configuration in tailwind.config.js ensures only used classes are included in production:

1module.exports = { 2 content: [ 3 './src/**/*.{js,jsx,ts,tsx}', 4 ], 5 theme: { 6 extend: {}, 7 }, 8 plugins: [], 9}

Conclusion

Tailwind CSS offers a rapid, intuitive way to style React applications without context switching between files. While the utility-first approach may seem verbose initially, it provides excellent developer experience and consistent designs. The component abstraction capabilities make it suitable for both small projects and large-scale applications.

Additional Resources

  • Tailwind CSS Documentation
  • Tailwind CSS Cheat Sheet
  • React + Tailwind Integration Guide