DEV Community

Cover image for Tailwind CSS Crash Course
Sabbir Zzaman
Sabbir Zzaman

Posted on

Tailwind CSS Crash Course

Introduction

Tailwind CSS is a highly popular and innovative utility-first CSS framework utility-first CSS framework that simplifies web development with ready to use utility classes.

What's you are going to learn?

  1. Colors: Effortlessly apply a diverse range of colors.
  2. Flex Layout: Create fluid and responsive arrangements.
  3. Grid layout: Construct organized multi-column designs.
  4. Specing: Achieve precise spacing through margin and padding utilities.
  5. Sizing: Exercise complete control over element dimensions.
  6. Customization: Infuse your unique style by extending utility classes.
  7. Responsiveness: Craft adaptive interfaces with breakpoints and modifiers.

🎨 Colors

Inherit, current, transparent, black, white, grey, zinc, natural, slate, red, orange, amber, yellow, lime, green, emerald, teal, cyan, sky, blue, indigo, violet, purple, fuchsia, pink, rose

For most colors (except the first 5), you get a scale from 100 to 900, going up by 100 each time (with a larger jump at the end, reaching 950). This gives you a bunch of choices for each color, like light gray (gray-100), medium gray (gray-500), or dark gray (gray-900). If that's not enough, you can even add your own favorite colors by tweaking Tailwind CSS's settings. This way, your website can have exactly the colors you want, making it uniquely yours.

<nav class="bg-blue-100">
  <a href="#" class="text-blue-500 hover:text-blue-700">Home</a>
  <a href="#" class="text-blue-300 hover:text-blue-500">About</a>
  <a href="#" class="text-blue-200 hover:text-blue-400">Services</a>
</nav>
Enter fullscreen mode Exit fullscreen mode

🛠️ Flex layout

A tool for organizing elements in a straight line, whether you want them side by side or in a row. This comes in especially handy when you're working on layouts that need to adapt and look great on various devices.

Let's say you're building a card component for a product showcase. By using flex layout, you can easily align the card's image, title, and description horizontally. Here's a simplified code snippet in HTML and Tailwind CSS:

<div class="flex">
  <img src="product-image.jpg" class="w-1/3">
  <div class="flex-1 p-4">
    <h2 class="text-lg font-semibold">Product Name</h2>
    <p class="mt-2">Description of the product goes here...</p>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

In this example, the "flex" class creates a flex container, making the image and the content inside it flex items. The "w-1/3" class sets the image's width to one-third of the container's width, while "flex-1" ensures the content takes up the remaining space. The padding and margin classes (p-4 and mt-2) add spacing for a clean and organized appearance.

⚒️ Grid layout

A powerful tool that lets you create organized designs using rows and columns, providing meticulous control over how elements are positioned. This becomes particularly handy when you're aiming for a well-structured layout, ensuring that your content aligns perfectly and consistently.

Imagine you're crafting a gallery page with images. By grid layout, you can arrange the images into a neat grid pattern, making sure each one occupies its designated space while maintaining an appealing visual rhythm. Here's a basic code snippet in HTML and Tailwind CSS to illustrate:

<div class="grid grid-cols-3 gap-4">
  <img src="image1.jpg" class="w-full">
  <img src="image2.jpg" class="w-full">
  <img src="image3.jpg" class="w-full">
  <!-- Add more images as needed -->
</div>
Enter fullscreen mode Exit fullscreen mode

In this example, the "grid" class creates a grid container, and "grid-cols-3" divides the container into three equally sized columns. The "gap-4" class adds a bit of spacing between the images for a polished look. Each image is assigned "w-full" to ensure it spans the full width of its grid cell.

📏 Specing

Tailwind CSS introduces practical classes that allow you to precisely manage the space around and within your elements. These classes, such as m-{size}, p-{size}, mx-{size}, and my-{size}, enable you to easily define margin and padding sizes, achieving the desired spacing in your design. The {size} Can be defined using a scale ranging from 0 to 96 representing multiples of 0.25rem.

Here's a code snippet in HTML and Tailwind CSS to exemplify:

<div class="bg-white p-4 m-2">
  <img src="product-image.jpg" class="w-full">
  <h2 class="text-lg font-semibold mt-2">Product Name</h2>
  <p class="text-gray-600 mt-1">Product description goes here...</p>
</div>
Enter fullscreen mode Exit fullscreen mode

In this example, the "p-4" class adds padding of size 4 (16px) to the product entry. The "m-2" class adds margin of size 2 (8px) around the entire entry.

📐 Sizing

Tailwind CSS simplifies element dimensions through classes like w-{size}, h-{size}, min-w-{size}, and max-w-{size}. These classes grant you precise control over how big or small your elements should be. The {size} can be defined using fixed sizes, percentages, or using the responsive sizing approach.

Imagine you're crafting a profile card. With Tailwind CSS, you can effortlessly determine the width, height, minimum width, and maximum width of the card. Here's a straightforward code snippet in HTML and Tailwind CSS to illustrate:

<div class="bg-white p-4 w-72 h-96">
  <img src="profile-image.jpg" class="w-full h-48">
  <h2 class="text-lg font-semibold mt-2">John Doe</h2>
  <p class="text-gray-600 mt-1">Web Developer</p>
</div>
Enter fullscreen mode Exit fullscreen mode

In this example, the "w-72" class sets the card's width to 18rem, while "h-96" gives it a height of 24rem. The profile image uses "w-full" to span 100% width of its container and "h-48" for a height of 12rem.

🔧 Customization

Tailwind CSS empowers your creativity with the ability to extend and add your own unique utility classes, enabling you to effortlessly tailor the framework to match your design preferences and branding.

Let's say you need to add custom colors. You can easily define your custom color in the configuration file:

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      colors: {
        customBlue: '#007BFF',
      },
    },
  },
  variants: {},
  plugins: [],
}
Enter fullscreen mode Exit fullscreen mode

Now, you can use your custom color in your HTML and Tailwind CSS classes:

<button class="bg-customBlue text-white font-bold py-2 px-4 rounded-full hover:bg-blue-700">Click Me</button>
Enter fullscreen mode Exit fullscreen mode

🖥️ Responsiveness

Tailwind CSS offers a ready-made toolkit of breakpoints for various screen sizes. You can also tweak tailwind default breakpoints classes from the configuration file (tailwind.config.js).

  • Predefined Breakpoints: Tailwind CSS comes with predefined breakpoints, tailored to different device screens.
  • Responsive Classes: Utilize classes like sm, md, lg, xl, and 2xl to apply styles precisely at specific breakpoints.

Apply responsive design effortlessly — for example, sm: text-lg adjusts font size starting from the small screen breakpoint.

With Tailwind CSS, crafting responsive designs that adapt to diverse devices becomes a seamless endeavor.

💡 Conclusion

Tailwind CSS is a powerful toolkit that simplifies web design. It offers easy ways to manage colors, arrange layouts, control spacing, and ensure your design works well on all devices. It's like having a versatile toolbox for creating beautiful and customized websites without the hassle. Learning Tailwind CSS opens the door to crafting websites that look great, work smoothly, and reflect your own unique style.

Thank you and happy designing!

Top comments (3)

Collapse
 
devrx profile image
Abhinav Pandey

This is like a bread full of good cheese. Great going!!

Collapse
 
sabbir_zz profile image
Sabbir Zzaman

Thank you so much for your kind words!

Collapse
 
ionisingtuna profile image
Brendan Mannion (IonisingTuna)

Typo: Specing