日本語

Viewing Grid and Flexbox Layout Overlays in Chrome DevTools

​​Chrome DevTools​CSS,CSS Grid,Flexbox

In the Chrome DevTools Elements panel, clicking the "grid" badge next to an element with display: grid shows grid lines and line numbers overlaid on the page. For an element with display: flex, the "flex" badge shows the positions of the items and the space between them. You can use these to check CSS layout problems on the page itself, such as columns that don't line up where you expect or spacing whose source you can't identify. Below I show what I actually did in a test environment, along with the results shown on screen and the values I read from the page.

Note

The test environment was Ubuntu 24.04 with Chrome for Testing 153.0.8010.12 (the build bundled with Playwright 1.63.0). I launched Chrome headless and opened the DevTools UI that it serves in a separate Chromium tab, and I operated it from there. The viewport of the page was 400px wide and 420px high. The DevTools UI was in English, so button and heading names below are given in English. I changed the overlay colors by setting the color swatch values directly from a test script; I did not use the color picker that opens from the swatch. I only tested turning overlays off by unchecking items in the Layout pane. I did not test Chrome on macOS or Windows, DevTools opened the usual way from a regular window, the Grid Editor or Flexbox editor in the Styles pane, or subgrid.

The test page

A page where the header, menu, body, and footer are laid out with Grid, and a row of tags inside the body is laid out with Flexbox.

html
<div class="page">
  <header class="head">ヘッダー</header>
  <nav class="side">メニュー</nav>
  <main class="main">
    <ul class="tags">
      <li>CSS</li>
      <li>Grid</li>
      <li>Flex</li>
    </ul>
    <p>本文</p>
  </main>
  <footer class="foot">フッター</footer>
