日本語

CSS outline vs. border: Adding a Frame Without Layout Shift

​​CSS​HTML,Playwright

In CSS, border takes up space, so adding a frame makes the element bigger and pushes neighboring elements aside. outline takes up no space, so neither the element's size nor the layout around it changes. When you wrap a selected card in a frame or show a border on a hovered button, the layout can shift or the hover can jitter. In that case, switch to outline, or reserve the space for the border in advance.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I opened HTML and CSS pages with Playwright, read sizes and positions with getBoundingClientRect() and similar methods, and checked pixel colors in screenshots. I did not test Firefox, Safari, or real smartphones.

How the size changes with four ways of adding a frame

I added a 4px frame to a 240px × 64px box in four different ways, and placed a gray box 12px to its right.

css
/* 1. border */
.box { border: 4px solid #00c8e8; }

/* 2. border and box-sizing: border-box */
.box { border: 4px solid #00c8e8; box-sizing: border-box; }

/* 3. outline */
.box { outline: 4px solid #00c8e8; }

/* 4. box-shadow drawn inside */
.box { box-shadow: inset 0 0 0 4px #00c8e8; }
Four-row comparison. From top: border: 4px, border: 4px with border-box, outline: 4px, and box-shadow: inset 0 0 0 4px. The pink dotted line marks the left edge of the right-hand neighbor when there is no frame. The border-only row makes the box 248×72, and the gray box on the right moves to the right of the dotted line. The other three rows stay at 240×64, and the right-hand neighbor sits on the dotted line. In the outline row, the frame extends outside the box.
Four ways to add a frame. The dotted line is the left edge of the right-hand neighbor when there is no frame.

Here are the measured values:

Method Element size Content width Right neighbor Where the frame is drawn
border 248×72px 240px Moves 8px to the right Outside the content (the element grows)
border + border-box 240×64px 232px Doesn't move Inside the element
outline 240×64px 240px Doesn't move Outside the element
box-shadow: inset 240×64px 240px Doesn't move Inside the element

With border alone, 4px is added on each side, so the element grew by 8px in both width and height. This is because, under the default box-sizing: content-box, width and height refer to the content size.

The other three kept the element's size. However, where the frame is drawn and what gets narrower differ. From here on, the measurements come from a page where a 160px × 60px box has another box placed 8px to its right.

With border-box, the element's size stays the same, but the content gets narrower

With box-sizing: border-box, width and height include the frame's thickness. As the MDN box-sizing page explains, the content area shrinks by the width of the frame.

In the test environment, the element's size did not change, but the content width (clientWidth) became narrower by the left and right frame widths (160px → 152px). The text inside also shifted 4px to the right, the frame's thickness. If the element's size must stay fixed and it's fine for the content to be slightly narrower, this is the simplest approach.

outline takes no space and is drawn outside the element

The MDN outline page says an outline takes up no space and therefore does not affect layout. The CSS specification (CSS UI 4), outline section also lists "takes no space" as the first difference from border.

In the test environment, the element's size, content width, and right neighbor's position were all the same as without a frame. The frame was drawn just outside the element's edge, and the pixel 2px outside the element's left edge had the frame color. The text inside did not move.

Showing a frame on hover does not cause jitter

I compared the position of the right neighbor when the frame appears only on hover.

css
/* the version that shifts */
.box:hover { border: 2px solid #00c8e8; }

/* the version that does not shift (outline) */
.box:hover { outline: 2px solid #00c8e8; }

Normally, the distance from the left edge of the box to the left edge of the right neighbor is 168px. With border applied on hover, the distance became 172px the moment the mouse entered, and the right neighbor moved 4px to the right. With outline, it stayed at 168px.

If you want to draw the frame with border, add a transparent border of the same width at all times, and change only the color on hover.

css
.box { border: 2px solid transparent; }
.box:hover { border-color: #00c8e8; }

With this approach, the right neighbor was already at 172px before hover, and it stayed at 172px on hover. Because the transparent frame reserves the space from the start, the size does not change on hover.

It can be hidden behind neighboring elements

Because an outline takes no space, it can overlap the elements around it. The specification notes that an outline does not affect layout, so it may overlap other elements on the page.

I set the gap to the right neighbor to 2px and gave the left box a 4px outline. The pixels in the gap had the frame color, but the 2px part that overlapped the right neighbor stayed gray, the neighbor's color. The outline was hidden under the right neighbor, which comes later in the document. Conversely, when I put the outline on the right box, the part overlapping the left box was drawn in the frame color.

The specification says outlines are drawn on top of the box, but it leaves the stacking order relative to other elements to the browser. In Chromium in the test environment, adding position: relative to the left box made the part overlapping the right neighbor also draw in the frame color.

css
.box {
  position: relative; /* keeps the frame from being hidden under later elements that have no position */
  outline: 4px solid #00c8e8;
}

In a list of cards or a row of buttons with narrow gaps, if the right or bottom side of a frame looks cut off, suspect that a following element is covering it, and try adding position: relative to the element with the frame.

It cannot be specified per side

Outline has no per-side properties such as border-top. The specification's note says an outline is the same on all sides, and there are no properties like outline-top or outline-left. In the test environment, CSS.supports() returned true for border-top, and false for outline-top and outline-bottom-width.

For a frame inside the element, use an inset box-shadow

If you give box-shadow the inset keyword with no blur and only a spread, a line is drawn inside the element.

css
.box { box-shadow: inset 0 0 0 4px #00c8e8; }

The element's size and content width did not change, and the pixels 2px inside the element's left edge had the frame color. Unlike border-box, the content area does not shrink, so the text stays at the left edge, and the content overlaps the frame. A child element placed at the top left (with a pink background) was drawn in front of the frame and covered it. To keep the content from overlapping the frame, add padding at least as large as the frame's thickness.

Using an offset, you can also draw a line on just one side. Here is an example that draws a 3px line only at the bottom.

css
.box { box-shadow: inset 0 -3px 0 #00c8e8; }

Only the inside of the bottom edge had the frame color, and the inside of the top and left edges kept the background color. The element stayed at 160×60px. This is useful when you want a line on one side only, such as the indicator for a selected tab.

Scroll range, and how overflow: hidden clips it

Inside a parent with overflow: auto that is 200px × 80px, I placed a child of the same size and gave the child a 20px frame. The parent's scrollWidth and scrollHeight were as follows.

Child's frame scrollWidth scrollHeight
None 200 80
outline: 20px 200 80
box-shadow: 0 0 0 20px 200 80
border: 20px 240 120
border: 20px + border-box 200 80

With border alone, the child overflows the parent, and the parent's scrollable range expands to 240×120px. An outline does not expand the scrollable range, even when it extends beyond the parent.

However, if the parent has overflow: hidden, an outline that extends beyond it is clipped and becomes invisible. I placed a child that filled the parent's width, gave it outline: 3px, and checked its top edge. Neither the outside nor the inside had the frame color, so the frame was not visible. When I set outline-offset: -3px to draw the frame inward, the pixel 1px inside the child's top edge had the frame color. The specification says that a negative outline-offset shrinks the outline inward, into the element.

css
.list > .item {
  outline: 3px solid #00c8e8;
  outline-offset: -3px; /* inward by the same amount as the thickness */
}

For how to fix this clipping with focus frames, see the separate article "How to Make Keyboard Focus Frames Easier to See with CSS," which covers it with images.

Wrapping a phrase that breaks across lines

When an inline element such as a <span> wraps onto two lines, border and outline change the shape of the frame.

Three-row comparison. Top, border: the frame around the phrase on the first line is open on its right end, and the frame around the phrase on the second line is open on its left end. Middle, border with box-decoration-break: clone: the phrases on the first and second lines are each closed by a rectangular frame. Bottom, outline: the first and second lines are also each closed by a rectangular frame.
A phrase wrapped onto two lines with a 3px frame

With border, the sides at the line break are left open. As the MDN box-decoration-break page explains, adding box-decoration-break: clone draws a closed frame for each line. With outline, each line got a closed frame without adding anything. The specification also says that an outline on an element spanning multiple lines is drawn to surround all of its parts, so the sides are not left open. However, according to the MDN outline page, whether it is drawn per line or as one frame around the whole element depends on the browser.

For inline elements, border also takes up horizontal space. The frame pushed the text after it to the right. The width of the phrase on the first line was 374px with outline, 377px with border (wider by the left frame), and 380px with border + clone (wider by both the left and right frames). The top and bottom frames did not change the line height, and the paragraph height was the same in every method. When you want to frame a keyword in a sentence without moving the text after it, outline is the better choice.

​Posted by 86