Every essential CSS concept in one place — selectors, box model, flexbox, grid, animations, variables, and more. Curated for beginners to advanced developers.
Target HTML elements precisely — from simple tags to complex combinators and attribute selectors.
/* Element, Class, ID */ p { color: blue; } .card { padding: 1rem; } #header { background: #111; } /* Combinators */ div p { } /* descendant */ div > p { } /* direct child */ h1 + p { } /* adjacent sibling */ h1 ~ p { } /* general sibling */ /* Attribute */ [type="text"] { } [href^="https"] { } /* starts with */ [class$="btn"] { } /* ends with */ [class*="card"] { } /* contains */ /* Universal & Group */ * { box-sizing: border-box; } h1, h2, h3 { font-weight: 700; }
/* Pseudo-classes */ a:hover { color: cyan; } a:visited { opacity: 0.7; } input:focus { outline: 2px solid blue; } li:first-child { font-weight: 700; } li:last-child { border: none; } li:nth-child(2n) { background: #f5f5f5; } p:not(.skip) { margin-bottom: 1rem; } input:checked { accent-color: cyan; } div:empty { display: none; } /* Specificity (low → high) */ /* tag=0,0,1 | class=0,1,0 */ /* id=1,0,0 | inline=1,0,0,0 */ .btn { color: red; } /* 0,1,0 */ #nav .btn { color: blue; } /* 1,1,0 ✓ */ color: green !important; /* beats all */
Every element is a box — understand margin, border, padding, and the display property.
.box { /* Width / Height */ width: 300px; height: 200px; max-width: 100%; min-height: 50px; /* Padding (inside) */ padding: 16px; /* all */ padding: 8px 16px; /* TB LR */ padding-top: 12px; /* Margin (outside) */ margin: 0 auto; /* center */ margin-bottom: 24px; /* Border */ border: 1px solid #ddd; border-radius: 12px; outline: 2px solid cyan; /* Box sizing */ box-sizing: border-box; }
/* Display values */ display: block; /* full width */ display: inline; /* text-like */ display: inline-block; /* best of both */ display: flex; /* flexbox */ display: grid; /* css grid */ display: none; /* hide */ /* Visibility */ visibility: hidden; /* hides, keeps space */ opacity: 0; /* transparent */ /* Overflow */ overflow: hidden; overflow: scroll; overflow: auto; overflow-x: hidden; text-overflow: ellipsis; white-space: nowrap;
/* Background */ background-color: #1a1a2e; background-image: url('bg.jpg'); background-size: cover; background-position: center; background-repeat: no-repeat; background-attachment: fixed; /* Gradients */ background: linear-gradient( 135deg, #06b6d4, #3b82f6 ); background: radial-gradient( circle, #06b6d4, transparent ); /* Shadows */ box-shadow: 0 4px 20px rgba(0,0,0,.2); box-shadow: inset 0 2px 6px #000; text-shadow: 0 2px 4px rgba(0,0,0,.5);
Fonts, sizes, spacing, alignment, and all the text styling properties you need.
/* Font family */ font-family: 'Inter', sans-serif; font-family: 'Fira Code', monospace; /* Size & Weight */ font-size: 16px; /* or rem, em */ font-size: clamp(1rem, 4vw, 2rem); font-weight: 400; /* normal */ font-weight: 700; /* bold */ font-style: italic; /* Spacing */ line-height: 1.6; letter-spacing: 0.05em; word-spacing: 4px; /* Google Fonts import */ @import url('https://fonts.googleapis.com/css2? family=Inter:wght@400;700&display=swap');
/* Alignment */ text-align: left | center | right | justify; vertical-align: middle; /* Decoration */ text-decoration: none; text-decoration: underline dotted cyan; /* Transform */ text-transform: uppercase | lowercase | capitalize; /* Indent & Wrapping */ text-indent: 2rem; white-space: nowrap | pre | pre-wrap; /* Gradient text trick */ .grad-text { background: linear-gradient(90deg, #06b6d4, #a855f7); -webkit-background-clip: text; -webkit-text-fill-color: transparent; } /* Truncate with ellipsis */ overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
One-dimensional layout — align and distribute items along a row or column with ease.
display: flex
.container { display: flex; /* Direction */ flex-direction: row; /* default */ flex-direction: column; flex-direction: row-reverse; /* Wrapping */ flex-wrap: wrap; flex-wrap: nowrap; /* default */ /* Main axis (horizontal in row) */ justify-content: flex-start; /* default */ justify-content: center; justify-content: flex-end; justify-content: space-between; justify-content: space-around; justify-content: space-evenly; /* Cross axis (vertical in row) */ align-items: stretch; /* default */ align-items: center; align-items: flex-start; align-items: flex-end; align-items: baseline; /* Multi-line alignment */ align-content: space-between; gap: 16px; /* gutters */ gap: 12px 24px; /* row col */ }
.item { /* Grow, Shrink, Basis */ flex-grow: 1; /* fill space */ flex-shrink: 0; /* don't shrink */ flex-basis: 200px; /* start size */ /* Shorthand */ flex: 1; /* grow=1 shrink=1 basis=0 */ flex: 0 0 300px; /* fixed width */ flex: auto; /* 1 1 auto */ /* Self alignment (overrides align-items) */ align-self: flex-end; align-self: center; justify-self: center; /* grid only */ /* Order (default: 0) */ order: -1; /* move to front */ order: 1; } /* Common pattern: Center anything */ .center { display: flex; align-items: center; justify-content: center; }
Two-dimensional layout system — rows and columns for complex, powerful page structures.
.grid { display: grid; /* Define columns */ grid-template-columns: 1fr 2fr 1fr; grid-template-columns: repeat(3, 1fr); grid-template-columns: 200px auto 200px; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); /* Define rows */ grid-template-rows: 80px 1fr 60px; grid-auto-rows: minmax(100px, auto); /* Named areas */ grid-template-areas: "header header" "sidebar main" "footer footer"; gap: 20px; row-gap: 16px; column-gap: 24px; justify-items: stretch; align-items: center; place-items: center stretch; }
/* Span columns / rows */ .item-wide { grid-column: 1 / 3; /* cols 1–2 */ grid-column: 1 / -1; /* full width */ grid-column: span 2; /* span 2 */ grid-row: 1 / 3; } /* Named area placement */ .header { grid-area: header; } .sidebar { grid-area: sidebar; } .main { grid-area: main; } .footer { grid-area: footer; } /* Self alignment */ .item { justify-self: end; align-self: start; place-self: center; } /* Responsive card grid */ .cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 24px; }
Control exactly where elements appear on the page, independent of the document flow.
/* Static (default — no offset) */ position: static; /* Relative — offset from itself */ position: relative; top: 10px; left: 20px; /* Absolute — relative to nearest positioned ancestor */ position: absolute; top: 0; right: 0; bottom: 0; left: 0; /* Fixed — relative to viewport */ position: fixed; top: 0; left: 0; right: 0; /* sticky nav */ /* Sticky — sticks at scroll point */ position: sticky; top: 72px; /* Centering with absolute */ position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
/* Z-Index (needs non-static position) */ z-index: -1; /* behind content */ z-index: 0; /* default */ z-index: 10; /* above */ z-index: 100; /* dropdowns */ z-index: 1000; /* modals */ z-index: 9999; /* tooltips */ /* Common layering system */ :root { --z-base: 1; --z-dropdown: 100; --z-sticky: 200; --z-overlay: 300; --z-modal: 400; --z-toast: 500; } /* Stacking context triggers */ /* position + z-index, opacity < 1, */ /* transform, filter, isolation:isolate */ .isolate { isolation: isolate; }
Smooth property changes with transitions, or full keyframe animations for complex motion.
/* Shorthand: property duration easing delay */ transition: all 0.3s ease; transition: color 0.2s ease-in-out; transition: transform 0.4s cubic-bezier(0.34,1.56,0.64,1); /* Multiple transitions */ transition: background 0.3s ease, transform 0.2s ease, box-shadow 0.3s ease; /* Easing functions */ transition-timing-function: linear; transition-timing-function: ease; transition-timing-function: ease-in; transition-timing-function: ease-out; transition-timing-function: ease-in-out; transition-timing-function: cubic-bezier(0.4,0,0.2,1); /* Hover lift effect */ .card { transition: transform 0.3s ease; } .card:hover { transform: translateY(-4px); }
/* Define keyframes */ @keyframes fadeIn { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } } @keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } @keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.05); } } /* Apply animation */ .element { animation-name: fadeIn; animation-duration: 0.6s; animation-timing-function: ease-out; animation-delay: 0.2s; animation-iteration-count: infinite; animation-direction: alternate; animation-fill-mode: both; /* Shorthand */ animation: fadeIn 0.6s ease-out 0.2s both; }
Design tokens with custom properties and powerful 2D/3D transforms for movement and perspective.
/* Define on :root for global scope */ :root { --color-primary: #06b6d4; --color-bg: #09090b; --font-size-base: 16px; --radius: 12px; --shadow: 0 4px 20px rgba(0,0,0,.15); --spacing-lg: 2rem; } /* Use with var() */ .btn { background: var(--color-primary); border-radius: var(--radius); box-shadow: var(--shadow); font-size: var(--font-size-base, 16px); /* fallback */ } /* Dark / Light mode theming */ [data-theme="dark"] { --color-bg: #09090b; } [data-theme="light"] { --color-bg: #fafaf9; } /* Override in local scope */ .card { --radius: 20px; /* local override */ } /* Update via JavaScript */ /* el.style.setProperty('--color-primary', '#f00') */
/* 2D Transforms */ transform: translateX(20px); transform: translateY(-10px); transform: translate(20px, -10px); transform: scale(1.1); transform: scale(1.2, 0.8); /* x, y */ transform: rotate(45deg); transform: skew(10deg, 5deg); /* Combine transforms */ transform: translateY(-4px) scale(1.02); /* 3D Transforms */ transform: rotateX(30deg); transform: rotateY(45deg); transform: translateZ(100px); transform: perspective(500px) rotateY(30deg); /* Transform origin */ transform-origin: center; transform-origin: top left; transform-origin: 50% 50%;
Adapt your layout for any screen size — mobile-first breakpoints, fluid typography, and modern container queries.
/* Mobile-first approach */ .grid { grid-template-columns: 1fr; } /* Common breakpoints */ @media (min-width: 480px) { /* sm */ } @media (min-width: 768px) { /* md */ .grid { grid-template-columns: 1fr 1fr; } } @media (min-width: 1024px) { /* lg */ .grid { grid-template-columns: repeat(3, 1fr); } } @media (min-width: 1280px) { /* xl */ } @media (min-width: 1536px) { /* 2xl */ } /* Other media features */ @media (prefers-color-scheme: dark) { } @media (prefers-reduced-motion: reduce) { * { animation-duration: 0.01ms !important; } } @media (orientation: landscape) { } @media print { .no-print { display: none; } }
/* Fluid units */ font-size: 4vw; /* 4% viewport width */ height: 100vh; /* full viewport height */ width: 50dvw; /* dynamic viewport */ /* Fluid typography with clamp() */ font-size: clamp(1rem, 4vw, 2.5rem); padding: clamp(16px, 5vw, 60px); /* min() and max() */ width: min(100%, 600px); padding: max(12px, 2vw); /* Container Queries (modern) */ .card-wrapper { container-type: inline-size; container-name: card; } @container card (min-width: 400px) { .card { flex-direction: row; } } /* Logical properties (RTL-safe) */ margin-inline: auto; padding-block: 1rem; border-inline-start: 3px solid cyan;
Inject virtual elements, style form controls, and apply visual effects with CSS filters.
/* ::before and ::after */ .card::before { content: ""; /* required! */ display: block; position: absolute; inset: 0; background: linear-gradient(to bottom, transparent, #000); } /* Other pseudo-elements */ p::first-line { font-weight: 700; } p::first-letter { font-size: 3em; float: left; } ::selection { background: #06b6d4; color: #fff; } ::placeholder { color: #888; font-style: italic; } ::marker { color: #06b6d4; } /* list bullets */ ::scrollbar { width: 8px; } ::scrollbar-thumb { background: #444; border-radius: 4px; } /* Custom checkbox / radio */ input[type="checkbox"] { accent-color: #06b6d4; } input[type="range"] { accent-color: #06b6d4; }
/* Filter functions */ filter: blur(4px); filter: brightness(1.2); filter: contrast(150%); filter: grayscale(100%); filter: hue-rotate(90deg); filter: invert(1); filter: opacity(0.5); filter: saturate(200%); filter: sepia(80%); /* Combine filters */ filter: brightness(1.1) contrast(1.2) saturate(1.3); /* Backdrop filter (glassmorphism) */ backdrop-filter: blur(20px); backdrop-filter: blur(10px) brightness(0.8); /* Drop shadow (respects transparency) */ filter: drop-shadow(0 4px 12px rgba(6,182,212,.4)); /* Blend modes */ mix-blend-mode: multiply; mix-blend-mode: screen; mix-blend-mode: overlay; background-blend-mode: luminosity;
Level up across the full web stack — free, beautifully formatted, downloadable references.