Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
CSS

How to Make an Image Responsive in HTML: CSS, srcset, sizes, and picture

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

Make an image responsive by constraining it to its container and letting its height follow its intrinsic aspect ratio:

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

Add the image’s intrinsic width and height attributes to reserve space while it loads. When several file sizes exist, add srcset and sizes so the browser can select an efficient source. Use <picture> when mobile and desktop need different crops or compositions.

The simplest responsive image

Start with a normal image element and two CSS declarations:

<img
  src="photo.jpg"
  alt="Description of the image"
  width="1200"
  height="800"
>
img {
  max-width: 100%;
  height: auto;
}

max-width: 100% prevents the image from becoming wider than its containing block. height: auto recalculates the height from the source aspect ratio, so the image does not stretch or squash. Google’s responsive-image guidance explains that CSS can allow content to overflow its container, which is why the maximum-width rule is needed (web.dev).

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

The intrinsic dimensions in the HTML are not the display size. They describe the source ratio and let the browser reserve the correct amount of space before the file arrives, reducing layout shift. Use the real pixel dimensions whenever you know them.

Scope the rule when necessary

A global rule affects every image. If some images intentionally use a different layout, apply a class instead:

.article-content img {
  max-width: 100%;
  height: auto;
  display: block;
}

display: block removes the small inline baseline gap that can appear below images. It is optional for responsiveness.

width: 100% versus max-width: 100%

Rule Result Use it when
max-width: 100% The image shrinks when its container is narrower, but a small source image is not enlarged beyond its intrinsic width. You want a safe default that prevents overflow without forcing upscaling.
width: 100% The image always fills the available width, including when that means enlarging a smaller source. The design requires a full-bleed or full-column image.

Keep height: auto with either rule unless you deliberately implement cropping. Setting unrelated fixed width and height values distorts the image. If a fixed box is required, define the crop explicitly and use an appropriate object-fit strategy.

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.

Prevent overflow in real layouts

Check the containing block

An image can still appear to overflow if its parent has a fixed width, excessive padding, or a long unbroken layout. Ensure the image’s containing block can shrink and that the responsive rule applies to the actual element. For a centered column:

.content {
  max-width: 70rem;
  margin-inline: auto;
  padding-inline: 1rem;
}

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

Use intrinsic dimensions

For a 1200 × 800 source, keep width="1200" height="800" even if CSS displays it at 600 × 400. The ratio tells the browser how much space to reserve. MDN documents these attributes and responsive-image behavior in its <img> reference.

Serve different resolutions with srcset and sizes

CSS controls the rendered size; it does not by itself reduce the bytes downloaded. When you have multiple files of the same image at different intrinsic widths, provide candidates with srcset and describe the expected slot width with sizes:

<img
  src="photo-800.jpg"
  srcset="photo-480.jpg 480w, photo-800.jpg 800w, photo-1200.jpg 1200w"
  sizes="(width <= 600px) 480px, (width <= 1000px) 800px, 1200px"
  width="1200"
  height="800"
  alt="Description of the image"
>

What the browser does

Each w descriptor states the candidate file’s intrinsic width. The browser evaluates the first matching condition in sizes, estimates the slot width, and chooses a suitable candidate. The sizes value is only a selection hint; it does not set the image’s CSS width. Keep the CSS rule that constrains or fills the layout.

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

If you omit sizes while using width descriptors, the default source size is 100vw. That can cause a browser to choose a larger file than necessary when the image actually sits in a narrower column. Write conditions that match your layout, not merely the device’s screen width. Details and examples are available in MDN’s responsive-images guide.

Density descriptors for same-size assets

When the displayed dimensions stay the same but you have standard- and high-density files, use density descriptors:

<img src="photo.jpg" srcset="photo.jpg 1x, [email protected] 2x" alt="Description of the image">

Do not mix w and x descriptors in one srcset list.

Use picture for art direction

srcset and sizes select a different resolution of the same composition. Use <picture> when the composition or crop itself must change:

<picture>
  <source media="(max-width: 600px)" srcset="photo-square.jpg">
  <img
    src="photo-wide.jpg"
    alt="Description of the image"
    width="1200"
    height="675"
  >
</picture>

