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

To change an image’s displayed size, set its width and height in HTML or CSS. To resize it responsively without distortion, use max-width: 100%; height: auto;. If it must fill a box with a different shape, use CSS object-fit to control cropping or empty space.

Choose the sizing behavior you need

First decide whether the image should have fixed dimensions, shrink with its container, or fill a box whose aspect ratio differs from the image. These are layout choices: changing the displayed size does not by itself select a smaller image file for download.

Goal Use Result
Set a particular display size HTML width and height attributes, or CSS dimensions A fixed rendered size, subject to any overriding CSS and container constraints.
Resize without changing proportions One dimension plus auto for the other The image keeps its aspect ratio.
Fit a responsive layout max-width: 100%; height: auto; The image can shrink to fit its container and retains its proportions.
Fill a box of a different shape Set box dimensions and choose object-fit The image can crop, leave space, or stretch, depending on the value.
Choose among alternate image files srcset and usually sizes The browser selects a source candidate; CSS still controls the rendered layout.

Set an image to a fixed size

Use HTML dimension attributes

For a straightforward image, put width and height on the <img> element. Their values are unitless integer pixel dimensions: write width="320", not width="320px".

<img src="photo.jpg" alt="A mountain lake" width="800" height="600">

The dimensions should describe the image’s intrinsic aspect ratio when known. Supplying them lets the browser calculate the image’s shape and reserve appropriate space before the file finishes loading, which can reduce layout shifts. They do not change the source file’s actual pixel dimensions.

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.

Use CSS for presentation rules

CSS is useful when you want consistent styling across images or need dimensions that change at different screen sizes. For example:

.profile-photo {
  width: 160px;
  height: 160px;
}

Those dimensions create a square box. If the source image is not square, choose an object-fit behavior as described below; otherwise the browser’s default fill behavior can distort it. HTML attributes provide intrinsic dimensions, while CSS can set the rendered dimensions. A CSS rule may override the rendered size suggested by the attributes.

Resize an image without stretching it

When changing only one dimension, set the other to auto. The browser then preserves the image’s proportions:

.article-image {
  width: 300px;
  height: auto;
}

This makes the image 300 CSS pixels wide while calculating its height from its aspect ratio. The reverse works too: use a fixed height and width: auto if the height is what matters.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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

Avoid giving an image incompatible width and height values unless distortion is intended. For instance, forcing a wide landscape photo into a narrow, tall box without an appropriate fit rule can make people and objects look unnaturally thin.

Make an image responsive to its container

For a common responsive pattern, let the image use its natural width until it reaches the available space, and preserve its ratio as it shrinks:

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

max-width: 100% prevents an image from overflowing a narrower parent; it does not necessarily enlarge a small source to fill a wider parent. height: auto keeps the proportions as the rendered width changes.

Apply the rule selectively if some images need different behavior. A global selector such as img is convenient for a simple page, but a class such as .article-image avoids affecting icons, avatars, or other images with distinct layout requirements.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<img class="article-image" src="photo.jpg" alt="A mountain lake" width="1600" height="1200">
.article-image {
  display: block;
  max-width: 100%;
  height: auto;
}

Here the HTML dimensions describe the source’s aspect ratio, and the CSS lets the displayed image shrink to fit the available width. Keep the attribute values accurate to the image when possible.

Fit an image into a fixed box with object-fit

Use object-fit when the image element has a set width and height but the source and box have different proportions. The property controls how the image content fits inside the element’s box.

Value What it does Use when
cover Preserves proportions while filling the box; excess parts are clipped. You need a full box, such as a consistently sized card thumbnail, and cropping is acceptable.
contain Preserves the whole image inside the box; unused space may remain. The entire image must be visible, such as a product illustration.
fill Fills the box and may stretch the image if proportions differ. Distortion is acceptable or the source and box already have matching proportions.
none Displays the image at its natural size within the element’s content area; parts may be clipped if they do not fit. You want the image’s natural sizing rather than automatic scaling.
scale-down Uses the smaller outcome of none and contain. You want the image to remain natural-sized when it fits, but shrink when necessary.

