日本語

CSS Grid Full-Bleed Image While Keeping Body Text Centered

​​CSS​HTML,Playwright

To keep the body text at a readable width in the center while stretching only images across the full width of the screen, make the article's outer box a CSS grid and give the left and right margins their own columns. Body text goes in the middle column, and images span all three columns. This approach works for blog posts and landing pages where you want photos shown full-bleed.

Note

The test environment is Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I opened the page at screen widths of 1280px and 375px and measured the position and width of elements with getBoundingClientRect(). I did not test Firefox, Safari, real smartphones, or stretching <figure> or videos.

Make a three-column grid with the side margins as columns

Set the outer box of the article (here .post) as follows.

html
<article class="post">
  <h1>見出し</h1>
  <p>本文の段落</p>
  <img class="full-bleed" src="photo.png" alt="山の写真">
  <p>本文の段落</p>
</article>
css
img { display: block; }

.post {
  display: grid;
  grid-template-columns:
    [full-start] minmax(1rem, 1fr)
    [content-start] min(100% - 2rem, 40rem)
    [content-end] minmax(1rem, 1fr)
    [full-end];
}

.post > * {
  grid-column: content;
}

.post > .full-bleed {
  grid-column: full;
  width: 100%;
  height: auto;
}

The middle column is the width of the body text. min() returns the smallest of the values you pass to it, and you can subtract inside min() without writing calc(). On wide screens the column is 40rem (640px). On narrow screens it is the width of the outer box (.post) minus the 2rem of side margin (343px at a screen width of 375px). As described on MDN's grid-template-columns page, a % here is relative to the width of the grid container (.post).

The side columns are minmax(1rem, 1fr), so they never get narrower than 1rem, and the leftover width is split equally between the left and right columns. This centers the body text.

The column widths and element positions measured in the test environment (Chromium 153) are shown below.

Screen width Column widths (left, middle, right) Body text left edge and width Image left edge and width
1280px 320px, 640px, 320px 320px, 640px 0px, 1280px
375px 16px, 343px, 16px 16px, 343px 0px, 375px

At both widths, the body text stayed in the middle column, and the image spread from the left edge at 0px across the full width of the screen. No horizontal scrolling appeared (scrollWidth matched the screen width).

Screen 1000px wide. The heading and paragraphs fit in the 640px center column, and only the mountain photo spreads to the left and right edges. Dotted lines marking the column boundaries are overlaid, labeled full-start, content-start, content-end, and full-end
Example shown at desktop width (1000px). The dotted lines are the grid column boundaries
Screen 375px wide. The heading and paragraphs sit with 16px margins on each side, and the mountain photo spreads from edge to edge of the screen
Example shown at phone width (375px). The dotted lines are the grid column boundaries

Writing grid-column: full places an item between two lines

grid-column: full does not refer to a line named full. As described in MDN's guide to named grid lines, when you name lines 〇〇-start and 〇〇-end, the space between them is treated as an area named 〇〇.

When you write only one name in grid-column, that name is used for both the start and the end (see the spec section on shorthands such as grid-column). The start looks for the full-start line and the end looks for the full-end line, so the image spans the three columns from the left margin to the right margin. grid-column: content works the same way and fits into the single content-start to content-end column.

With this naming, you can add columns and still use grid-column: content on the body text. To place images in a frame slightly wider than the body text, add wide-start and wide-end lines and change the image's grid-column to wide.

css
.post {
  display: grid;
  grid-template-columns:
    [full-start] minmax(1rem, 1fr)
    [wide-start] minmax(0, 6rem)
    [content-start] min(100% - 2rem, 40rem)
    [content-end] minmax(0, 6rem)
    [wide-end] minmax(1rem, 1fr)
    [full-end];
}

.post > .full-bleed {
  grid-column: wide;
  width: 100%;
  height: auto;
}

At a screen width of 1280px, the body text stays in the center 640px as before, and the image with grid-column: wide is 832px wide (640px plus 96px on each side). At 375px, the two added columns shrink to 0px, and the image is 343px wide, the same as the body text.

Give every child element grid-column: content

You cannot omit .post > * { grid-column: content; }. When I removed it and tested, at a screen width of 1280px the heading went into the left column (320px wide), the first paragraph into the middle column, and the second paragraph into the right column (320px wide), so they sat side by side. As described in MDN's guide to auto-placement, children without a specified position are placed one cell at a time in source order. At 375px, the heading was squeezed into the 16px left column and horizontal scrolling appeared (scrollWidth 391px).

Keep width: 100% on the image too. When I removed it, the image was displayed at its original size of the test image (1600×900px), and scrollWidth became 1600px at both screen widths, so the page could be scrolled horizontally.

Paragraph spacing becomes double the normal gap

With a grid, the gap between paragraphs grows. I put the same <p> elements in a row (in the test environment, the browser default gave them 16px top and bottom margins) and measured from the bottom edge of the first paragraph to the top edge of the second. In normal block flow the gap was 16px, and in the grid it was 32px.

In normal flow, the bottom margin of the upper paragraph and the top margin of the lower paragraph (both 16px) collapse into one 16px gap. In a grid, the margins do not collapse, so you get two gaps' worth of space. The spec section on grid item margins also states that margins of adjacent grid items do not collapse.

To restore the original spacing, set the vertical margins of the children to 0 and create the space between rows with row-gap.

css
.post {
  row-gap: 1em;
}

.post > * {
  margin-block: 0;
}

With this added, the gap between paragraphs was 16px.

The 100vw approach overflows by the width of the scrollbar

Without a grid, you can also narrow the body box with max-width and stretch only the image with 100vw and a negative margin.

css
.post {
  max-width: 40rem;
  margin-inline: auto;
  padding-inline: 1rem;
}

.full-bleed {
  width: 100vw;
  max-width: none;
  height: auto;
  margin-left: calc(50% - 50vw);
}

Playwright launches headless Chromium with --hide-scrollbars, so the page is displayed with scrollbars taking no width. In this state, as with the grid approach, the image spread from the left edge at 0px across the full width of the screen, and no horizontal scrolling appeared.

So I removed this launch option and measured again with the vertical scrollbar taking 15px of width. At a screen width of 1280px, the usable width of the page content became 1265px, and the two approaches gave different results.

Approach Image left edge and width Page width (scrollWidth)
Grid 0px, 1265px 1265px (no horizontal scrolling)
100vw and negative margin −7.5px, 1280px 1273px (can scroll horizontally by 8px)

The width from 100vw stayed at 1280px, which includes the scrollbar. As described on MDN's length page, vw is a unit based on the width of the viewport. The spec (CSS Values and Units Module Level 4) says that unless the root element's overflow or scrollbar-gutter is set to something that always shows a scrollbar or reserves its space (such as overflow: scroll), sizes are determined as if there were no scrollbar.

As a result, the image was wider than the available space by the width of the scrollbar. Its left edge sat at −7.5px, and its right edge went 7.5px past the usable width of the page content. scrollWidth became 1273px, so the page could be scrolled horizontally by that amount. With the grid approach, the image's width comes from the outer box (width: 100%).

Point

If you want an element to span the full width of the screen, letting it span the outer grid columns is more robust against scrollbar width than sizing it with 100vw.

​Posted by 86