Course contentsShow
← Web Development
Lesson 373 of 2,9826. CSS GridFree lesson

Grid-Based Timelines

Create vertical and horizontal timelines with alternating content using Grid placement.

Grid-Based Timelines

What you'll learn: You'll learn how to create elegant vertical and horizontal timelines using CSS Grid to alternate content positions along a central line.

What Is a Grid-Based Timeline?

A timeline displays events chronologically, often with a center line separating alternating content blocks. Think of a historical timeline in a museum where events zigzag left and right down a hallway—Grid makes this pattern beautifully simple by placing items on either side of a central track.

How Grid Creates Timelines

The key is using three columns for vertical timelines (or three rows for horizontal ones):

.timeline {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 2rem;
}

The center column (auto) holds your timeline line or markers, while the outer columns (1fr each) hold the alternating content.

Placing Timeline Items

You control which side each event appears on using grid-column:

.timeline-item:nth-child(odd) {
  grid-column: 1; /* Left side */
}

.timeline-item:nth-child(even) {
  grid-column: 3; /* Right side */
}

.timeline-marker {
  grid-column: 2; /* Center track */
}

For horizontal timelines, swap columns for rows and use grid-row instead.

Why This Works

Grid's explicit placement lets you position events precisely without complex positioning or floats. The alternating :nth-child() pattern creates the zigzag effect automatically. You can even overlap items (like placing a marker dot on the same row as content) because Grid supports layering.

This approach scales beautifully—add more events, and they automatically flow into the alternating pattern. Perfect for portfolios, project histories, or company milestones.

Key Takeaway: CSS Grid timelines use a three-track layout with alternating item placement to create professional vertical or horizontal chronologies without complex positioning.