Skip to content
Blog

CSS Images: How to Resize, Crop and Add Background Images

CSS Images: How to Resize, Crop and Add Background Images
In this article
  1. Make images responsive without stretching them
  2. Crop an image to a fixed shape with object-fit
  3. Fixed shape, flexible width: aspect-ratio
  4. Background images
  5. Sharp backgrounds on high-resolution screens
  6. Fix the small gap under images
  7. Quick reference

CSS works with images in two ways. It styles <img> elements that sit in your HTML (size, cropping, rounded corners), and it adds decorative images to any element with background-image. The three rules that fix most image problems are max-width: 100%; height: auto; so images shrink without distortion, object-fit: cover to crop an image into a fixed box, and background-size: cover for full-width banners. We ran the key examples below in headless Chrome 153 and give the sizes it measured.

Terminal showing measured image sizes in headless Chromium: 300x150 with max-width and height auto, 300x300 with object-fit cover, 320x180 with aspect-ratio, and the inline image gap
Measured in headless Chromium: what each CSS rule does to an 800×400 image

Make images responsive without stretching them

A photo that is 800 pixels wide will overflow a 300 pixel column on a phone. The standard fix is:

img {
  max-width: 100%;
  height: auto;
}

Both lines matter. We placed an 800 by 400 pixel image, with width="800" height="400" in the HTML, inside a 300 pixel wide box:

CSS on the imageRendered sizeResult
max-width: 100%; height: auto;300 x 150Scaled down, correct shape
max-width: 100%; only300 x 400Distorted: the HTML height of 400 still applies

Keep the width and height attributes in your HTML anyway. According to MDN’s img reference, they let the browser work out the aspect ratio before the image downloads and reserve the space, which prevents the page from jumping as images load. We checked this with an image that had no file loaded yet: with the attributes and height: auto, the browser already reserved a 300 x 150 space for it.

Crop an image to a fixed shape with object-fit

Product grids, team photos and blog cards often need every image to be the same size, whatever shape the original is. Setting both width and height stretches the picture. object-fit tells the browser how to fit it instead:

.card img {
  width: 300px;
  height: 300px;
  object-fit: cover;
  object-position: center;
}

Our 800 by 400 image rendered as a 300 by 300 square, filled edge to edge with the sides cut off. The values, as MDN describes them:

ValueWhat happens
fill (default)Stretched to fill the box; the shape may distort
coverFills the box, keeps its shape, and is clipped to fit
containThe whole image is visible, with empty bands (“letterboxed”) if the shapes differ
noneShown at its natural size
scale-downWhichever of none or contain gives the smaller image

object-position chooses which part stays visible when the image is cropped. Its default is 50% 50%, the centre. Use object-position: top for portraits, so heads are not cut off.

Fixed shape, flexible width: aspect-ratio

If the width should follow the layout but the shape should stay fixed, combine aspect-ratio with object-fit:

.hero img {
  width: 100%;
  aspect-ratio: 16 / 9;
  height: auto;
  object-fit: cover;
}

In a 320 pixel container the image came out at 320 x 180, exactly 16:9, although the file itself is 2:1.

Background images

Use background-image for decoration: banners behind a heading, patterns, textures. A full-width banner looks like this:

.banner {
  min-height: 320px;
  background-image: url("/images/banner.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}
  • background-size: cover scales the image, keeping its shape, until it covers the whole element; the overflow is cropped. contain scales it as large as possible without cropping, which can leave gaps. Both are defined on MDN’s background-size page.
  • The path in url() is relative to the CSS file, not to the HTML page. If the image works in the page but not in your stylesheet, this is usually why.
  • An element with only a background image has no height of its own, so give it content, padding or a min-height.

Do not put meaningful pictures in the background. MDN warns that browsers give assistive technology no information about background images, so a screen reader will not announce them. Product photos, charts and anything with text belong in an <img> with alt text.

Sharp backgrounds on high-resolution screens

image-set() lets the browser pick the best file for the screen, and MDN lists it as widely available:

.banner {
  background-image: image-set(
    url("banner.jpg") 1x,
    url("banner@2x.jpg") 2x
  );
}

You can also offer formats with type(), for example an AVIF file with a JPEG fallback. Chrome accepted both forms in our test.

Fix the small gap under images

Images are inline elements by default, and MDN explains that the bottom of an inline image sits on the text baseline. The space below the baseline, meant for letters like “g” and “y”, shows up as a gap. In our test, a box holding a 150 pixel tall image measured 154 pixels with a 16 pixel font. Adding one line fixed it, and the box measured exactly 150:

img {
  display: block;
}

Quick reference

You want toCSS
Shrink images to fit the screenmax-width: 100%; height: auto;
Crop to a square or cardwidth and height plus object-fit: cover;
Keep a fixed shape at any widthaspect-ratio: 16 / 9; object-fit: cover;
Round avatarA square image plus border-radius: 50%;
Full-width bannerbackground-size: cover; background-position: center;
Remove the gap below an imagedisplay: block;

CSS resizing only changes how an image is displayed, not the file the visitor downloads. A 4 MB photo shown at 300 pixels still costs 4 MB, so resize and compress images before you upload them. If you are new to stylesheets, our KB article on inline, internal and external CSS explains where these rules go, and when your site is ready to go live, our shared web hosting includes free SSL and NVMe storage.

Related articles

Start Building Today

Ready to Get Started?

VPS, dedicated servers, streaming and managed hosting with 24/7 support from the Ucartz team.