Example: crop photos to consistent landscape thumbnails without stretching them.

.thumbnail {
  width: 320px;
  height: 180px;
  object-fit: cover;
}

With cover, some of the source image is outside the box. Adjust which part remains visible with object-position:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
.thumbnail {
  width: 320px;
  height: 180px;
  object-fit: cover;
  object-position: center top;
}

Use contain instead if clipping would hide important content:

.product-image {
  width: 320px;
  height: 180px;
  object-fit: contain;
}

object-fit is widely available across browsers; MDN describes it as available across browsers since January 2020. See MDN’s object-fit reference and its guide to fitting an image into a box without distortion.

Use srcset and sizes to offer image-file choices

CSS sizing determines the displayed layout. If you have multiple versions of an image and want the browser to choose a suitable file, use srcset and sizes. This is a separate decision from the image element’s rendered width.

<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="1200"
  alt="A mountain lake"
>

In this example, the 400w, 800w, and 1600w descriptors state the candidate files’ widths. The sizes value describes the likely rendered slot: full viewport width at up to 600 CSS pixels, and 800 CSS pixels above that. The browser uses these hints when selecting a resource. You still use CSS, for example max-width: 100%; height: auto;, to make the layout fit its container. For the responsive image syntax and candidate descriptors, see MDN’s guide to responsive images.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use accurate dimensions to reduce layout shifts

When an image has known intrinsic dimensions, include matching width and height attributes even if CSS later makes it fluid. The browser can use their ratio to reserve space before the image arrives. This helps keep following text and page elements from jumping when the image loads. MDN explains this dimension-based aspect-ratio calculation in its images and multimedia reference and image guide.

Do not put guessed or unrelated dimensions in the attributes. If you give dimensions that imply a different ratio from the source, the browser may reserve the wrong shape before load, and a fixed CSS height can visibly distort the final result unless an object-fit mode is chosen.

Common problems and fixes

  • The image looks stretched: One or both dimensions are forcing a different aspect ratio. Set one dimension to auto, or use object-fit: cover or contain when a fixed box is required.
  • The image overflows on a phone: Add max-width: 100%; height: auto; to the relevant image or class. Check that the image is actually inside the expected container and that a more specific CSS rule is not overriding the sizing.
  • The whole image is not visible: Check whether object-fit: cover is cropping it. Change to contain if the full image matters, or adjust object-position to keep the important area in view.
  • There is empty space around the image: With contain, unused space is expected when the source and box have different proportions. Use cover if filling the box matters more than showing every edge.
  • The image’s aspect ratio changes after loading: Verify that the HTML width and height describe the source file accurately, and inspect CSS for a forced height or width that conflicts with the ratio.
  • The browser downloads a file that seems too large: Display sizing does not select a smaller source. Provide appropriately sized candidates with srcset and describe the rendered slot with sizes.
  • Changing the HTML attributes has no visible effect: CSS may set the rendered dimensions or apply a maximum size. Inspect the matching element’s computed styles and adjust the CSS rule responsible for the result.

Or skip the browser setup

If you need a screenshot of a page after changing its image layout, ScreenshotNeo can return an image or PDF from one GET request. It removes cookie and consent banners, 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 cost nothing, and the response identifies the page verdict and billing status. Its MCP server provides screenshot tools for AI agents, including Claude, Cursor, and other MCP clients.

Install Python’s requests package if needed (python -m pip install requests), set your API key, and run this complete example. Replace the URL with the page you want to capture. The API options are documented at ScreenshotNeo docs.

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

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

The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Sign up for free ScreenshotNeo screenshots.

Frequently Asked Questions

Do HTML image dimensions use pixels?

Yes. The width and height attributes take unitless integer pixel values, such as width="400".

Can I make an image responsive without enlarging a small source?

Yes. Use max-width: 100%; height: auto;. The maximum width lets it shrink to fit; it does not force it to grow beyond its natural size.

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.