CSS max-width: Keep Images from Overflowing Their Container
To keep an image from overflowing its parent, add max-width: 100% and height: auto to the img. Only images wider than the parent shrink to the parent's width. Images smaller than the parent keep their original size. This is the fix for a blog photo that extends past the right edge of a smartphone screen and causes horizontal scrolling.
The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened pages that contained only HTML and CSS in Playwright and measured sizes with getBoundingClientRect(). Unless noted otherwise, the screen width was 360px, the image was 1200×600px (a PNG I generated for the test), and the parent was a div as wide as the body. The body had margin: 0 and the text size was 16px. I did not test Firefox or Safari.
img {
max-width: 100%;
height: auto;
}

- 1. Without any styles, an image keeps its original width
- 2. An image with width and height attributes needs height: auto
- 3. width: 100% stretches small images
- 4. For images with padding or a border, use box-sizing: border-box
- 5. In a Flexbox row with text, max-width alone does not shrink the image
- 6. In Grid, tables, and parents whose width depends on content, max-width alone was enough
Without any styles, an image keeps its original width
When I placed a 1200×600px image on a 360px-wide screen with no styles, it stayed at 1200×600px. The page width (scrollWidth) became 1200px, and horizontal scrolling appeared.
When I added max-width: 100% to the img, the image became 360×180px, and the page width fit within 360px. max-width sets an upper limit on the width value. As described on MDN's max-width page, a percentage is relative to the width of the containing block (here, the parent div), so 100% means "up to the width of the parent." I did not set the height, but it followed the aspect ratio and became 180px.
I opened the same page at different screen widths:
| Screen width | Image size | Page width |
|---|---|---|
| 320px | 320×160px | 320px |
| 360px | 360×180px | 360px |
| 768px | 768×384px | 768px |
| 1280px | 1200×600px | 1280px |
At 1280px, wider than the image, the image stayed at its original 1200px and did not grow further. max-width only sets a limit. It never stretches an image.
If the parent has padding, the content width inside it becomes the limit. With padding: 0 16px on the parent, the image at 360px became 328×164px, which is 360px minus 32px for the left and right padding.
An image with width and height attributes needs height: auto
When I put max-width: 100% alone on an image with width and height attributes, such as <img src="photo.jpg" width="1200" height="600" alt="…">, the image became 360×600px. Only the width shrank. The height stayed at the 600px attribute value, so the image looked stretched vertically.
According to how the HTML Living Standard handles the width and height attributes of img, these two attributes map to the CSS width and height properties and also to aspect-ratio. In my test, the computed height was 600px and the computed aspect-ratio was auto 1200 / 600. Because the CSS height was fixed at 600px, the height did not change when the width shrank.
Adding height: auto made the image 360×180px, and its aspect ratio was restored. The attribute values are also used to convey the aspect ratio, so I kept the attributes and set height: auto in CSS. MDN's img page also says that giving width and height lets the browser calculate the aspect ratio and reserve space before the image loads, which reduces layout shift.
You can also set the attribute values to the size you want to display. With width="600" height="300" and max-width: 100%; height: auto;, the image was 360×180px on a 360px screen and 600×300px (the attribute size) on a 1280px screen.
width: 100% stretches small images
If you write width: 100% instead of max-width: 100%, the image always matches the parent's width. I compared these with a 200×100px image:
| Styles on the 200×100px image | Image size (screen width 360px) |
|---|---|
max-width: 100% |
200×100px |
width: 100% |
360×180px |
width: 100%; max-width: 300px |
300×150px |
With width: 100%, the 200px image was stretched to 360px, displaying it at 1.8 times its original width. On pages with icons or small diagrams that should appear at their original size, use max-width: 100%. For hero images that should always fill the frame, use width: 100%. If you do not want them stretched too far, add a maximum length with max-width.
For images with padding or a border, use box-sizing: border-box
When I added padding: 8px and border: 2px solid to an image to give it a photo-frame look, the image was 380px wide even with max-width: 100%. On a 360px screen, it overflowed by 20px. With the default content-box, padding and border are added outside the width. See MDN's box-sizing page.
img {
max-width: 100%;
height: auto;
padding: 8px;
border: 2px solid #1f2a30;
box-sizing: border-box; /* Keep the width including padding and border within 100% */
}
With this rule, the total width including the border became 360px, and the overflow disappeared.
In a Flexbox row with text, max-width alone does not shrink the image
When an img and a paragraph (p) sit side by side as children of a parent with display: flex, the image did not fit on the screen even with max-width: 100%.
<div class="row">
<img src="photo.png" alt="写真の説明">
<p>画像の横に並べる文章です。</p>
</div>
.row {
display: flex;
gap: 16px;
}
p {
margin: 0;
}
img {
max-width: 100%;
}
The image shrank to 360px but not below that. The text was squeezed to 30.39px wide, so the 16px text wrapped with only one character per line. The page width became 406px, and horizontal scrolling appeared. The image was also stretched vertically to 360×288px. According to MDN's align-items page, when align-items is not set, its default normal behaves as stretch for flex children. So the image's height was stretched to 288px, the same as the tall text. When I changed the vertical alignment by adding align-items: flex-start to the parent, the image returned to 360×180px, but the overflow remained.
The image does not shrink further because, for flex children, the default min-width: auto treats the content size as the minimum width. The Flexbox specification on how the minimum size of flex items is determined says that a child with min-width: auto does not shrink below its content size, and that the content size is limited by max-width. Here, the 360px from max-width: 100% became the image's minimum width.
There are two fixes, and both fit the image within the screen. The third row of the table below shows the result when the widths are divided differently.
| Approach | Image size | Text width | Page width |
|---|---|---|---|
Add min-width: 0 to the img |
293.52×146.75px | 50.48px | 360px |
Wrap the img in a div |
293.5×146.75px | 50.48px | 360px |
Give the wrapping div
flex: 1 |
137.59×68.8px | 206.41px | 360px |
Adding min-width: 0 to the img lets the image shrink. Wrapping the img in a div makes the div the flex child, and the img's max-width: 100% becomes relative to the div's width. In this case too, the image shrank.
However, in both cases the text was only 50.48px wide, which is too narrow to read. To divide the width between the image and the text, use flex. When I gave the wrapping div
flex: 1, the image became 137.59px wide and the text 206.41px wide.
In Grid, tables, and parents whose width depends on content, max-width alone was enough
In a Grid with grid-template-columns: 1fr 1fr, placing an image with no styles in the left-column div widened the image column to 1200px, and the page width became 1232px. When I added max-width: 100% to the img, both columns became 172px, and the image shrank to 172×86px to fit.
With only max-width: 100% on the image, the image fit at 360×180px and the page width stayed at 360px in the following parents:
- A
tablecell (with nowidthon thetable, withwidth: 100%, or withtable-layout: fixed) - A parent with
display: inline-block,float: left,position: absolute, orwidth: fit-content
When img had max-width: 100%, the Grid 1fr columns, tables without a set width, and inline-block parents did not widen to fit the image. The CSS Sizing specification on replaced elements with percentage sizes says that when a percentage such as the max-width of a replaced element like img refers to a parent whose width depends on the image's size, the browser treats that percentage as 0 only when calculating the minimum content width. Because the image's minimum width is treated as 0, the parent does not widen to fit the image.
When an img sits directly as a Flexbox child, its minimum width is the 360px from max-width: 100%, as described in the previous section. In Flexbox, add min-width: 0 or wrap the img in a div.