</div>
css
.page {
  display: grid;
  grid-template-columns: 120px 1fr;
  grid-template-rows: 60px 1fr 40px;
  grid-template-areas:
    "head head"
    "side main"
    "foot foot";
  gap: 12px 16px;
  min-height: 400px;
  box-sizing: border-box;
  padding: 8px;
}
.head { grid-area: head; background: #eefafd; padding: 20px 28px; }
.side { grid-area: side; background: #eefafd; padding: 28px; }
.main { grid-area: main; padding: 28px 12px; }
.foot { grid-area: foot; background: #eefafd; padding: 10px 28px; }
.tags {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  list-style: none;
  margin: 0;
  padding: 0;
  height: 48px;
}
.tags li {
  padding: 4px 12px;
  border: 1px solid #9aa5ab;
}

Clicking the "grid" badge to overlay grid lines and numbers

According to the official Chrome DevTools documentation on inspecting CSS grid, elements with display: grid or display: inline-grid get a "grid" badge in the DOM tree of the Elements panel. Clicking the badge toggles the grid overlay (lines and numbers drawn over the page) for that element.

In my test environment, the "grid" badge appeared to the right of the <div class="page"> row in the DOM tree. Clicking it turned the badge blue and drew grid lines over div.page on the page.

The top half is the DOM tree, where the "grid" badge to the right of the div class="page" row is filled in blue. The bottom half is the page, with pink lines over the header, menu, body, and footer, and numbers such as 1, 2, 3 and -1, -2, -3 at the line ends
The DOM tree and page after clicking the "grid" badge

The lines are drawn at the outer edges of the grid and at the boundaries between tracks (columns and rows). The diagonal-striped bands between tracks are the gap areas: the vertical band is the 16px gap between columns, and the horizontal band is the 12px gap between rows.

Numbers appear at the line ends. There are two columns, so there are three vertical lines, numbered 1, 2, 3 from the left, and -1, -2, -3 counting from the right. There are three rows, so there are four horizontal lines, and I saw 1 to 4 and -1 to -4. When you place items by line number, for example with grid-column: 2 / 3 or grid-row: 1 / -1, this display lets you confirm which line is which number.

Switching line names and track sizes in the Layout pane

For a closer look, open the "Layout" tab on the right side of the Elements panel. In my test environment, it showed a "Grid / Grid Lanes" section on top and a "Flexbox" section below. The official documentation calls this the "Grid" section, but in Chrome 153, which I tested, the heading was "Grid / Grid Lanes".

In the "Overlay display settings" under "Grid / Grid Lanes", there was one dropdown and three checkboxes. The dropdown had three options: "Hide line labels", "Show line numbers", and "Show line names". It was initially set to "Show line numbers". Below that, under "Grid / Grid Lanes overlays", the grid elements on the page (here, div.page) were listed with checkboxes, and the div.page I had turned on with the badge was checked.

"Show line names" displays the names generated from areas

Selecting "Show line names" in the dropdown replaced the line numbers with line names.

In the overlay of div.page, the line ends show names such as head-start, main-start, side-end, and foot-end instead of numbers. In the top-left corner, the name labels overlap each other
The overlay with "Show line names" selected

My test CSS does not name any lines in grid-template-columns or grid-template-rows. Even so, the following names appeared:

Line Names shown
Column line 2 (boundary between menu and body) main-start, side-end
Column line 3 (right edge) main-end, foot-end, head-end
Row line 2 (below the header) main-start, side-start, head-end
Row line 3 (above the footer) foot-start, main-end, side-end
Row line 4 (bottom edge) foot-end

This happens because naming areas with grid-template-areas also gives the lines around each area names of the form "area-name-start" and "area-name-end". The MDN page on named grid lines also says that defining named template areas creates named lines. If an item placed with line names such as grid-column: main-start / main-end ends up in the wrong position, you can use this display to check which line each name is attached to.

Note that in the top-left corner, the name labels for column line 1 and row line 1 overlapped, and part of one was hidden.

"Show track sizes" and "Show area names" overlay sizes and area names

I changed the dropdown to "Hide line labels" to remove the line numbers, then checked "Show track sizes" and "Show area names".

The top half is the Layout pane, with Hide line labels in the dropdown and Show track sizes and Show area names checked. The bottom half is the page, with labels showing 120px·120px and 1fr·248px for the columns, 60px·60px, 1fr·260px, and 40px·40px for the rows, and head, side, main, and foot in the top-left of each area
The Layout pane settings and the overlay showing track sizes and area names

Track size labels showed the size written in the CSS and the actual size on screen, joined by "·". The official documentation also explains that the labels show the "specified size" and the "computed size". Comparing these with the values I read by running getComputedStyle on the page:

Track Size written in CSS Label shown getComputedStyle value
Column 1 120px 120px·120px 120px
Column 2 1fr 1fr·248px 248px
Row 1 60px 60px·60px 60px
Row 2 1fr 1fr·260px 260px
Row 3 40px 40px·40px 40px

You can read directly on the page how many pixels 1fr actually became. The 248px for column 2 is the 400px viewport width minus 8px of padding on each side, minus column 1's 120px, minus the 16px gap between columns.

With "Show area names" on, head, side, main, and foot appeared at the top-left of their areas. However, the head label overlapped the column size label, and part of it was hidden.

I also checked "Extend grid lines", which extended the grid lines as dotted lines beyond div.page to the edge of the page viewport. This is useful for checking whether elements outside the grid line up with its lines. After checking this, I unchecked it again.

Using the "flex" badge to overlay Flexbox items and free space

According to the official documentation on inspecting Flexbox, elements with display: flex or display: inline-flex get a "flex" badge, and clicking it toggles the Flexbox overlay.

With the grid overlay on div.page still showing, including the track sizes and area names, I clicked the "flex" badge next to <ul class="tags"> in the DOM tree. The ul.tags entry under "Flexbox overlays" in the Layout pane was checked as well, so both overlays appeared on the page at the same time.

The overlay color is set by the color swatch to the right of the element name in the Layout pane. In my test environment, both div.page and ul.tags were initially #f59794 (pink). When I changed the ul.tags swatch value to #00c8e8 (light blue), only the Flexbox overlay turned light blue.

Point

When you overlay Grid and Flexbox together, changing the color of one of them with its swatch in the Layout pane makes them easier to tell apart.

The grid overlay on div.page (pink, with track size labels and area names still showing), with a light-blue dotted outline around ul.tags in the body. The three items CSS, Grid, and Flex are each outlined with dotted lines, and the spaces between the items are light-blue diagonal stripes
Grid overlay (pink) and Flexbox overlay (light blue) displayed together

In the Flexbox overlay, the outside of ul.tags and each of the three items were outlined with dotted lines, and the spaces between items were shown as diagonal stripes.

The diagonal stripes are the free space between items

To confirm what the diagonal stripes mean, I turned off div.page in the Layout pane so only the Flexbox overlay was shown. Then I added justify-content: flex-start to the style attribute of .tags on the page and compared it with space-between.

With justify-content: space-between (top), there are two diagonal stripes of equal width between CSS, Grid, and Flex. With justify-content: flex-start (bottom), the stripes between items are thin, and there is a wide diagonal stripe to the right of Flex
The Flexbox overlay with justify-content changed

After the change, the overlay was redrawn to match the new layout. I ran getBoundingClientRect on the page to calculate the free space from the item positions and widths. The results are below:

justify-content Between item 1 and item 2 Between item 2 and item 3 Right of item 3
space-between 27.88px 27.88px 0px
flex-start 8px 8px 39.75px

With space-between, the space between items is the 8px gap plus half of the remaining 39.75px free space in each of the two gaps (19.875px), which gives 27.88px. The stripes were drawn at that width. With flex-start, the spaces between items are only the 8px gap, so those stripes are thin. The unused 39.75px appeared as a stripe to the right of the last item. If the spacing between items is larger than you expect, the width of these stripes tells you whether space other than gap is being added.

Turning off the overlays

Overlays are turned off by unchecking them in the Layout pane. In my test environment, unchecking div.page in the Layout pane also returned the "grid" badge in the DOM tree to its unpressed state (aria-pressed was false) and removed its blue fill. The official Flexbox documentation also says that unchecking an item in the Layout pane does the same thing as clicking the badge. When I also unchecked ul.tags, all overlays on the page disappeared.

​Posted by 86