CSS Layout Guide: Flexbox & Grid - WittyWriter

CSS Layout Guide: Flexbox & Grid

📘 Key Concepts and Definitions

Flexbox Terminology

Grid Terminology

🧮 Layout Calculation & Units

Flexbox Sizing: The flex Property

The flex property is a shorthand for flex-grow, flex-shrink, and flex-basis.

flex: [grow] [shrink] [basis];

Grid Sizing: The fr Unit

The fractional unit (fr) represents a fraction of the available space in the grid container. It makes responsive layouts simple.

Example: grid-template-columns: 1fr 2fr; creates two columns where the second is twice as wide as the first.

🛠️ Properties & Syntax

Flexbox Container Properties

PropertyDescriptionCommon Values
displayDefines the element as a flex container.flex, inline-flex
flex-directionSets the direction of the main axis.row, row-reverse, column, column-reverse
justify-contentAligns items along the main axis.flex-start, flex-end, center, space-between, space-around
align-itemsAligns items along the cross axis.stretch, flex-start, flex-end, center, baseline
flex-wrapAllows items to wrap onto multiple lines.nowrap, wrap, wrap-reverse
align-contentAligns wrapped lines along the cross axis (only works with flex-wrap: wrap).flex-start, center, space-between, stretch

Grid Container Properties

PropertyDescriptionExample Values
displayDefines the element as a grid container.grid, inline-grid
grid-template-columnsDefines the number and sizes of columns.100px 1fr 2fr, repeat(3, 1fr)
grid-template-rowsDefines the number and sizes of rows.minmax(150px, auto) 1fr
gapShorthand for setting row and column gaps.20px, 10px 20px (row column)
justify-itemsAligns grid items along the row (inline) axis.start, end, center, stretch
align-itemsAligns grid items along the column (block) axis.start, end, center, stretch

Flexbox Item Properties

Applied to the direct children of the flex container.

Grid Item Properties

Applied to the direct children of the grid container.

🧭 When to Use Flexbox vs. Grid

The Golden Rule: Use **Flexbox** for one-dimensional layouts (a row OR a column). Use **Grid** for two-dimensional layouts (rows AND columns).

Use Flexbox For:

Use Grid For:

⌨️ Productivity Tips

📊 Visual Aids

Flexbox vs. Grid Comparison

FeatureFlexboxGrid
DimensionsOne-dimensional (Row OR Column)Two-dimensional (Rows AND Columns)
Primary UseContent alignment, componentsOverall page layout
Sizing ControlContent-driven (items grow/shrink)Layout-driven (strict tracks)
Item WrappingWraps as a new row/columnPlaces items in available cells, can leave gaps

Grid Line Numbering

Grid lines are numbered starting from 1. You use these numbers to place items.

  Line 1    Line 2    Line 3
  +---------+---------+
  |         |         | Line 1
  +---------+---------+
  |         |         | Line 2
  +---------+---------+
        

🧪 Examples and Use Cases

Flexbox Example: Centered Navigation Bar

<!-- HTML -->
<nav class="navbar">
  <a href="#">Home</a>
  <a href="#">About</a>
  <a href="#">Contact</a>
</nav>

<!-- CSS -->
.navbar {
  display: flex;
  justify-content: center; /* Center items on the main axis */
  align-items: center;    /* Center items on the cross axis */
  gap: 20px;
  background-color: #333;
  padding: 1rem;
}

Grid Example: Responsive Photo Gallery

<!-- HTML -->
<div class="gallery">
  <div class="photo">1</div>
  <div class="photo">2</div>
  <div class="photo">3</div>
  <div class="photo">4</div>
</div>

<!-- CSS -->
.gallery {
  display: grid;
  /* Create columns that are at least 200px wide, but grow to fill space */
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 10px;
}

🧹 Troubleshooting Common Issues

📚 References and Further Reading

🍪 We use cookies to improve your experience. Learn more