Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use max-width: 100%; height: auto; on the image. The image can shrink to fit its containing block, while height: auto keeps its intrinsic proportions. Unlike width: 100%, this rule does not enlarge a small source image. From there, choose a different pattern only when your design needs a full-width image, a fixed-ratio frame, or different image files for different screens.

The default responsive-image rule

Put this rule in your stylesheet:

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

max-width: 100% limits the rendered width to the width of the containing block. If the source image is wider than that block, the browser scales it down instead of letting it overflow. If the source is already narrower, it remains at its natural rendered width. height: auto tells the browser to calculate the height from the image’s intrinsic aspect ratio, so the picture is not stretched or squashed.

This is the right starting point for articles, cards, product descriptions and other content images where the whole image should remain visible and enlargement is unnecessary.

Scope the rule when necessary

A global selector is convenient, but a component selector prevents unrelated images from inheriting a layout decision:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
.article-content img {
  max-width: 100%;
  height: auto;
  display: block;
}

display: block is optional. It removes the small baseline gap that inline images can leave beneath themselves and makes predictable spacing easier. It does not control responsiveness.

When to use width: 100% instead

Use width: 100%; height: auto; when the image is intended to occupy the entire width of its parent at all times:

.hero-image {
  width: 100%;
  height: auto;
}

This can enlarge a source that is smaller than its parent. That may be desirable for a banner or a full-bleed content image, but enlargement can make a low-resolution file look soft. If your requirement is only “never overflow,” keep max-width: 100%.

Requirement CSS What happens to a small source
Shrink when needed, never exceed natural size max-width: 100%; height: auto; Not enlarged
Always fill the parent width width: 100%; height: auto; May be enlarged
Fill a fixed frame width: 100%; height: 100%; object-fit: …; Follows the selected fit behavior

Responsive images in a fixed-ratio frame

Cards, galleries and video-style thumbnails often need every tile to have the same shape. In that case, make the frame responsible for the dimensions and tell the image how to fit inside it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
.image-frame {
  aspect-ratio: 16 / 9;
  overflow: hidden;
}

.image-frame img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
}

object-fit: cover fills the frame while preserving the image’s proportions. If the source and frame have different ratios, some of the image is cropped. object-position controls which part remains visible; for a subject near the top, for example, use object-position: center top.

Show the entire image with contain

.image-frame img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
  background: #eee;
}

contain preserves the complete image and fits it inside the frame. If the ratios differ, unused space appears on two sides. A background color can make that space intentional rather than surprising.

Do not force a frame when you need the intrinsic ratio

For an ordinary article image, forcing unrelated width and height values can distort the layout or create an unnecessary crop. Let the image determine its height with height: auto, or use a deliberate aspect-ratio frame only when a consistent shape is part of the design.

Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

CSS sizing versus responsive image files

CSS controls how the chosen file is rendered. It does not provide smaller downloads for phones or higher-resolution files for large displays. Use HTML srcset and sizes when the browser should choose among several source files.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<img
  src="photo-800.jpg"
  srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
  sizes="(max-width: 600px) 100vw, 800px"
  width="1600"
  height="900"
  alt="Description of the photograph"
>

The width descriptors in srcset advertise the available file widths. The sizes value describes the image’s intended layout width: up to 600 pixels of viewport width it can use the full viewport width; above that, the layout reserves 800 pixels. The browser combines those hints with device pixel density and network conditions to select a candidate.

The CSS can remain:

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

Think of the tasks separately: CSS prevents layout overflow, while srcset and sizes offer alternate files and selection hints.

Why include width and height attributes?

Provide the source image’s intrinsic dimensions when they are known. The browser can establish the correct aspect ratio before the file finishes loading and reserve the appropriate space, reducing layout movement. These attributes do not force the image to render at that pixel size when CSS makes it flexible.

Useful patterns you can copy

Responsive article image

.post-body img {
  max-width: 100%;
  height: auto;
  display: block;
  margin-inline: auto;
}

This keeps the complete image visible, prevents overflow and centers an image that is narrower than the text column.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Full-width hero with a maximum content width

.hero {
  width: 100%;
  max-width: 1200px;
  margin-inline: auto;
}

.hero img {
  width: 100%;
  height: auto;
  display: block;
}

The hero fills its wrapper, while the wrapper stops growing beyond 1,200 pixels. Adjust that value to your layout rather than treating it as a universal requirement.

Thumbnail card with a stable shape

.card-media {
  aspect-ratio: 4 / 3;
  overflow: hidden;
}

.card-media img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Choose this only when cropping is acceptable. Replace cover with contain when every pixel of the source must remain visible.

Responsive image with a focal point

.card-media img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: 50% 20%;
}

The second percentage moves the visible area toward the top, which can keep a face or product near the focal point as the frame changes shape.

