Just like bootstrap, take advantage of existing CSS classes rather than writing your own when reasonable. Here are some nice (free!) component libraries that use Tailwind:
Useful gradient tutorial
https://blog.logrocket.com/guide-adding-gradients-tailwind-css/#adding-text-gradients
Handling hover, dark mode, and more
Tailwind allows you to use modifiers for various classes. Think of them as if
statements. For example hover:bg-sky-700
would use the bg-sky-700
on hover. Additionally, these can be stacked for more specific situations like so: dark:md:hover:bg-fuchsia-600
.
What modifiers are available? (examples)
- Pseudo-classes:
:hover
,:focus
,:first-child
, and:required
- Pseudo-elements:
::before
,::after
,::placeholder
, and::selection
- Media and feature queries, like responsive breakpoints (
md
,lg
), dark mode, andprefers-reduced-motion
Base
p-#
- paddingpx-#, py-#,
- padding top/bottom, padding left/rightspace-x-#, space-y-#
- space between child elements (by adding a margin to each child)relative, absolute, static, fixed
- position classesitems-center, items-start, ...
- setsalign-items
leading-#
- line heightbg-red-500-5
- color value ofred-500
and opacity of 5%
Height/Width
h-#, w-#
- height, width (4 is 1 rem or 16px)h-full, w-full
- stands for 100%max-w-#, max-h-#
- max width/height
Flex
flex
- assignsdisplay: flex
flex-col, flex-row, flex-??-reverse
- sets the flex directionflex-wrap
- allows wrappingflex-nowrap
- allows overflowing
Display
block
- separate line, fill parentinline-block
- wrap text (to prevent extending beyond parent)inline
- wrap normallyflex
- block level flex containerinline-flex
- flows with texttable
grid
hidden
Grid
grid
grid-cols-#
- num columnsgap-#
- gap between columnscol-span-#
- how many columns the element will take up
Animation
ease-in, ease-out, ease-in-out
Tidbits
You can apply tailwind classes in a CSS rule (as opposed to html class) directly using the @apply
directive. For example @apply border border-red-500/50 bg-red-500/5;
.
Config
The tailwind.config.mjs
can be used to extend or directly change the existing default configuration. It should look something like this:
export default {
content: ["./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}"],
theme: {
colors: {
'blue': '#1fb6ff',
},
extend: {
colors: {
banana: ('--banana'),
'pthalo-green': '#123524',
}
}
Notice how this both modifies an existing color (blue), and adds two new ones in two different ways.
- A standard object key
banana
, with works without quotes. Additionally, rather than being assigned an explicit value, it uses a CSS variable--banana
from the file(s) that use this class. - A string, which uses quotes. Good for reserved words or special characters.
Tailwind will automatically generate these classes based on a relevant prefix. For example,colors
is associated withbg-*
,text-*
,border-*
and will fill in that wildcard making classes likebg-pthalo-green
andtext-banana
. You may also nest these definitions as needed to create more variations likebg-banana-ripe
andbg-banana-unripe
.