Complete Reference · Free Download

CSS
Cheat Sheet

Every essential CSS concept in one place — selectors, box model, flexbox, grid, animations, variables, and more. Curated for beginners to advanced developers.

Browse Cheat Sheet ↓
14+Topics Covered
250+Code Snippets
6 pagesPDF Download
FreeAlways
Basics

CSS Selectors

Target HTML elements precisely — from simple tags to complex combinators and attribute selectors.

🎯
Basic & Combinator Selectors
Target by tag, class, ID, attribute, or relationship.
css
/* 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 & Specificity
State-based targeting and how browsers calculate selector priority.
css
/* 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 */
Layout

Box Model & Spacing

Every element is a box — understand margin, border, padding, and the display property.

📦
Box Model Properties
Margin, padding, border and sizing control.
css
.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 & Overflow
Control how elements are rendered in the document flow.
css
/* 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 & Shadows
Color, gradients, images, and depth with shadows.
css
/* 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);
Text

Typography & Text

Fonts, sizes, spacing, alignment, and all the text styling properties you need.

🔤
Font Properties
Control typeface, size, weight, style, and line height.
css
/* 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');
📐
Text Styling & Alignment
Align, decorate, transform, and clip text for any design.
css
/* 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;
Layout

Flexbox

One-dimensional layout — align and distribute items along a row or column with ease.

📐
Flex Container Properties
Applied to the parent with display: flex
css
.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 */
}
🧩
Flex Item Properties
Applied to direct children to control sizing and order.
css
.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;
}
Layout

CSS Grid

Two-dimensional layout system — rows and columns for complex, powerful page structures.

🔲
Grid Container
Define rows, columns, gaps, and named areas on the parent.
css
.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;
}
📌
Grid Items & Placement
Span rows/columns and place items in named grid areas.
css
/* 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;
}
Layout

Positioning & Z-Index

Control exactly where elements appear on the page, independent of the document flow.

📍
Position Property
static, relative, absolute, fixed, sticky — and when to use each.
css
/* 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 & Stacking Context
Layer elements with z-index. Understand stacking contexts.
css
/* 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; }
Motion

Transitions & Animations

Smooth property changes with transitions, or full keyframe animations for complex motion.

Transitions
Smoothly animate property changes on state change (hover, focus, etc.).
css
/* 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); }
🎬
Keyframe Animations
Full control over multi-step motion with @keyframes.
css
/* 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;
}
Custom Properties

CSS Variables & Transforms

Design tokens with custom properties and powerful 2D/3D transforms for movement and perspective.

🎨
CSS Custom Properties (Variables)
Define and reuse values across your entire stylesheet.
css
/* 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') */
🔄
Transform Functions
Translate, scale, rotate, and skew elements in 2D and 3D.
css
/* 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%;
Responsive Design

Media Queries & Responsive CSS

Adapt your layout for any screen size — mobile-first breakpoints, fluid typography, and modern container queries.

📱
Media Queries
Write mobile-first CSS, then layer on larger screen styles.
css
/* 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 & Container Queries
Use relative units and modern container queries for intrinsic sizing.
css
/* 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;
Advanced Selectors

Pseudo-elements & Filters

Inject virtual elements, style form controls, and apply visual effects with CSS filters.

🔮
::before, ::after & Pseudo-elements
Add decorative content and target specific parts of elements.
css
/* ::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; }
🌈
CSS Filters & Blend Modes
Apply visual effects like blur, brightness, and Photoshop-like blend modes.
css
/* 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;
📚 More Cheat Sheets

Explore Other Cheat Sheets

Level up across the full web stack — free, beautifully formatted, downloadable references.

🎨

Download the Full PDF Cheat Sheet

Get all 14 CSS topics in a beautifully formatted 6-page PDF — perfect to pin on your desk or keep on your phone.

↑ Back to Top

No spam. Download link sent to your email instantly.

Join WhatsApp Channel