The three ways to resize images in HTML
You can change how large or small a picture appears on a webpage using the width and height attributes inside the image tag itself, by writing CSS rules in a style sheet, or by editing the actual image file before you upload it. The first two methods are what most people use because they let you resize without changing the original file, and they work instantly when you reload the page.
The simplest approach is to add width and height numbers directly to your <img> tag. This tells the browser exactly how many pixels wide and tall to display the picture. If you only set one dimension, the browser automatically shrinks or stretches the other one to keep the image from looking squashed or stretched.
The second method uses CSS, which gives you more control and keeps your HTML cleaner if you have many images. You write rules that apply the same size to multiple pictures at once, or change sizes based on screen width so mobile phones see smaller versions than desktop computers.
Key Takeaways
- Add width and height attributes directly to the <img> tag to resize a single picture: <img src="photo.jpg" width="300" height="200">
- Set only width or only height and the browser will automatically scale the other dimension to prevent distortion.
- Use CSS in a style sheet to resize multiple images at once or apply different sizes on mobile versus desktop screens.
- Always keep a copy of your original image file because resizing in HTML does not change the file itself, only how it displays.
Resizing with width and height attributes
Open the HTML file where your image tag lives. Find the line that starts with <img src= and locate the closing angle bracket at the end. Just before that bracket, add a space and then type width="300" (replace 300 with however many pixels wide you want the image). Then add another space and type height="200" (replace 200 with the height you want).
Here is what it looks like in practice. If your original tag says <img src="vacation.jpg">, you change it to <img src="vacation.jpg" width="400" height="300">. Save the file and reload the page in your browser. The picture will now display at 400 pixels wide and 300 pixels tall.
If you only want to set the width and let the height adjust automatically, you can do that. Write <img src="vacation.jpg" width="400"> and the browser will make the height smaller or larger in the same proportion, so the picture does not look stretched. This is the safest approach if you are not sure what the final size should be.
Using CSS to resize images
CSS lets you write one rule that resizes all images of a certain type, or all images with a certain class name. This is faster than editing each image tag individually, especially if you have dozens of pictures on your site.
Open your CSS file (or the <style> section inside your HTML file's <head>). To resize all images on the page, add these lines:
img { width: 300px; height: 200px; }
Save and reload. Every image will now display at 300 by 200 pixels. If you want different sizes for different images, give each image a class name. In your HTML, write <img src="photo1.jpg" class="thumbnail"> and <img src="photo2.jpg" class="fullwidth">. Then in your CSS, write:
.thumbnail { width: 150px; height: 150px; } .fullwidth { width: 600px; height: 400px; }
Now the first image displays at 150 by 150 pixels and the second at 600 by 400 pixels, even though they use the same <img> tag structure.
Making images responsive for different screen sizes
A responsive image changes size depending on whether someone is viewing your page on a phone, tablet, or desktop computer. You write CSS rules that say "on screens smaller than 600 pixels wide, make images 100 pixels; on screens larger than 600 pixels, make them 300 pixels."
In your CSS, use @media rules to set different sizes for different screen widths. Here is an example:
img { width: 100%; max-width: 600px; } @media (max-width: 768px) { img { max-width: 400px; } } @media (max-width: 480px) { img { max-width: 200px; } }
This code says: make images as wide as their container (100%), but never wider than 600 pixels. On tablets (768 pixels or smaller), cap them at 400 pixels. On phones (480 pixels or smaller), cap them at 200 pixels. The image shrinks smoothly as the screen gets smaller, so it always looks right.
Understanding pixels and file size
A pixel is a tiny dot on the screen. When you set width="300", you are telling the browser to display the image 300 dots wide. This number has nothing to do with the actual file size on your computer — a 300-pixel-wide image can be 50 kilobytes or 500 kilobytes depending on how the original file was saved.
Resizing in HTML does not make the file smaller or larger. If your original photo is 2 megabytes, it stays 2 megabytes even if you display it at 100 pixels wide. The browser still downloads the entire file, then shrinks it on the screen. To actually reduce file size, you need to edit the image in a photo editor like Photoshop, GIMP, or an online tool, then save it at a smaller width before you upload it to your website.
This matters for page speed. If you have a 5-megabyte photo but display it at 300 pixels wide, visitors still wait for all 5 megabytes to download. For best performance, resize your images in a photo editor before uploading, then use HTML width and height attributes to fine-tune the display size.
Common mistakes to avoid
The most common error is setting only width or height to a very different number than the original proportions. If your image is naturally 800 pixels wide and 600 pixels tall (a 4:3 ratio), and you set width="300" height="500", the picture will look stretched vertically. Always set both dimensions in the same proportion as the original, or set only one and let the browser calculate the other.
Another mistake is forgetting the units. In HTML attributes, you write width="300" (just the number). In CSS, you write width: 300px; (with "px" at the end). If you write width: 300 in CSS without the "px", it will not work.
Do not assume that resizing in HTML makes the page load faster. It does not. The browser still downloads the full original file. If page speed matters to you, resize the image file itself in a photo editor before uploading it to your website.
Frequently Asked Questions
What happens if I set width and height to different proportions than the original image?
The image will look stretched or squashed. If the original is a square but you set width="400" height="200", it will appear as a wide rectangle. To avoid this, either set only width or only height and let the browser scale the other dimension, or make sure your width and height numbers are in the same ratio as the original image.
Can I use percentages instead of pixels for image size?
Yes. In CSS, you can write width: 50%; to make an image half as wide as its container. In HTML attributes, percentages do not work — you must use pixel numbers. Percentages are useful for responsive design because the image automatically shrinks or grows with the screen size.
Does resizing an image in HTML reduce how much data visitors have to download?
No. Resizing in HTML only changes how the image displays on the screen. The browser still downloads the entire original file. To reduce download size, you must edit the image in a photo editor and save it at a smaller width before uploading it to your website.
What is the difference between width and max-width in CSS?
Width sets a fixed size — the image will always be that wide. Max-width sets a maximum size — the image can be smaller if the screen is narrow, but will never be wider than the max-width value. For responsive design, max-width is usually better because it adapts to different screen sizes.
Can I resize an image by changing the src attribute?
No. The src attribute only tells the browser which file to load. It does not affect size. You must use width and height attributes or CSS to change how large the image appears.