How to change image size in HTML
Resize an image with width and height, with CSS in pixels or percent, keep it from stretching, fit it to phone screens, crop it to a square or banner with object-fit, and make the file itself smaller.
Have the file already? Upload it and get a link anyone can open. Free plan, no card.
Get a link freeAn image shows at the size of the file unless you tell the browser otherwise. A photo from a phone is often 4000 pixels wide, so it spills off the page. You can change the size in two places: the width and height attributes on the <img> tag, or CSS. Both only change how big it looks; the file stays the same, which matters for loading speed (last section).
Width and height on the img tag
<img src="photo.jpg" alt="Our shop front" width="600" height="400">
The numbers are pixels, with no "px". Set both to the image's real proportions: if the file is 3000 by 2000, any pair with the same 3:2 ratio works, such as 600 by 400 or 300 by 200. With both set, the browser reserves the space before the image loads, so the text below does not jump.
Set only width="600" and the browser works out the height from the file, so the image keeps its shape. Set both to numbers with a different ratio and the image stretches or squashes.
Change the size with CSS
CSS wins over the attributes, so use it when you want one rule for many images. Put it in a <style> block in the <head>:
.photo { width: 300px; height: auto; }
<img src="photo.jpg" alt="Our shop front" class="photo">
height: auto keeps the proportions. For a size relative to the page or the box the image sits in, use a percentage: width: 50%; makes it half as wide as its container. Inline CSS works too for a single image: <img src="photo.jpg" alt="..." style="width: 300px; height: auto;">.
Make images fit phone screens
One rule stops every image on the page from running off a narrow screen:
img { max-width: 100%; height: auto; }
Images keep their normal size on a laptop and shrink to the screen width on a phone. Keep the width and height attributes on each <img> as well; with this rule the browser uses them for the shape and still reserves the right space.
If an image still overflows on a phone, a fixed width in CSS is overriding it, such as width: 800px on the image or its container. Swap it for max-width: 800px.
Crop to a fixed box without stretching
For a row of thumbnails, team photos or a banner, you often want every image the same size even though the files have different shapes. Set both sizes and add object-fit:
.thumb { width: 200px; height: 200px; object-fit: cover; }
cover fills the box and crops what does not fit, keeping the middle. object-fit: contain shows the whole image inside the box with empty space at the sides instead. To keep a face or a logo in view when cropping, move the focus point with object-position: top; or object-position: 30% 50%;.
For a round profile photo, add border-radius: 50%; to the same rule.
When the size will not change
- ✓The image is stretched or squashed: width and height have a different ratio from the file. Set height: auto, or add object-fit: cover.
- ✓The width attribute does nothing: a CSS rule sets the width, and CSS wins. Search the <style> block and any linked .css file for img or the image's class.
- ✓The image is blurry: it is shown larger than the file. A 400-pixel-wide file at width: 800px looks soft; use a bigger file.
- ✓The image is tiny inside a flex row: the container shrinks it. Add flex-shrink: 0 to the image, or give it a min-width.
- ✓width="50%" in the attribute behaves oddly: percentages belong in CSS. Use style="width: 50%;" instead.
Make the file smaller too
Changing the display size does not change the download. A 6 MB photo shown at 300 pixels still makes everyone download 6 MB, which is slow on a phone. Before you add a photo, export it at about twice the width you show it (600 pixels wide for a 300-pixel slot, for sharp screens) as JPEG or WebP. On a Mac, Preview does this under Tools > Adjust Size; on Windows, the Photos app has Resize image in its menu. Free web tools such as Squoosh work in any browser.
If the images are inside a page an AI tool made, ask it to add width and height attributes and the max-width rule to every image; Claude and ChatGPT will do that in one pass.
Share the page
Open the file in a browser and drag the window narrow to check the images on a phone-sized screen. Then share it: with FileShare Pro you upload the HTML file with its images from the dashboard, copy the link and send it. It opens in any browser and the person opening it needs no account. Upload a new version over the old one and the link stays the same. If an image does not show once shared, the path is the usual cause: see add an image in HTML. Sign up free to share your first page.
Frequently asked
Add width and height in pixels to the img tag, such as <img src="photo.jpg" width="600" height="400" alt="...">, or set width in CSS with height: auto to keep the proportions.
Set only the width and use height: auto in CSS. If the image must fill a fixed box, set both sizes and add object-fit: cover, which crops instead of stretching.
Add img { max-width: 100%; height: auto; } to your CSS. Images then shrink to fit narrow screens and keep their normal size on wider ones.
No. The browser still downloads the whole file. Export the image at the size you need, as JPEG or WebP, to make the file smaller.
Drop the file, copy the link — live in seconds, free plan included.
Start sharing free