The browser uses the first matching <source>; the nested <img> remains the fallback and supplies the accessible name. You can combine a srcset on a source with a media condition when each art-directed crop also has multiple resolutions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Question Choose
Is the picture the same, but available at several widths? srcset plus sizes.
Does the mobile version need a different crop or composition? <picture> with media-conditioned <source>.
Do you only need to stop overflow? CSS max-width: 100%; height: auto;.

Accessibility and layout stability

Write the right alt text

  • For an informative image, describe its purpose or meaningful content in alt.
  • For a purely decorative image, use an empty value: alt="".
  • Do not repeat nearby caption text unless the repetition is useful.

Every responsive technique still needs an accessible <img> fallback. The <picture> element does not replace alt.

Reserve the correct space

Include intrinsic width and height whenever possible. The browser can calculate the aspect-ratio space before downloading the image, reducing unexpected movement as the page loads. If you intentionally crop into a fixed box, document that behavior in the CSS rather than pretending the source ratio is unchanged.

A complete responsive-image pattern

<figure class="article-media">
  <picture>
    <source
      media="(max-width: 600px)"
      srcset="photo-square-480.jpg 480w, photo-square-800.jpg 800w"
      sizes="100vw"
    >
    <img
      src="photo-wide-800.jpg"
      srcset="photo-wide-480.jpg 480w, photo-wide-800.jpg 800w, photo-wide-1200.jpg 1200w"
      sizes="(width <= 700px) 100vw, 70rem"
      width="1200"
      height="675"
      alt="A hiker crossing a mountain ridge"
    >
  </picture>
  <figcaption>The same subject uses a square crop on narrow screens.</figcaption>
</figure>
.article-media {
  margin: 0;
}

.article-media img {
  display: block;
  max-width: 100%;
  height: auto;
}
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshooting responsive images

The image still overflows on mobile

  • Inspect the element and verify the rule is applied to the image, not only a different selector.
  • Check for an inline style or later rule that sets a larger fixed width.
  • Look for a parent with a fixed width or padding that prevents the containing block from shrinking.
  • Check whether a different element, such as an embedded frame, is overflowing instead of the image.

The image looks stretched

Use height: auto and ensure the HTML dimensions represent the source ratio. If you need a fixed crop, use a deliberate box and object-fit; do not assign arbitrary width and height values.

The browser downloads an unnecessarily large file

Confirm that the files in srcset have accurate width descriptors and that sizes describes the real rendered slot. Remember that sizes does not resize the image. CSS still controls display width.

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

The wrong crop appears

Check the order of <source> elements. The first matching media condition wins. Put the narrowest or most specific condition before a broader one, and keep a reliable <img> fallback.

Layout jumps while the image loads

Add accurate intrinsic width and height attributes. Without them, the browser may not know the image’s ratio until the resource is decoded.

Performance and maintenance checklist

  • Use max-width: 100%; height: auto; as the baseline.
  • Choose width: 100% only when filling the container is intentional.
  • Publish multiple intrinsic widths when download-size selection matters.
  • Match sizes to the actual layout slot.
  • Use <picture> for art direction, not merely for ordinary scaling.
  • Provide accurate alt, width, and height values.
  • Test narrow, wide, and intermediate viewport widths; responsive breakpoints are not limited to phones and desktops.

Or skip the browser setup

If you need screenshots of responsive states rather than an image embedded in your page, ScreenshotNeo returns a clean PNG, JPEG, WebP, or PDF from one GET request. It accepts cookie and consent banners as a visitor, then removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and the response identifies the page verdict and billing status in X-Page-Verdict and X-Billed headers.

cURL:

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}`);

See the complete parameter reference in the ScreenshotNeo documentation. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for 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 screenshots. Create a free ScreenshotNeo account.

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

Frequently Asked Questions

Does responsive CSS change the original image file?

No. CSS changes how the browser lays out the image. Use responsive source files with srcset and sizes when you also want to reduce downloaded bytes.

Can I use srcset without sizes?

Yes, but with width descriptors the browser assumes a default source size of 100vw. Add sizes when the image occupies a narrower or variable slot.

Which element provides alt text inside picture?

The nested <img> provides the fallback image and its alt text; <source> elements do not carry the accessible name.

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Read next

Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.