日本語

Negative margin Values in CSS: How They Behave

​​CSS​Flexbox,Playwright

CSS margins can also take negative values. A negative margin-top moves the element upward. A negative margin-bottom leaves the element itself in place and pulls the element after it closer. For left and right margins, a negative margin-left on an element with a set width only shifts it to the left. On an element with width: auto, the same margin makes it wider. You can use this to cancel a card's padding so an image spans the full width, or to stack avatar icons so they slightly overlap.

Note

Test environment: Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I opened a test page with Playwright, read the position and width of each element with getBoundingClientRect(), and measured them in px. I did not test Firefox or Safari. This article covers only horizontal writing mode. It does not cover vertical writing (writing-mode).

The MDN margin page says that a negative value draws the element closer to its neighbors than the default. The CSS Box Model specification also says that negative margin values are allowed, although browser implementations may have limits.

Top and bottom margins: the top moves up, the bottom pulls the next element in

I stacked three 40px-tall block elements vertically, gave the middle element a negative margin, and measured the top edge of each element. The parent is a div that is 300px wide.

Value on the middle element 1st Middle 3rd Parent height
None 0px 40px 80px 120px
margin-top: -20px 0px 20px 60px 100px
margin-bottom: -20px 0px 40px 60px 100px
margin-bottom: -40px 0px 40px 40px 80px

With margin-top: -20px, the middle element moved up 20px and overlapped the first element by 20px. The third element also moved up 20px along with it.

With margin-bottom: -20px, the middle element stayed put. Only the third element moved up 20px and overlapped the middle element. With -40px, which equals the element's height, the third element landed exactly on top of the middle element. In both cases, the parent's height shrank by the same amount.

Vertical margins of adjacent block elements can collapse into one margin. According to the MDN margin collapsing page, when positive and negative values are mixed, the result is the largest positive value plus the most negative value. The table above shows results when the neighboring element has no margin.

Left and right margins: with width auto, the element gets wider

In the horizontal direction, the result depended on whether the element had an explicit width. I placed one 40px-tall div inside a 300px-wide parent with 20px left and right padding, so the content box was 260px wide. The left and right edges are measured from the parent's left edge.

Value on the child Left Right Width
None 20px 280px 260px
margin-left: -20px 0px 280px 280px
margin-right: -20px 20px 300px 280px
margin-inline: -20px 0px 300px 300px
width: 100px; margin-left: -20px 0px 100px 100px
width: 100px; margin-right: -20px 20px 120px 100px

For a block element with no width set (width: auto), a negative margin made the element wider by the same amount. margin-left: -20px extended it 20px to the left, and margin-right: -20px extended it 20px to the right. With negative margins on both sides, the element canceled the parent's padding and spanned from edge to edge of the parent.

For an element with a fixed width of 100px, the width stayed at 100px. margin-left: -20px shifted it 20px to the left. margin-right: -20px changed neither its position nor its width.

Making an image in a card span the full width

You can use this "width auto grows" behavior to make an image or a divider band span the full width inside a card that has padding. Set the negative value to the card's padding and apply it to both sides.

css
.card {
  padding: 0 20px;
}

.card .bleed {
  margin-inline: -20px; /* Cancel the card's left and right padding */
}

I did not set a width on .bleed. In the same parent, width: 100%; margin-inline: -20px kept the width at the parent's content width (260px). The left edge was 0px and the right edge was 260px. The element did not grow. It only shifted to the left.

Overlapping parts: the lower element's background is painted on top

When negative margins make elements overlap, you may wonder which one is painted on top. The following image shows a lower element (gray) with margin-top: -40px placed under an upper element (blue).

Two screens shown one above the other. In the top screen, labeled "No value set" (the label in the image is in Japanese), the gray lower element overlaps the lower part of the blue upper element. The overlapping part is outlined with a pink dotted line and labeled "Overlapping part" (the label is in Japanese). In the overlapping part, the gray background covers the blue element, but the upper element's text is visible above the gray background. In the bottom screen, labeled "position: relative on the upper element", the blue upper element is painted above the gray lower element, and the overlapping part is blue.
Paint order of two blocks overlapping with margin-top: -40px, with no value set and with position: relative set on the upper element

Without any value set, the lower element's gray background, which comes later in the HTML, covered the upper element's blue background in the overlapping part. However, the upper element's text was visible above the gray background.

The MDN page on stacking without z-index says that elements without a position overlap in the order they appear in the HTML, and that elements with a position are painted above them. When I added position: relative to the element I wanted on top, its background and text both came to the top (bottom row of the image).

I overlapped two 60px-tall elements with margin-top: -30px. I called document.elementFromPoint() at an overlapping point to see which element was on top.

Value Topmost element at the overlapping point
None Lower element
position: relative on the upper element Upper element
position: relative on both Lower element
position: relative on both, z-index: 1 on the upper element Upper element

When I set position: relative on both elements, the lower element, which comes later in the HTML, went back on top. To control the stacking order reliably, combine position: relative with z-index.

Stacking horizontally with slight overlaps

You can also use negative margins to line up flex items with slight overlaps, such as a row of participant avatars.

css
.avatars {
  display: flex;
}

.avatars > * + * {
  margin-left: -12px; /* Overlap the 2nd and later items onto the item on their left by 12px */
}

I placed four 48px round icons in a row and gave the second and later icons margin-left: -12px. Their left edges changed from 0px, 48px, 96px, and 144px to 0px, 36px, 72px, and 108px, so each icon overlapped the previous one by 12px. When I called elementFromPoint() where the first and second icons overlapped, it returned the second icon. The icon that comes later in the HTML was painted on top.

Inline elements respond only to the horizontal direction

When I gave a span in running text a negative margin, margin-left: -16px moved the span 16px to the left, and it overlapped the text before it. margin-top: -16px did not change its position. The MDN margin page also says that top and bottom margins have no effect on non-replaced inline elements, such as span.

Percentages are based on the parent's width

When you write a margin as a percentage, the base is the width of the parent (containing block), even for top and bottom margins. The MDN margin page also says that percentages refer to the inline size of the containing block, which is the width in horizontal writing mode.

In a 300px-wide parent with 20px left and right padding, I gave a child margin-top: -10%. The value read with getComputedStyle() was -26px, which is 10% of the parent's content width of 260px. The base is not the parent's height, so you cannot use a percentage to shift an element upward based on the height.

Overflow to the left cannot be scrolled to, but overflow to the right creates a horizontal scrollbar

On a 400px-wide screen, I gave a block element that is a direct child of body and has auto width a negative margin, and measured the page's overall width (document.documentElement.scrollWidth).

Value Page width
margin-left: -50px 400px
margin-right: -50px 450px
width: 400px; margin-left: -50px 400px

With margin-right: -50px, the element extended 50px to the right. The page width increased by the part that went beyond the screen, and a horizontal scrollbar appeared. Overflow to the left did not increase the page width. Even when I tried to scroll left with window.scrollTo(-100, 0), window.scrollX stayed at 0, and the element's left edge stayed at -50px, outside the screen. The part that overflowed to the left cannot be seen, even by scrolling.

Warning

If you widen an element by setting a negative right margin, check on a phone-width screen that no horizontal scrollbar appears on the page.

Negative values are not allowed for padding

Unlike margin, padding cannot take a negative value. When I read padding-top of an element with padding: -10px using getComputedStyle(), it was 0px. When I wrote padding: 5px; padding: -10px on the same element in that order, it stayed at 5px. The negative declaration was ignored, and the earlier value remained. The MDN padding page and the CSS Box Model specification both state that negative padding values are invalid.

​Posted by 86