1/1/1970
CLI or PostCSSStep 1: Install Tailwind CSS
For CLI:
# Install Tailwind CSS
npm install -D tailwindcss
# Create `tailwind.config.js` file
npx tailwindcss initStep 2: Configure Template Paths
Update tailwind.config.js to include the paths to all your template files:
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./src/**/*.{html,js}"], // Adjust paths as per your project structure
theme: {
extend: {},
},
plugins: [],
}./src/ -> starts from the src directory of your project.**/(glob pattern) -> search recursively in all subdirectories.* ->This means any file, regardless of its name.{js,ts,jsx,tsx} -> defines the file extensions Tailwind should look for.Step 3: Add Tailwind Directives to CSS
Create your main CSS file (e.g., src/index.css) and include Tailwind's layers:
/* src/index.css */
@tailwind base;
@tailwind components;
@tailwind utilities;Step 4: Build Process
For CLI:
# Scan template files for classes and build your CSS
npx tailwindcss -i ./src/index.css -o ./src/output.css --watchFor PostCSS:
# Install PostCSS and Autoprefixer
npm install -D postcss autoprefixer// setup `postcss.config.js`
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}Step 5: Include the Generated CSS
For CLI:
import './output.css';or
<head>
<link href="./output.css" rel="stylesheet">
</head>For PostCSS:
import './index.css';or
<head>
<link href="./index.css" rel="stylesheet">
</head>Using the Tailwind CLI
PostCSS and Autoprefixer are NOT explicitly required because:PostCSS for processing styles, so you don't need to install PostCSS manually.Using Tailwind as a PostCSS Plugin
PostCSS and Autoprefixer are explicitly required because:PostCSS processes the Tailwind directives (@tailwind base, @tailwind components, etc.), and Autoprefixer ensures browser compatibility by adding vendor prefixes.Tailwind CSS Advantage
Advantage over Inline style
Only Important utility classes, and variants are noted, for more check -> flowbite/tailwind
Apply the max-width of an element to fix its width to the current breakpoint.
.container {width: 100%}
x-auto -> center & px-* ->horizontal padding
<div class="container mx-auto px-4">sm, lg etc. -> specific styles at different breakpoints.
<div class="md:container md:mx-auto">Define how the width and height of an element are calculated: should they include padding and borders, or not.
.box-border {box-sizing: border-box};
.box-content {box-sizing: content-box};Use the breakpoints (screen sizes) to set how the utility-classes respond according to the device width.
sm: -> @media (min-width: 640px) { ... }
md: -> @media (min-width: 768px) { ... }
lg: -> @media (min-width: 1024px) { ... }
xl: -> @media (min-width: 1280px) { ... }
2xl: -> @media (min-width: 1536px) { ... }.hidden { display: none; }
.contents { display: contents; }
.list-item { display: list-item; }
.block { display: block; }
.inline { display: inline; }
.flex { display: flex; }
.table { display: table; }
.grid { display: grid; }.float-right {float: right;}
.float-left {float: left;}
.float-none {float: none;}<img> or <video>, should be resized to fit its container..object-contain {object-fit: contain;}
.object-cover {object-fit: cover;}
.object-fill {object-fit: fill;}
.object-none {object-fit: none;}
.object-scale-down {object-fit: scale-down;}Specify the alignment of the selected replaced element's contents within the element's box.
.object-center {object-position: center;}
.object-bottom {object-position: bottom;}
.object-left-top {object-position: left top}top, left, right, left-bottom, right-top, right-topSet the desired behavior for an element's overflow — i.e. when an element's content is too big to fit in its block formatting context — in both directions.
.overflow-auto { overflow: auto; }
.overflow-hidden { overflow: hidden; }
.overflow-x-visible { overflow-x: visible; }
.overflow-y-scroll { overflow-y: scroll; }
.overflow-x-auto { overflow-x: auto; }
.overflow-y-hidden { overflow-y: hidden; }visible, scroll, x-auto, x-hidden, x-scroll, y-auto, y-hidden, y-visible,Scroll Behavior
.scrolling-touch { -webkit-overflow-scrolling: touch; }
.scrolling-auto { -webkit-overflow-scrolling: auto; }Set how an element is positioned in a document.
.static { position: static; }
.fixed { position: fixed; }
.absolute { position: absolute; }
.relative { position: relative; }
.sticky { position: sticky; }Determine the final location of positioned elements.
.inset-0 {top: 0rem; right: 0rem; bottom: 0rem; left: 0rem;}
.inset-x-2.5 {right: 0.625rem; left: 0.625rem;}
.-inset-y-5 {top: -1.25rem; bottom: -1.25rem;}
.inset-x-auto {right:auto; left:auto}-inset-[n], -inset-x-[n], inset-y-[n].top-1/4 {top: 25%;}
.-right-24 {right: -6rem;}
.left-full {left: 100%; }-top-[n], bottom-[n], -left-[n]....x/y direction, t/b/l/r sides, +/- Value, 4xn -> 1 rem4 * [n] -> 1 rem[n] range from 0-96 in increments: 0, 0.5, 1, 1.5, 2, 2.5, 3-16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80, 96[n] values : 1/2 (50%), 1/3 (33.33%), 2/3 (66.67%), 1/4 (25%), 3/4 (75%), full (100%)Show or hide an element without changing the layout of a document
.visible {visibility: visible;}
.invisible {visibility: hidden;}Set the z-order of a positioned element and its descendants or flex items.
.z-0 {z-index: 0;}
.z-10 {z-index: 10;}
.z-auto {z-index: auto;}Set how a flex item will grow or shrink to fit the space available in its flex container.
.flex {display: flex;}
.inline-flex {display: inline-flex;}Set how flex items are placed in the flex container defining the main axis and the direction (normal or reversed)
.flex-row { flex-direction: row; }
.flex-row-reverse { flex-direction: row-reverse; }
.flex-col { flex-direction: column; }
.flex-col-reverse { flex-direction: column-reverse; }Set whether flex items are forced onto one line or can wrap onto multiple lines.
.flex-nowrap { flex-wrap: nowrap; }
.flex-wrap { flex-wrap: wrap; }
.flex-wrap-reverse { flex-wrap: wrap-reverse; }Set how a flex item will grow or shrink to fit the space available in its flex container.
.flex-1 { flex: 1 1 0%; }
.flex-auto { flex: 1 1 auto; }
.flex-initial { flex: 0 1 auto; }
.flex-none { flex: none; }Set the flex grow factor of a flex item's main size.
.flex-grow {flex-grow: 1;}
.flex-grow-0 {flex-grow: 0;}Set the flex shrink factor of a flex item.
.flex-shrink {flex-shrink: 1;}
.flex-shrink-0 {flex-shrink: 0;}Set the order to lay out an item in a flex or grid container.
.order-first {order: -9999;}
.order-last {order: 9999;}
.order-none {order: 0;}
.order-5 {order: 5;}Values Range : 1-12
Set the padding area of an element on all sides, vertically, horizontally, or just on one side.
.p-0 {padding: 0rem;}
.-px-2 {padding-left: 0.5rem; padding-right: 0.5rem;}
.pt-px {padding-top: 1px;}py, pb, pl, prx/y direction, t/b/l/r sides, +/- Value, 4xn -> 1 rem[n] range from 0-96 in increments: 0, 0.5, 1, 1.5, 2, 2.5, 3-16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80, 96Set the margin area of an element on all sides, vertically, horizontally, or just on one side.
.m-0 { margin: 0rem; }
.mx-2 { margin-left: 0.5rem; margin-right: 0.5rem; }
.mt-px { margin-top: 1px; }my, mb, ml, mrx/y direction, t/b/l/r sides, +/- Value, 4xn -> 1 rem[n] range from 0-96 in increments: 0, 0.5, 1, 1.5, 2, 2.5, 3-16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80, 96Control the space between child elements using margins.
.space-x-0 { margin-left: 0; margin-right: 0; }
.-space-y-14 { margin-top: -3.5rem; margin-bottom: -3.5rem; }
.space-y-reverse { flex-direction: column-reverse; }x/y direction, +/- Value, 4xn -> 1 rem[n] range from 0-96 in increments: 0, 0.5, 1, 1.5, 2, 2.5, 3-16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80, 96text-xs { font-size: 0.75rem; line-height:1rem;}
text-sm {font-size: 0.875re; line-height: 1.25rem}
Tailwind includes modifiers for just about everything you’ll ever need, including:
:hover, :focus, :first-child, and :required::before, ::after, ::placeholder, and ::selectionprefers-reduced-motion[dir="rtl"] and [open]
hover : Mouse cursor is over the element.
active : Mouse click or touch is pressed down.
focus : Element is selected via click or keyboard.
Tailwind also includes modifiers for other interactive states like
:visited,:focus-within,:focus-visible, and more.
Software engineer - frontend - backend
first : Targets the first child of a parent element.
last : Targets the last child of a parent element.
odd: Targets odd-numbered children in a parent element.
even: Targets even-numbered children in a parent element.
Tailwind also includes modifiers for other structural pseudo-classes like
:only-child,:first-of-type,:empty, and more.
required : Targets form elements that have the required attribute.
invalid : Targets form elements that are invalid based on HTML5 validation.
disabled: Targets form elements that are disabled (i.e., elements that are not interactive and cannot be focused or changed).
group - style an element based on the state of some parent element,group class, and use group-* modifiers like group-hover to style the target element:group-focus, group-active, or even group-odd.peer : style an element based on the state of a sibling element,peer class, and use peer-* modifiers like peer-invalid to style the target element:peer-focus, peer-required, and peer-disabled.peer marker can only be used on previous siblings because of how the subsequent-sibling combinator works in CSS.