Two routes to overlapping layouts

Overlapping or co-locating two elements on a page is a common requirement for distinctive visual designs, and sometimes you only need elements positioned close together rather than on top of one another. Two approaches cover the job: the position property and CSS Grid.

Absolute positioning and its containment problem

Setting position: absolute; on an element places it wherever you specify on the page. Positioning a child at the top-left of the page, for example, puts it in that corner regardless of where its parent sits.

.child {
  /* ... */
  position: absolute;
  top: 0;
  left: 0;
}

That behavior is fragile. Anything that appears later in the flow — a navigation icon meant for the top-left corner, say, with a third-party banner ad inserted ahead of it — pushes the element down and breaks the intended placement. The same issue affects a self-contained component you intend to reuse in many locations, since it must work in its own context wherever it is dropped.

The fix is to give the parent position: relative;. Any child with position: absolute; is then placed absolutely relative to that containing unit.

.child {
  /* ... */
  position: absolute;
  top: 0;
  left: 0;
}

.parent {
  position: relative;
}

Stacking children inside a positioned parent

The same premise handles direct stacking. With two child elements layered on top of each other and separated by 150 pixels, both remain contained by the parent and stay positioned inside it.

<div class="parent">
  <h2>Parent</h2>
  <div class="child child-1">
    <h2>Child 1</h2>
  </div>
  <div class="child child-2">
    <h2>Child 2</h2>
  </div>
</div>
.child {
  position: absolute;
  top: 0;
}

.child-1 {
  left: 0;
}

.child-2 {
  left: 150px;
}

.parent {
  position: relative;
}

The technique is somewhat old school, but it is a long-standing fallback: it behaves consistently across browsers and supports even the most unusual placements.

Grid areas for layered elements

CSS Grid offers another way to overlap elements, stack them, or adjust their placement. Whether it fits your project depends on how far back browser support needs to reach; caniuse is the reference for that.

Elements can be placed where the container requires:

.parent {
  display: grid;
  grid-template-columns: 250px 1fr;
  grid-template-rows: 150px 1fr;
}

.child {
  grid-area: 1 / 1 / 2 / 2;
}

To make one element stack on another, give both the exact same grid area. A margin can then offset them slightly.

.parent {
  display: grid;
  grid-template-columns: 250px 1fr;
  grid-template-rows: 150px 1fr;
}

.child {
  grid-area: 1 / 1 / 2 / 2;
}

.child-2 {
  margin-left: 200px;
}

For anyone who finds the grid technique hard to picture, a CSS Grid Generator was built to make the layout clearer.

Where these techniques apply

Stacking, layering and offsetting elements suit a wide range of layouts — navigations and footers among them — and are useful whenever finer-grained control over element placement is needed.