Blog

Zhuravkov Igor / Porfolio

Zhuravkov Igor

B. 2000

Visit

Tips

Tailwind css tips and tricks

Made by Tailwindcss communityMade by Tailwindcss community

Made by Tailwindcss community

Tailwind CSS is a utility-first CSS framework that allows you to build custom designs without ever leaving your HTML. It provides a set of pre-defined classes that you can use to style your elements. In this article, we will look at some tips and tricks for using Tailwind CSS.

1. Use the @apply directive to create custom classes

The @apply directive allows you to create custom classes that can be used in your HTML. You can use it to create classes for common styles such as text color, font size, padding, margin, etc. For example, if you want to create a class for a button with a blue background and white text, you can do this:

1@tailwind base;
2
3@tailwind components;
4
5@tailwind utilities;
6
7.btn {
8  @apply bg-blue-500 text-white;
9}

2. Use the @layer directive to group related styles

The @layer directive allows you to group related styles into layers. This is useful when you have a lot of styles and want to organise them into logical groups. For example, if you have a lot of styles for buttons, you can group them into a layer called buttons:

1@tailwind base;
2
3@tailwind components;
4
5@tailwind utilities;
6
7@layer buttons {
8  .btn {
9    @apply bg-blue-500 text-white;
10  }
11}

3. Use the @responsive directive to create responsive styles

The @responsive directive allows you to create responsive styles that will only be applied when the screen size is within a certain range. For example, if you want to create a class for a button with a blue background and white text that will only be applied when the screen size is between 640px and 768px, you can do this:

1@tailwind base;
2
3@tailwind components;
4
5@tailwind utilities;
6
7@responsive {
8  .btn {
9    @apply bg-blue-500 text-white;
10  }
11}

4. Use the @variants directive to create variants of existing styles

The @variants directive allows you to create variants of existing styles. For example, if you want to create a class for a button with a blue background and white text that will only be applied when the button is hovered over, you can do this:

1@tailwind base;
2
3@tailwind components;
4
5@tailwind utilities;
6
7@variants hover {
8  .btn {
9    @apply bg-blue-500 text-white;
10  }
11}

5. Use the @screen directive to create custom breakpoints

The @screen directive allows you to create custom breakpoints that can be used in your responsive styles. For example, if you want to create a breakpoint for screens that are 768px wide, you can do this:

1@tailwind base;
2
3@tailwind components;
4
5@tailwind utilities;
6
7@screen 768px {
8  .btn {
9    @apply bg-blue-500 text-white;
10  }
11}

Conclusion

In this article, we looked at some tips and tricks for using Tailwind CSS. We learned how to use the @apply directive to create custom classes, how to use the @layer directive to group related styles, how to use the @responsive directive to create responsive styles, how to use the @variants directive to create variants of existing styles, and how to use the @screen directive to create custom breakpoints. I hope you found this article useful and learned something new. If you have any questions or comments, please leave them in the comments section below. Thank you for reading!

More from the blog