Two constraints: full-bleed sections without duplicate markup

The layout calls for a sticky call-to-action panel that stays in view while other sections scroll past it. Above 768px the panel sits on the right; below that breakpoint it must appear after the hero, which carries the “Start your trial” header. The requirements produce two hard problems:

  • Full-bleed elements must span the viewport without colliding with the sticky panel.
  • The HTML must not be duplicated — no separate desktop and mobile copies toggled by visibility.

Because position: sticky is applied to .box--sticky, that element has to be a sibling of the other elements, full-bleed ones included, for the positioning to work at all. That constraint is what drives the markup:

<div class="grid">

  <div class="box box--hero">Hero Box</div>

  <div class="box box--sticky">Sticky Box</div>

  <div class="box box--bleed">Full-bleed Box</div>
  <div class="box box--bleed">Full-bleed Box</div>
  <!-- a bunch more of these -->

</div>

Sticky inside grid

Sticky behavior in a grid container needs very little: position: sticky on .box--sticky plus a top: 0 offset. The rule is scoped to viewports wider than 768px.

@media screen and (min-width: 768px) {
  .box--sticky {
    position: sticky;
    top: 0;
  }
}

One environment caveat applies: Safari breaks sticky positioning when an ancestor sets overflow: auto. This is listed among the known issues on caniuse:

A parent with overflow set to auto will prevent position: sticky from working in Safari.

Approach one: absolutely positioned pseudo-elements

Full-bleed spans via negative offsets on pseudo-elements are a common pattern. For centered content the arithmetic is simple — the negative offset is 100vw minus the element’s 100%, divided by -2, since two offsets are needed:

.box--bleed {
  max-width: 600px;
  margin-right: auto;
  margin-left: auto;
  padding: 20px;
  position: relative; 
}

.box--bleed::before {
  content: "";
  background-color: dodgerblue; 
  position: absolute;
  top: 0;
  bottom: 0;
  right: calc((100vw - 100%) / -2);
  left: calc((100vw - 100%) / -2);
}

Viewport units have their own documented problem, again per caniuse:

Currently all browsers but Firefox incorrectly consider 100vw to be the entire page width, including vertical scroll bar, which can cause a horizontal scroll bar when overflow: auto is set.

This layout, though, has no content below the sticky panel — the panel must not overlap anything, so the content is deliberately not centered. That makes the offset math conditional on the grid widths.

The grid itself is built from custom properties, so the maximum width, gaps and columns can be redefined by changing variable values rather than redeclaring grid-gap, grid-template-columns and max-width:

.grid {
  display: grid;
  grid-gap: var(--gap);
  grid-template-columns: var(--cols);
  max-width: var(--max-width);
  margin-left: auto;
  margin-right: auto;
}
:root {
  --gap: 20px;
  --cols: 1fr;
  --max-width: calc(100% - 2 * var(--gap));
}

@media screen and (min-width: 768px) {
  :root {
    --max-width: 600px;
    --aside-width: 200px;
    --cols: 1fr var(--aside-width);
  }
}

@media screen and (min-width: 980px) {
  :root {
    --max-width: 900px;
    --aside-width: 300px;
  }
}

At 768px and up there are two columns: a fixed --aside-width column and a 1fr column that takes the remainder, capped by the container’s --max-width. Below 768px the grid collapses to a single column with a gap, and the container’s maximum width is the viewport minus the side gaps.

For the uncentered case, the offset basis is not the parent’s 100%. It accounts for the gap and the sticky element’s width, so full-bleed content never extends past the hero’s bounds and the sticky panel stays clear of it.

.box--bleed {
  position: relative;
  z-index: 0;
}

.box--bleed::before {
  content: "";
  display: block;
  position: absolute;
  top: 0;
  bottom: 0;
  left: calc((100vw - (100% + var(--gap) + var(--aside-width))) / -2);
  right: calc(((100vw - (100% - var(--gap) + var(--aside-width))) / -2) - (var(--aside-width)));
  z-index: -1;
}

The left offset subtracts the element’s 100%, --gap and --aside-width from 100vw:

left: (100vw - (100% + var(--gap) + var(--aside-width))) / -2);

The right offset adds both --aside-width and --gap back into that calculation:

right: ((100vw - (100% + var(--gap) + var(--aside-width))) / -2) - (var(--aside-width) + var(--gap));

The 100vw inaccuracy described above shows up in this approach as horizontal overflow. Clipping overflow on the x-axis of the body removes it, and is a reasonable default regardless:

body {
  max-width: 100%;
  overflow-x: hidden;
}

Approach two: padding computed from the grid

The alternative avoids pseudo-elements entirely: configure the grid so the gutters do the work. The container is set up the same way as before:

.grid {
  display: grid;
  grid-gap: var(--gap);
  grid-template-columns: var(--cols);
}
:root {
  --gap: 20px;
  --gutter: 1px;
  --cols: var(--gutter) 1fr var(--gutter);
}

Below 768px the grid has three columns. The center column is the flexible one; the outer two exist only to enforce the horizontal gap.

@media screen and (max-width: 767px) {
  .box {
    grid-column: 2 / -2;
  }
}

Every grid child is placed in the center column. Above 768px, --max-width limits the width of the inner columns and --aside-width sets the sticky panel’s width, keeping the panel off the content of the full-bleed elements.

:root {
  --gap: 20px;
}

@media screen and (min-width: 768px) {
  :root {
    --max-width: 600px;
    --aside-width: 200px;
    --gutter: calc((100% - (var(--max-width))) / 2 - var(--gap));
    --cols: var(--gutter) 1fr var(--aside-width) var(--gutter);
  }
}

@media screen and (min-width: 980px) {
  :root {
    --max-width: 900px;
    --aside-width: 300px;
  }
}

The gutter width is derived as follows, where 100% is the viewport width: subtract the inner columns’ maximum width from the viewport, halve the result to get the two gutters, then subtract the grid gap to obtain the gutter column width.

--gutter: calc((100% - (var(--max-width))) / 2 - var(--gap));

Placing .box--hero so it begins at the first inner column pushes the sticky box to start immediately after the hero:

@media screen and (min-width: 768px) {
  .box--hero {
    grid-column-start: 2;
  }
}

An explicit placement for the sticky box works equally well:

.box--sticky {
  grid-column: 3 / span 1;
}

Full-bleed elements then get grid-column: 1 / -1, spanning from the first grid line to the last:

@media screen and (min-width: 768px) {  
  .box--bleed {
    grid-column: 1 / -1;
  }
}

Centering their content takes left and right padding. Left padding equals the gutter column width plus the grid gap; right padding equals the left padding plus a second grid gap plus the sticky element’s width.

@media screen and (min-width: 768px) {
  .box--bleed {  
    padding-left: calc(var(--gutter) + var(--gap));
    padding-right: calc(var(--gutter) + var(--gap) + var(--gap) + var(--aside-width));
  }
}

This version avoids the viewport-unit bug, which makes it the preferable of the two.