CSS 3 cheatsheet

CSS 3: Flexbox

7 common CSS 3 tasks.

Center something

Center a child horizontally and vertically.

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}

Lay out items in a row with spacing

Place children side by side with a gap between them.

.row {
  display: flex;
  gap: 1rem;
}

Push items to the edges

Put the first item at the start and the last at the end.

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

Wrap onto new lines

Let items flow to the next line when there is no room.

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

Make one item fill the space

Let an item take all remaining space.

.search {
  flex: 1;
}

Stack items vertically

Switch the main axis to vertical.

.stack {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

Keep the footer at the bottom

Make the page at least as tall as the screen, with the main area filling the space.

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

main {
  flex: 1;
}

Was this page helpful?