Skip to main content

CSS Cheatsheet

Frontend 17 views Apr 2026

CSS Cheatsheet

Selectors

/* Basic */
*           { }   /* universal */
div         { }   /* element */
.class      { }   /* class */
#id         { }   /* id */

/* Combinators */
div p       { }   /* descendant */
div > p     { }   /* direct child */
div + p     { }   /* adjacent sibling */
div ~ p     { }   /* general sibling */

/* Attribute */
[type]            { }   /* has attribute */
[type="text"]     { }   /* exact match */
[class^="btn"]    { }   /* starts with */
[class$="lg"]     { }   /* ends with */
[class*="col"]    { }   /* contains */

/* Pseudo-classes */
a:hover         { }
a:focus         { }
a:active        { }
input:checked   { }
input:disabled  { }
p:first-child   { }
p:last-child    { }
p:nth-child(2)  { }
p:nth-child(2n) { }   /* even rows */
p:not(.skip)    { }
input:focus-visible { }  /* keyboard focus only */

/* Pseudo-elements */
p::before        { content: ">> "; }
p::after         { content: " <<"; }
p::first-line    { }
p::first-letter  { font-size: 2em; }
::selection      { background: yellow; }

Box Model

/* Always use border-box */
*, *::before, *::after { box-sizing: border-box; }

.box {
  width: 300px;
  height: 200px;

  /* Padding */
  padding: 10px;                   /* all 4 sides */
  padding: 10px 20px;              /* top+bottom  left+right */
  padding: 10px 20px 15px 25px;    /* top right bottom left */

  /* Border */
  border: 2px solid #333;
  border-radius: 8px;
  border-radius: 50%;              /* circle */
  outline: 2px solid blue;
  outline-offset: 4px;

  /* Margin */
  margin: 0 auto;                  /* center horizontally */
  margin: 16px 0;                  /* top+bottom only */

  /* Overflow */
  overflow: hidden;
  overflow: auto;
  overflow-x: hidden;
  overflow-y: auto;
}

Flexbox

.container {
  display: flex;

  /* Direction */
  flex-direction: row;             /* default, left to right */
  flex-direction: column;          /* top to bottom */
  flex-direction: row-reverse;
  flex-direction: column-reverse;

  /* Wrapping */
  flex-wrap: nowrap;               /* default */
  flex-wrap: wrap;

  /* Main axis (horizontal when row) */
  justify-content: flex-start;
  justify-content: flex-end;
  justify-content: center;
  justify-content: space-between;
  justify-content: space-around;
  justify-content: space-evenly;

  /* Cross axis (vertical when row) */
  align-items: stretch;            /* default */
  align-items: flex-start;
  align-items: flex-end;
  align-items: center;
  align-items: baseline;

  /* Multi-line cross axis */
  align-content: center;
  align-content: space-between;

  gap: 16px;
  gap: 10px 20px;    /* row-gap column-gap */
}

.item {
  flex: 1;                /* grow:1, shrink:1, basis:0 */
  flex: 0 0 200px;        /* fixed 200px, no grow/shrink */
  flex-grow: 2;
  flex-shrink: 0;
  flex-basis: 150px;
  align-self: center;     /* override align-items for this item */
  order: -1;              /* reorder visually */
}

CSS Grid

.grid {
  display: grid;

  /* Columns */
  grid-template-columns: 200px 1fr 1fr;
  grid-template-columns: repeat(3, 1fr);
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  grid-template-rows: 80px 1fr 60px;

  gap: 20px;
  column-gap: 20px;
  row-gap: 10px;

  /* Alignment */
  justify-items: start | center | end | stretch;
  align-items:   start | center | end | stretch;
  justify-content: start | center | space-between;
  align-content:   start | center | space-between;

  /* Named areas */
  grid-template-areas:
    "header  header  header"
    "sidebar main    main"
    "footer  footer  footer";
}

.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main    { grid-area: main; }
.footer  { grid-area: footer; }

.item {
  grid-column: 1 / 3;      /* span cols 1 to 2 */
  grid-column: span 2;     /* span 2 columns */
  grid-row: 1 / 3;
  justify-self: center;
  align-self: end;
}

CSS Custom Properties (Variables)

:root {
  --color-primary:   #3b82f6;
  --color-secondary: #64748b;
  --color-text:      #1e293b;
  --color-bg:        #ffffff;
  --spacing-sm:  0.5rem;
  --spacing-md:  1rem;
  --spacing-lg:  2rem;
  --radius:      0.5rem;
  --shadow:      0 4px 6px -1px rgb(0 0 0 / 0.1);
}

.button {
  background: var(--color-primary);
  padding: var(--spacing-sm) var(--spacing-md);
  border-radius: var(--radius);
  color: var(--color-bg);
}

/* Override per component */
.card { --radius: 1rem; }

/* Dark mode */
@media (prefers-color-scheme: dark) {
  :root {
    --color-text: #f1f5f9;
    --color-bg:   #0f172a;
  }
}

Animations & Transitions

/* Transition */
.btn {
  background: blue;
  transition: background 0.3s ease, transform 0.2s ease;
}
.btn:hover {
  background: darkblue;
  transform: scale(1.05);
}

/* Keyframe animations */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to   { opacity: 1; transform: translateY(0); }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50%       { transform: scale(1.1); }
}

.fade-in { animation: fadeIn 0.4s ease forwards; }
.spinner { animation: spin 1s linear infinite; }
.pulse   { animation: pulse 2s ease-in-out infinite; }

/* Respect reduced motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

Media Queries

/* Mobile-first breakpoints */
/* xs: default, no query needed */

/* sm: 640px+ */
@media (min-width: 640px)  { }

/* md: 768px+ */
@media (min-width: 768px)  { }

/* lg: 1024px+ */
@media (min-width: 1024px) { }

/* xl: 1280px+ */
@media (min-width: 1280px) { }

/* Feature queries */
@supports (display: grid) { }
@supports not (display: grid) { }

/* Orientation */
@media (orientation: landscape) { }

/* Print */
@media print {
  nav, aside { display: none; }
  body { font-size: 12pt; }
}

/* Real pointer device (not touch) */
@media (hover: hover) {
  .btn:hover { background: darkblue; }
}

Modern CSS

/* Container queries */
.card-container { container-type: inline-size; }

@container (min-width: 400px) {
  .card { flex-direction: row; }
}

/* Logical properties (RTL-aware) */
margin-inline: auto;
margin-block: 1rem;
padding-inline-start: 1rem;
border-block-end: 1px solid;
inset: 0;

/* :has() — parent selector */
.form:has(input:invalid) { border-color: red; }
li:has(> a.active) { font-weight: bold; }

/* Native CSS nesting */
.card {
  padding: 1rem;
  & h2 { font-size: 1.5rem; }
  &:hover { box-shadow: var(--shadow); }
}

/* color-mix() */
background: color-mix(in srgb, blue 70%, white);

/* accent-color — style form controls */
input[type="checkbox"] { accent-color: var(--color-primary); }

/* aspect-ratio */
.video-wrap { aspect-ratio: 16 / 9; }
.avatar     { aspect-ratio: 1; border-radius: 50%; }

Found this helpful? Share it!

Tweet LinkedIn WhatsApp
Translate Page