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:
#1 Best Overall
.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.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minute.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
- 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.
<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.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →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.
Rank #3
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
imgelement 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: 0on 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.
Recommended Free Tools
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
- 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.
Accessibility and delivery details
- Write useful
alttext for informative images. Use an emptyalt=""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
srcsetrather 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
- If the image should merely fit its parent, start with
max-width: 100%; height: auto;. - If it must always fill the parent, use
width: 100%; height: auto;and accept that a small source can be enlarged. - If every card needs the same shape, create an
aspect-ratioframe and choosecoverfor filling orcontainfor showing everything. - If different screens need different download sizes, add
srcsetand an accuratesizesvalue; CSS and HTML solve different problems. - Declare intrinsic
widthandheight, test the cascade, and verify the result at the breakpoints your layout actually uses.
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.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →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.
Best Value
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.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesFrequently 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.
Quick Recap
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.

