CSS 3 cheatsheet

CSS 3: Responsive design

7 common CSS 3 tasks.

Change styles by screen width

Apply styles when the screen is at least 768px wide. Writing mobile styles first and adding queries for larger screens keeps CSS simple.

@media (min-width: 768px) {
  .sidebar { display: block; }
}

Scale a size fluidly

Keep a font size between a minimum and maximum, growing with the viewport.

h1 {
  font-size: clamp(1.75rem, 4vw, 3rem);
}

Respond to the container's size

Style a component according to the space it has, not the screen.

.card-wrapper {
  container-type: inline-size;
}

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

Support dark mode

Follow the user's operating system setting.

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111;
    --text: #eee;
  }
}

Respect reduced motion

Turn off animations for people who ask for less movement.

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}

Make images scale

Never overflow the container, and keep the proportions.

img {
  max-width: 100%;
  height: auto;
}

Keep a shape

Hold a 16:9 ratio, for example for a video container.

.video {
  aspect-ratio: 16 / 9;
}

Was this page helpful?