To resize an image dynamically, use CSS to control the size and crop it occupies on the page, and use HTML’s srcset and sizes attributes when you want the browser to choose a suitable image file for the layout and screen. CSS changes how an image is displayed; responsive-image markup can change which file is downloaded. Use both when you need responsive presentation and appropriately sized downloads.
What “dynamic image resizing” means
The phrase can describe two different jobs. First, an image can scale or crop to fit a changing layout. Second, a browser can select a different source file based on the available layout space and device conditions. CSS handles the first job; responsive image attributes handle the second.
An image made smaller with CSS may still use the same large source file. Conversely, srcset can offer smaller files, but it does not decide the image’s visual crop or make it fill a particular box. Combining the two approaches is often the right answer.
Make an image fluid with CSS
For an image that should scale with its containing block while keeping its original proportions, use width: 100% and height: auto:
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →#1 Best Overall
- Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
- Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
- Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
- Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
- 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
<img class="fluid" src="photo.jpg" alt="A kayaker crossing a mountain lake" width="1200" height="800">
.fluid {
display: block;
width: 100%;
height: auto;
max-width: 100%;
}
The image will be no wider than its container, and its height will follow its intrinsic aspect ratio. The HTML width and height values describe the source dimensions; they help the browser reserve the right amount of space before the image loads. Use the actual dimensions or the same aspect ratio as the source, not arbitrary values.
This changes the rendered size, not the source file. If a 3,000-pixel-wide photo is displayed at 400 pixels, the browser may still download that large photo. Offer multiple source widths with srcset and sizes if reducing the download is also a goal.
Let the browser choose a source with srcset and sizes
Use width descriptors such as 480w when an image’s rendered slot can change with the layout. Each descriptor must state the candidate file’s real intrinsic width. The sizes attribute describes the expected display width of the slot under specified conditions; the browser uses that information, along with factors such as device pixel ratio, to select a candidate.
<img
src="photo-800.jpg"
srcset="photo-480.jpg 480w,
photo-800.jpg 800w,
photo-1200.jpg 1200w"
sizes="(width <= 600px) 100vw,
(width <= 1000px) 80vw,
1200px"
alt="A kayaker crossing a mountain lake"
width="1200"
height="800">
How to set sizes
Read the conditions from top to bottom. If the viewport width is at most 600 pixels, the image slot is expected to be as wide as the viewport. If the first condition does not match but the viewport is at most 1,000 pixels, the slot is expected to use 80% of the viewport width. In other cases, the slot is expected to be 1,200 pixels wide.
Those values must reflect the actual layout. If the image sits inside a column that is only half the viewport width, describing it as 100vw can lead the browser to choose a larger file than necessary. The final value, which has no media condition, describes the usual slot when none of the preceding conditions match.
Rank #2
- 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
- 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
- Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
- 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
- What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
How the browser chooses
srcset provides candidates; it does not order the browser to use a particular file at a particular viewport. The browser weighs the described slot size and device conditions. There may not be an exact match: the browser can choose a larger candidate and scale it down. A candidate that is too small may look soft when displayed at a larger size or on a high-density screen.
The src attribute remains useful as a fallback. Keep it pointing to a valid image and make sure each candidate URL works. Avoid declaring a file as, for example, 1200w if its actual intrinsic width is not 1,200 pixels.
Use density descriptors for a fixed display size
When the CSS display size is fixed and only the desired pixel density varies, use x descriptors rather than width descriptors. For example:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
<img
src="logo.png"
srcset="logo.png 1x, logo-2x.png 2x"
width="200"
height="80"
alt="Acme logo">
This indicates that the 200-by-80 display-size image has a 2× alternative for a higher-density display. Use width descriptors and sizes for a slot that changes across layouts; use density descriptors when the CSS display size stays fixed. Do not combine width and density descriptors in the same candidate list.
Fit or crop an image inside a fixed box
If a card or thumbnail needs a consistent shape, give the image a box and decide whether to preserve all of the image or fill the box by cropping. object-fit controls that choice; object-position controls which part remains in view.
Rank #3
- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
.card-image {
display: block;
width: 100%;
aspect-ratio: 4 / 3;
object-fit: cover;
object-position: 50% 40%;
}
object-fit: coverfills the box while preserving the image’s proportions. Parts outside the box may be cropped.object-fit: containkeeps the entire image visible. Depending on the source and box proportions, empty space may remain.object-position: 50% 40%adjusts the focal alignment; change the values to keep a face or other important subject in view.
The aspect-ratio rule establishes the shape of the rendered box. If the original image has a different ratio, cover crops it and contain leaves unused space. Do not use cropping where the whole image must remain visible, such as a diagram with labels near its edges.
Use picture for different crops or image formats
Use <picture> when a narrow layout needs a different composition, or when you want to offer formats such as AVIF and WebP with a conventional image fallback. The nested <img> supplies the fallback image, alternative text, and dimensions.
Crashes, 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 minutePC 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 & 11<picture>
<source
media="(width <= 600px)"
srcset="portrait-480.jpg 480w, portrait-800.jpg 800w"
sizes="100vw">
<source
type="image/avif"
srcset="photo-800.avif 800w, photo-1200.avif 1200w"
sizes="(width <= 1000px) 80vw, 1200px">
<source
type="image/webp"
srcset="photo-800.webp 800w, photo-1200.webp 1200w"
sizes="(width <= 1000px) 80vw, 1200px">
<img
src="photo-800.jpg"
alt="A kayaker crossing a mountain lake"
width="1200"
height="800">
</picture>
The browser evaluates the source conditions and uses a matching supported source. Put the art-direction source first when its media condition should take priority, then provide format alternatives. The fallback img should be a complete, usable image, not an empty placeholder.
Art direction and responsive sizing solve different problems. Use a different crop when the composition should change on smaller screens; use multiple widths of the same composition when only the file size needs to adapt. You can use both where appropriate.
Choose the right approach
| Need | Use | What changes |
|---|---|---|
| Scale with a flexible column while preserving the image proportions | CSS width and automatic height | Rendered dimensions; not necessarily the downloaded file |
| Keep a consistent card shape and fill its box | aspect-ratio, object-fit: cover, and optionally object-position |
Rendered box and visible crop |
| Keep the full image visible inside a fixed box | object-fit: contain |
Fit within the box, potentially leaving empty space |
| Offer appropriately sized files for a changing layout | srcset with width descriptors and sizes |
Browser-selected source file |
| Offer a higher-density file at a fixed display size | srcset with density descriptors |
Browser-selected density candidate |
| Change the crop by layout or offer format alternatives | picture with ordered source elements and an img fallback |
Source composition or supported format |
Build and check responsive image markup
- Measure the layout slot. Determine how wide the image actually renders at common viewport sizes, including the width of its containing column.
- Create real image candidates. Export several widths that bracket the slots your layout uses. Confirm each file’s intrinsic dimensions before writing its
wdescriptor. - Write the display rules. Use fluid width and automatic height for a proportional image, or set a deliberate aspect ratio and fitting behavior for a crop.
- Describe the slot in sizes. Match the conditions to the layout and give an accurate unconditional value for the common wider-screen slot.
- Add meaningful alternative text and dimensions. Describe the image’s informative content in
alt; usealt=""for an image that is purely decorative and adds no information. Set correct intrinsicwidthandheightattributes or an equivalent aspect-ratio constraint. - Test real breakpoints and densities. Inspect the image at narrow and wide viewports and on a high-density display. Use browser network tools to see which candidate loaded and whether its dimensions make sense for the slot.
There is no universal bandwidth-saving percentage for this technique. Results depend on the original and candidate dimensions, encoding, layout slot, device pixel ratio, and network conditions.
Rank #4
- Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
- Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
- Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
- Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
- Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
Why not swap the source with JavaScript?
A late JavaScript source swap is usually a poor default for responsive images. The browser may start loading the original source before the script runs, then have to request a different file after the script changes it. Declarative srcset and sizes let the browser make its selection as it processes the markup, avoiding that avoidable delay and duplicate request risk.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Use JavaScript only when application-specific behavior cannot be expressed with CSS, srcset, sizes, or picture. If JavaScript does change an image source, make the behavior intentional and test the network requests at relevant viewport sizes rather than assuming the initial source will never be fetched.
Troubleshoot common problems
The image looks blurry
Check whether the selected source is large enough for the displayed slot and device density. Make sure the w descriptors match real file widths and that your candidate set includes a suitable larger option. A smaller source may be selected when sizes understates the actual slot.
The page downloads an unexpectedly large image
Compare the actual layout width with sizes. If the slot is a narrow column, do not describe it as the full viewport width. Also check whether the source candidates are much larger than the slots they are intended to serve.
The image stretches or has the wrong shape
For a proportional image, use height: auto and avoid setting unrelated width and height values in CSS. For a fixed-shape box, use object-fit to choose between crop and containment and set object-position if the subject is misaligned.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsBest Value
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
The crop cuts off an important subject
With cover, cropping is expected if the box and source have different aspect ratios. Adjust object-position, use a less aggressive box ratio, or supply a different crop in a media-conditioned picture source.
A candidate or format does not appear
Check that each URL loads, its file has the format claimed by the source’s type, and the media condition matches the viewport. Retain the nested img fallback so there is a valid image when a source is unsupported or unmatched.
The page jumps when the image loads
Provide correct intrinsic dimensions on the img or define a suitable aspect-ratio for its box. If the reserved ratio differs from the image or the final crop, layout can still shift when the content appears.
Or skip the browser setup
ScreenshotNeo is a website screenshot API, not an image-resizing service: it captures a web page as PNG, JPEG, WebP, or PDF. If what you need is a rendered page image rather than a resized source asset, a single request can capture it:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
See the ScreenshotNeo API documentation for request options. Before capture, it accepts cookie or consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and response headers identify the page verdict and billing status. Its MCP server provides take_screenshot, get_page_info, and capture_pdf for AI agents and MCP clients. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots. Learn more at ScreenshotNeo, or sign up free for 1,000 screenshots a month with no card.
Frequently asked questions
Should I use width descriptors or 2x descriptors?
Use width descriptors such as 800w with sizes when the rendered slot changes with the layout. Use density descriptors such as 2x when the CSS display size is fixed and you are offering a higher-density alternative.
Does CSS make an image file smaller?
No. CSS controls the rendered dimensions and fit. To give the browser a choice of differently sized files, provide candidates with srcset and describe their expected slot with sizes.
Do I need picture for every responsive image?
No. Use img with srcset and sizes for alternate widths of the same composition. Use picture when you need art direction or format-specific sources with an image fallback.
Recommended Free Tools
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.

