tailwind
Tailwind merge + clsx
npm i tailwind-mergenpm i clsx
import { twMerge } from "tailwind-merge";
import { clsx, ClassValue } from "clsx";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
Crash course: Essentials
Section 1: Responsiveness
When using breakpoints, the style applies to the specified breakpoint and all sizes above. Behind the scenes, breakpoints just execute media queries.
sm: 640 px and abovemd: 768 px and abovelg: 1024 px and abovexl: 1280 px and above2xl: 1536 px and above
Tailwind is mobile-first, meaning that whatever styles you apply are applied to the smallest screen size first: 0px - infinite px, and then the breakpoints apply.
<!-- base style is small text, but on medium and above sizes, text is large -->
<button class="text-sm md:text-lg">
Section 2: gradients
There are three classes concerned with gradients:
bg-gradient-to-[direction]handles the direction of the gradientfrom-[color]-[shade_weight]defines the starting color of the gradient.to-[color]-[shade_weight]defines the ending color of the gradient.
<div class="h-12 w-24 bg-gradient-to-tr from-cyan-600 to-cyan-100"></div>
Section 3: Adding background images
Although there are no utility classes for background images, you can create a custom class in the tailwind config for a background image.
- In the example below, we create the
bg-hero-imageclass by applying thehero-imagekey in the backgroundImage config.
const heroImageUrl =
'https://img.freepik.com/free-vector/hand-painted-watercolor-pastel-sky-background_23-2148902771.jpg?w=2000'
module.exports = {
theme: {
extend: {
backgroundImage: {
'hero-image': `url('${heroImageUrl}')`,
},
},
},
plugins: [],
}
- We use the
bg-hero-imageclass along with other background utilities to create an effective hero section using a single div.
<div class="bg-hero-image h-screen w-screen bg-cover bg-no-repeat bg-center"></div>
Here are some useful background utilities:
bg-cover: background-size of coverbg-center: background-position of centerbg-no-repeat: background-repeat of no-repeat.
Section 4: animation utility classes
animate-none: no animationanimate-spin: rotation animation, in an infinite circleanimate-bounce: bouncing animation, infinite.animate-ping: pinging animation, infinite.animate-pulse: pulsing animation, infinite.
Section 5: variants
You have variants for dealing with pseudoselectors and states of elements, which are prefixes that apply the styles if in the variant state. The general syntax for the style is as so:
<variant>:<utility-class>
dark: applies class if in dark modehover: applies class if element is being hovered overfocus: applies class if element is currently focusedstarting: applies class using@starting-styledirective for the element
For more info about variants, go here: