designwithfocus

Web design basics

How to Add White Space Around an Image in CSS

By Design With Focus Team 5 min read
An image with even white padding inside a thin border

Short answer

White space around an image is padding inside the box or margin outside it. MDN says padding sits between content and border, and margin sits around the element. A white background on the padding reads as a white gap. Margins are transparent. object-fit only changes how the image fills its own content box.

Last verified against the sources listed below.

Key takeaways

  • Padding adds space between the image content and the border. Margin adds space outside the border.
  • A background color paints the content, padding, and border areas. Margins stay transparent.
  • One padding value sets all four sides. Two, three, or four values follow the usual order.
  • Negative padding is invalid. Margin may be negative and can pull neighbors closer.
  • object-fit sizes the image inside its content box. Unfilled space there shows the background.

How to add white space around an image is a box-model task. MDN’s padding page says padding is the space between an element’s content and its border. Margin is the space around the element. The white look comes from a background, not from the length alone.

CSS is what sets those properties. The image file does not have to grow.

What is the box around the image?

Every element is a box with four areas. The box model introduction lists them from the inside: content, padding, border, and margin.

The content area holds the image. Padding wraps that content. The border wraps the padding.

The margin is an empty band outside the border that separates the box from its neighbors. The CSS box model spec says each box has a content area, a band of padding, a border, and a margin outside the border. It also says the background paints the content, padding, and border areas, and that margins are always transparent.

The HTML spec says an img element represents an image. Padding on that element, or on a wrapper, is what opens a gap around the pixels.

How do padding and margin differ?

Padding is inside the border. Margin is outside it. That single difference decides whether the gap picks up the element’s background.

MDN says one padding value applies to all four sides. Two values set top and bottom, then left and right. Three values set top, then left and right, then bottom.

Four values go clockwise from the top: top, right, bottom, left. Each value is a length or a percentage. Negative padding is invalid.

Margin uses the same one-to-four value pattern and also allows the keyword auto. Negative margin is valid and pulls the element toward its neighbors. Top and bottom margins do not apply to non-replaced inline elements. An image is a replaced element, so that limit is about elements such as span, not about img.

Percentage padding and margin refer to the inline size of the containing block, which in a horizontal language is its width. A percentage gap changes when that containing block changes. A length such as 1rem or 24px does not.

Which method should you use?

Pick padding, margin, or a larger file. They are not the same white space.

Method Where the gap lives What paints it
padding on the image or a wrapper Between the content and the border The element’s background, which you can set to white
margin on the image or a wrapper Outside the border, before the neighbors Nothing of its own. The margin is transparent
A larger canvas in an editor Pixels inside the image file Whatever you painted before export
object-fit Inside the content box, if the picture does not fill it The replaced element’s background, not a new outer mat

CSS Images says object-fit specifies how the contents of a replaced element fit the box established by its used width and height. If the content does not fill the content box, the unfilled space shows that element’s background. The draft of the same module says the same.

That inner gap is useful for a fixed frame. It is not padding around the frame.

How do you write the CSS?

Set the padding, then a white background, then a margin if neighbors need room. The lengths below are ordinary CSS lengths from the property syntax, not a required size.

  1. Choose the element. Use the img when the gap belongs to the picture. Use a wrapper, such as a figure, when the gap should include the image and its caption as one box.
  2. Set padding. padding: 1rem puts the same gap on all four sides. padding: 1rem 2rem puts 1rem on the top and bottom and 2rem on the left and right.
  3. Set background-color: white on that same element so the padding band is white. Without a background, the band shows whatever is behind the box.
  4. Set margin if you also need space outside the border. margin: 1rem auto adds vertical space and, with a width, can center the box. Leave margin at its initial value of 0 if the padding is the only gap you want.
  5. Add a border only if you want a line outside the white band. The border sits outside the padding, so the white mat stays inside the line.
  6. Use object-fit only when the image must fit a width and height you set. object-fit: contain keeps the aspect ratio and may leave bands inside the content box. Those bands are not a substitute for step 2.
img.mat {
  padding: 1rem;
  background-color: white;
  border: 1px solid black;
}

The box model in practice is the longer walk through content, padding, border, and margin.

Responsive layout is why a padding length can change in a media query while a baked-in canvas cannot.

After you export the image, an image compressor can shrink the file. Run it on the picture itself. Do not expect it to add the white band. The band is the padding and the background in the stylesheet.

Frequently asked questions

Should the white gap be padding or margin?

Use padding when the white area should sit inside the border and take the background. Use margin when the gap should separate the box from its neighbors. Margins are transparent, so a margin looks white only if something behind it is white. For a white mat inside a frame, set the background and the padding, then the border.

Can you put padding on the img element itself?

Yes. Padding applies to an img, which is not one of the table parts MDN excludes. The HTML spec says an img represents an image. Padding adds space between the image and the border. Use a wrapper when the gap should include a caption too. Put the background on the same element as the padding.

Why does the gap look transparent instead of white?

The gap is white only if a background paints it. The box model says backgrounds paint the content, padding, and border areas, and that margins are always transparent. A margin over a parent with no background shows whatever is behind that parent. Set background-color to white on the element whose padding should read as a mat, or on the parent if the gap is margin.

Does object-fit add white space around the picture?

It can leave empty space inside the content box, which is not padding. The CSS Images spec says object-fit fits a replaced element's contents to the box from its used width and height. If the content does not fill that box, the leftover space shows the element's background. Contain may letterbox the image inside the content box. Use padding when you want space outside that content.

Will a larger export replace the CSS gap?

A larger canvas bakes the gap into the file. CSS padding and margin do not. Baked pixels travel with the image and cannot change per layout. A stylesheet can use a different padding at a different width. Margin may also be negative, which an exported canvas cannot do. Compress the file if you need a smaller download. Compression does not add the gap. The gap is still a CSS property.

Sources