No history yet

Modern CSS Layouts

Flexible and Grid-Based Layouts

Web layouts used to be a messy business, often relying on clever hacks with properties like float. Those days are over. Modern CSS gives us two powerful systems for arranging elements: Flexbox and Grid. They aren't competing; they are complementary tools designed for different tasks. excels at arranging items in a single line, either a row or a column. Think navigation bars or a list of cards. CSS Grid, on the other hand, is for two-dimensional layouts—arranging items into both rows and columns simultaneously. This makes it perfect for the overall structure of a page.

The key is knowing when to use which. Use Flexbox for components and one-dimensional layouts. Use Grid for the overall page layout.

Flexbox in One Dimension

To start using Flexbox, you simply declare a container element as a flex container. This immediately affects its direct children, which become 'flex items'.

.container {
  display: flex; /* This is the magic property! */
}

Once you've done this, you gain access to a set of properties for controlling the alignment and distribution of the items inside. Two of the most important are justify-content and align-items. justify-content aligns items along the main axis (the direction of the flex container), while align-items aligns them along the cross axis (perpendicular to the main axis).

For example, to create a simple horizontal navigation bar where the links are evenly spaced, you might use:

nav {
  display: flex;
  justify-content: space-around; /* Distribute items evenly */
  align-items: center; /* Vertically center the items */
}

Grid for Two Dimensions

While Flexbox is for lines of items, CSS Grid is for creating complex, grid-based layouts. It gives you control over both rows and columns. You start by defining a grid container and specifying the structure of your columns and rows.

.page-wrapper {
  display: grid;
  grid-template-columns: 200px 1fr 1fr; /* A 200px sidebar and two equal flexible columns */
  grid-template-rows: auto 1fr auto; /* A header, a flexible content area, and a footer */
  gap: 1rem; /* Space between grid items */
}

The code above creates a three-column grid. The first column is fixed at 200 pixels wide. The next two columns use the fr unit, which represents a fraction of the available space. This makes them flexible and responsive. The gap property is a convenient shorthand for setting the space between rows and columns.

For more semantic control, you can name grid areas and assign elements to them. This makes your layout code incredibly readable.

.page-wrapper {
  display: grid;
  grid-template-areas:
    "header header header"
    "nav    main   aside"
    "footer footer footer";
  grid-template-columns: 150px 1fr 150px;
  grid-template-rows: auto 1fr auto;
  gap: 10px;
}

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

Designing for All Screens

Modern layouts must adapt to different screen sizes. This is achieved with , which apply CSS rules only when certain conditions are met, like the screen width being below a certain size. This practice is central to the mobile-first design philosophy: design the simplest layout for small screens first, then use media queries to add complexity for larger screens.

Relative units are crucial for this. Instead of fixed pixels, use:

  • %: A percentage of the parent element's size.
  • em: Relative to the font-size of the parent element.
  • rem: Relative to the font-size of the root (html) element. rem is often preferred for consistency.
/* Default styles for mobile */
.container {
  display: flex;
  flex-direction: column; /* Stack items vertically on small screens */
}

/* Styles for tablets and larger */
@media (min-width: 768px) {
  .container {
    flex-direction: row; /* Arrange items horizontally */
  }
}

This code snippet shows a common mobile-first pattern. By default, the container stacks its items vertically. On screens 768px or wider, the media query kicks in, and the layout switches to a horizontal arrangement. This approach ensures a functional experience on all devices, starting with the most constrained.

Time to check your understanding of these modern layout techniques.

Quiz Questions 1/5

What is the primary conceptual difference between CSS Flexbox and CSS Grid?

Quiz Questions 2/5

In a horizontal flex container, which property is used to align items vertically (along the cross axis)?

By combining Flexbox for components, Grid for overall layout, and media queries for responsiveness, you can build sophisticated, modern websites that look great on any device.