Common problems and fixes

The image still overflows

  • Check that the rule targets the actual img element and is not overridden by a later declaration with a fixed width.
  • Inspect the containing block. A long unbroken word, wide table or other child may be causing the parent itself to overflow.
  • For a flex or grid item, allow the item to shrink with min-width: 0 on the relevant parent when its default minimum size prevents shrinking.
  • Look for inline styles such as style="width: 900px" or a framework utility that wins the cascade.

The image is stretched or squashed

Remove a forced height or pair the dimensions with height: auto. If the design intentionally uses a frame, set both dimensions on the frame and use object-fit: cover or contain rather than allowing the replaced element to distort.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

There is an unexpected crop

object-fit: cover is working as designed: it fills the box by trimming overflow. Use contain to show the complete source, change the frame ratio, or adjust object-position to move the crop.

A small image looks blurry

width: 100% may be enlarging it. Use max-width: 100% when enlargement is not wanted, or supply a larger source through srcset. CSS cannot create detail that is absent from the source file.

The page jumps while images load

Add accurate intrinsic width and height attributes, or reserve space with a deliberate aspect-ratio frame. Avoid guessing dimensions: an incorrect ratio reserves the wrong space and causes a later adjustment.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

srcset does not seem to change the file

Verify that each candidate has a correct width descriptor, that sizes matches the actual layout width, and that the browser is not reusing a cached candidate. A responsive CSS rule alone will not cause alternate files to be downloaded.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Accessibility and delivery details

  • Write useful alt text for informative images. Use an empty alt="" for purely decorative images so assistive technology can skip them.
  • Keep the source’s intrinsic ratio unless you have deliberately selected a crop. Cropping can remove context that the alternative text does not replace.
  • Choose source files that are large enough for the largest intended rendered size, then offer smaller candidates with srcset rather than sending the largest file everywhere.
  • Test narrow and wide viewports, high-density displays, slow loading and images with subjects near each edge. A crop that looks fine in one frame can hide important content in another.

A practical decision checklist

  1. If the image should merely fit its parent, start with max-width: 100%; height: auto;.
  2. If it must always fill the parent, use width: 100%; height: auto; and accept that a small source can be enlarged.
  3. If every card needs the same shape, create an aspect-ratio frame and choose cover for filling or contain for showing everything.
  4. If different screens need different download sizes, add srcset and an accurate sizes value; CSS and HTML solve different problems.
  5. Declare intrinsic width and height, test the cascade, and verify the result at the breakpoints your layout actually uses.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If you need a screenshot of a responsive page to verify how these rules render, ScreenshotNeo can capture the URL through one request. It accepts the cookie or consent banner like a visitor and removes more than 60 known consent platforms, newsletter popups and chat widgets before the capture; each cleanup step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads and cache hits cost nothing, and the response identifies the page verdict and billing status in X-Page-Verdict and X-Billed headers.

See the full parameter list in the ScreenshotNeo documentation. A basic cURL request is:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Python:

import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)

Node.js:

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

For visual regression work, its options include full-page capture with lazy images loaded, CSS-selector element capture, device presets and custom viewports, retina scale, custom CSS or JavaScript, selector waits, network-idle waits, request blocking, cookies and headers, caching with a chosen TTL, asynchronous jobs with signed webhooks, bulk capture of up to 100 URLs per call, PDFs and signed links. Its MCP server supplies take_screenshot, get_page_info and capture_pdf tools to Claude, Cursor and other MCP clients.

The Free plan includes 1,000 screenshots per month with no card. Paid plans start at $5 for 3,000 shots; every feature is available on every plan, and yearly billing gives two months free. Create a free ScreenshotNeo account to capture your responsive layouts.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

FAQ

Should I put responsive image CSS on every image?

Use a shared rule for content images that should not overflow, but scope it when logos, icons or deliberately fixed-size graphics need different behavior.

Does max-width: 100% improve image quality?

No. It controls rendered size. Image quality depends on the source resolution and on choosing an appropriate file with srcset when multiple sizes are available.

Can I use object-fit without a fixed height?

It is most useful when the replaced element has a defined box to fit into. For naturally sized article images, height: auto is usually the simpler choice.

Do srcset and sizes replace CSS?

No. They help select a file; CSS still determines how that selected file is laid out and constrained.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Should I put responsive image CSS on every image?

Use a shared rule for content images that should not overflow, but scope it when logos, icons or deliberately fixed-size graphics need different behavior.

Does max-width: 100% improve image quality?

No. It controls rendered size. Quality depends on source resolution and selecting an appropriate file with srcset when alternatives exist.

Can I use object-fit without a fixed height?

It is most useful when the image has a defined box to fit into. Naturally sized article images generally work better with height: auto.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.