Small SEO Tools

How to Keep a Padded Image Inside Its Container

An image can overflow its card even when its file dimensions are correct and its CSS width is 100%. Padding and borders may be added outside that width. Check the image’s rendered box before making another smaller export.

This example keeps one 300 by 200 PNG unchanged. The repair changes how CSS accounts for its decoration, not the pixels stored in the file.

Check the file and the rendered box separately

Open the actual file delivered by the page. Load it in the Image Resizer and read Original size and the populated Width and Height fields. Our synthetic test PNG loads as 300 by 200. This check does not require selecting Resize image or exporting a replacement.

Next, inspect the image element in browser developer tools. Record its active width, padding, border and box-sizing, then compare its outer edge with the parent’s content area. A file’s pixel grid and an element’s CSS box answer different questions; the image resource and display-size guide covers that separate distinction.

For this example, the parent has a 300 CSS-pixel content width and no padding or border of its own:

<div class="parent">
  <img class="photo" src="/images/box-test.png"
       width="300" height="200"
       alt="Blue test image with a central red square">
</div>

Account for padding and borders in the width

These rules reproduce the overflowing image:

.parent {
  width: 300px;
}

.photo {
  display: block;
  width: 100%;
  height: auto;
  box-sizing: content-box;
  padding: 16px;
  border: 2px solid #172033;
}

With content-box, the 100% width gives the image content 300 CSS pixels. The left and right padding add 32 more, and the two borders add 4. The outer width is therefore 336 CSS pixels.

MDN’s box-model introduction distinguishes the content, padding, border and margin areas. Inspect the actual rule rather than assuming the width label includes every area. A theme or reset may already change the element’s sizing behavior.

Include the decoration with border-box

For this component, keep the existing width and decoration and change the sizing rule:

.photo {
  box-sizing: border-box;
}

Now the specified width includes padding and borders. The complete decorated image fits the 300-pixel parent, while its content area becomes 264 pixels wide. MDN’s box-sizing reference explains this change in width accounting. The padding still takes space; it has moved inside the specified total.

We loaded the original in the current resizer code locally in Chrome, then displayed exactly the same PNG bytes in six native image elements. An independent decoder checked source dimensions and screenshot border, padding and image positions, alongside separate box arithmetic:

Parent width Image setup Content width Outer width Horizontal overflow
300 No padding or border 300 300 0
300 Decorated content-box 300 336 36
300 Decorated border-box 264 300 0
200 No padding or border 200 200 0
200 Decorated content-box 200 236 36
200 Decorated border-box 164 200 0

All measurements are CSS pixels. Each decorated case uses 16 pixels of padding and a 2-pixel border on every side. The parent’s measured scroll width exceeded its client width only in the two decorated content-box cases. These observations describe this controlled component, not every cause of page overflow.

Review the saved component at narrow widths

  1. Confirm the saved image rule uses the intended sizing mode. Check the computed style after the stylesheet and any theme overrides load.
  2. Compare the outer border edge with the parent’s available content width at each relevant layout size.
  3. Inspect the smaller picture area created by internal padding. If the image needs more room, reconsider the decoration or component layout.
  4. Check neighboring content too. Margins, fixed-width ancestors, minimum sizes or long text can create separate overflow.

Keep the original image when its dimensions already meet the resource requirement. If the whole page still appears too wide on a phone, the viewport and layout guide covers that broader check. Verify the final component instead of treating a smaller file as a CSS repair.