DEV Community

Cover image for TailwindCSS - Guide
Shubham Tiwari
Shubham Tiwari

Posted on

TailwindCSS - Guide

Hello Everyone, today i will discuss some useful points in TailwindCSS with examples.

Here are 10 useful tricks and tips for working with Tailwind CSS:

1. Customizing Colors:

Tailwind CSS provides a default color palette, but you can easily customize it to match your project's branding. In your tailwind.config.js file, you can add or modify the theme.colors section to define your own colors.

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      colors: {
        primary: '#ff0000',
        secondary: '#00ff00',
      },
    },
  },
};
Enter fullscreen mode Exit fullscreen mode

2. Responsive Design:

Tailwind CSS makes it easy to create responsive designs. You can use responsive utility classes like sm, md, and lg to apply different styles based on screen sizes. For example, text-center sm:text-left will center align text on mobile screens and align it to the left on larger screens.

<div className="text-center sm:text-left">Responsive Text</div>
Enter fullscreen mode Exit fullscreen mode

3. Extract Reusable Components:

Tailwind CSS encourages building small, reusable components. You can extract common styles into utility classes and apply them to multiple elements. This
improves consistency and reduces code duplication.

<div className="bg-primary text-white">Reusable Component</div>
Enter fullscreen mode Exit fullscreen mode

4. Purge Unused Styles:

By default, Tailwind CSS includes a large set of utility classes. To keep your final CSS bundle small, you can use the PurgeCSS tool to remove unused styles. Configure PurgeCSS in your build process to only include the styles you're actually using.

5. Dark Mode Support:

Tailwind CSS includes built-in dark mode support. You can enable it in your tailwind.config.js file and use the dark: prefix to apply specific styles for dark mode. For example, dark:bg-gray-900 will set the background color to a dark gray in dark mode.

<div className="bg-white dark:bg-gray-900">Dark Mode Background</div>
Enter fullscreen mode Exit fullscreen mode

6. Hover and Focus Styles:

Tailwind CSS provides utility classes for hover and focus states. You can easily apply different styles when an element is hovered over or focused. For example, hover:bg-blue-500 will change the background color to blue when the element is hovered.

<button className="bg-blue-500 hover:bg-blue-700">Hover Me</button>
Enter fullscreen mode Exit fullscreen mode

7. Flexbox and Grid Utilities:

Tailwind CSS offers utility classes for creating flexible layouts using flexbox and grid. You can use classes like flex, flex-wrap, justify-center, items-center, grid, grid-cols-2, etc., to quickly build responsive layouts without writing custom CSS.

<div className="flex justify-center items-center">Flexbox Center</div>

<div className="grid grid-cols-2 gap-4">Grid Layout</div>
Enter fullscreen mode Exit fullscreen mode

8. Spacing Utilities:

Tailwind CSS has a range of spacing utility classes for adding margins and padding to elements. You can use classes like m-4 (margin), p-4 (padding), mx-auto (horizontal margin auto), etc., to easily control the spacing of elements.

<div className="m-4">Margin</div>
<div className="p-4">Padding</div>
Enter fullscreen mode Exit fullscreen mode

9. Typography Styles:

Tailwind CSS includes a set of utility classes for typography. You can use classes like text-2xl (font size), font-bold, text-center, text-red-500, etc., to quickly style your text without writing custom CSS.

<div className="text-2xl font-bold text-center text-red-500">Styled Text</div>
Enter fullscreen mode Exit fullscreen mode

10. Extensions and Plugins:

Tailwind CSS has a rich ecosystem of extensions and plugins that provide additional functionality. For example, there are plugins for adding forms, tooltips, transitions, and more. Explore the Tailwind CSS documentation and community resources to find useful extensions and plugins.

These are just a few tips to get you started with Tailwind CSS. The framework offers many more features and customization options, so I encourage you to explore the documentation to learn more.

THANK YOU FOR CHECKING THIS POST
You can contact me on -
Instagram - https://www.instagram.com/supremacism__shubh/
LinkedIn - https://www.linkedin.com/in/shubham-tiwari-b7544b193/
Email - shubhmtiwri00@gmail.com

^^You can help me with some donation at the link below Thank youπŸ‘‡πŸ‘‡ ^^
β˜• --> https://www.buymeacoffee.com/waaduheck <--

Also check these posts as well
https://dev.to/shubhamtiwari909/website-components-you-should-know-25nm

https://dev.to/shubhamtiwari909/smooth-scrolling-with-js-n56

https://dev.to/shubhamtiwari909/swiperjs-3802

https://dev.to/shubhamtiwari909/custom-tabs-with-sass-and-javascript-4dej

Top comments (3)

Collapse
 
felistus profile image
Obieze Ezeugo Felistus

I find this modifier (sm:) irrelevant.
Just a self-thought, though.
100% of my projects, I have never used it.

Please, can you show an example on how to use the Purge config?
Thanks

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

tailwind.config.js

module.exports = {
purge: [
'./src//*.html',
'./src/
/*.js',
// Add any other file types that contain Tailwind CSS classes
],
// Other Tailwind CSS configuration options...
};

Package.json
"scripts": {
"build:css": "postcss src/styles.css -o dist/styles.css"
}

CMD
npm run build:css

The build process will analyze your HTML and JavaScript files based on the configured paths in the purge array of your tailwind.config.js file. It will remove any unused CSS classes and generate a minified CSS file in the dist directory (or any other output directory you specify).

Collapse
 
felistus profile image
Obieze Ezeugo Felistus

thank you so much for this