Images need to fit well on your webpage. Sizing helps control how big or small images appear so your page looks nice and loads well.
Image sizing basics in HTML
<img src="image.jpg" alt="Description" width="100" height="50">
The width and height attributes set the image size in pixels.
You can also use CSS to control image size for more flexibility.
<img src="flower.jpg" alt="Red flower" width="200">
<img src="cat.png" alt="Cute cat" height="150">
<img src="dog.jpg" alt="Happy dog" width="300" height="200">
<style>img { max-width: 100%; height: auto; }</style> <img src="landscape.jpg" alt="Beautiful landscape">
This page shows three images sized differently. The first uses the width attribute, the second uses height, and the third uses CSS to fit the container width while keeping shape.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Image Sizing Example</title> <style> body { font-family: Arial, sans-serif; padding: 1rem; } .container { max-width: 400px; border: 1px solid #ccc; padding: 1rem; } img { max-width: 100%; height: auto; display: block; margin-bottom: 1rem; border-radius: 0.5rem; } </style> </head> <body> <main class="container"> <h1>Image Sizing Basics</h1> <p>Below are three images with different size settings:</p> <img src="https://via.placeholder.com/600x400" alt="Large placeholder image" width="300"> <img src="https://via.placeholder.com/600x400" alt="Medium placeholder image" height="150"> <img src="https://via.placeholder.com/600x400" alt="Full width placeholder image"> <p>The first image is set to 300 pixels wide.</p> <p>The second image is set to 150 pixels tall.</p> <p>The third image uses CSS to fit the container width.</p> </main> </body> </html>
Using only width or height keeps the image's natural shape.
Setting both width and height can stretch or squash the image if proportions don't match.
CSS sizing is better for responsive designs that work on phones and desktops.
Use width or height attributes to size images simply.
Use CSS for flexible, responsive image sizing.
Keep image proportions to avoid distortion.
Practice
Solution
Step 1: Identify image sizing attributes
Thewidthattribute is used in the<img>tag to set the image width in pixels or percentage.Step 2: Confirm correct attribute name
Among the options, onlywidthis a valid HTML attribute for image sizing.Final Answer:
width -> Option AQuick Check:
Image width attribute = width [OK]
width or height attributes for quick sizing [OK]- Confusing
sizewithwidth - Using CSS property names as HTML attributes
- Trying to use
scalewhich is not valid
Solution
Step 1: Understand HTML attribute value format
In HTML, thewidthattribute expects a number representing pixels without units.Step 2: Check each option
<img src='photo.jpg' width='200px'>uses '200px' which is invalid for thewidthattribute.<img src='photo.jpg' width='200'>correctly uses '200' without units.<img src='photo.jpg' style='width: 200'>uses CSS syntax incorrectly inside an attribute.<img src='photo.jpg' height='200px'>sets height, not width.Final Answer:
<img src='photo.jpg' width='200'> -> Option CQuick Check:
Width attribute value is number only [OK]
- Adding 'px' units inside width or height attributes
- Confusing CSS style syntax with HTML attributes
- Setting height instead of width by mistake
<img src='flower.jpg' width='150' height='100'>
Solution
Step 1: Understand width and height attributes together
When bothwidthandheightare set, the image is forced to those exact dimensions, ignoring original proportions.Step 2: Predict visual effect
Because the original image ratio may differ, forcing both width and height can distort the image shape.Final Answer:
Image shown with width 150px and height 100px, possibly distorted -> Option BQuick Check:
Both width and height set = fixed size, possible distortion [OK]
- Assuming height auto adjusts when both attributes set
- Thinking image keeps original ratio automatically
- Believing invalid attributes cause image not to show
<img src='cat.png' width='300' height='auto'>
Solution
Step 1: Check width attribute value
Thewidthattribute correctly uses the number '300' without units.Step 2: Check height attribute value
Height attribute does not accept 'auto' as a value in HTML attributes; it only accepts numbers.Final Answer:
Using 'auto' in height attribute is invalid in HTML -> Option AQuick Check:
Height attribute must be number, not 'auto' [OK]
- Using CSS values like 'auto' in HTML attributes
- Thinking
<img>needs closing slash in HTML5 - Thinking width attribute requires 'px' units
Solution
Step 1: Understand responsive image sizing
Settingwidth: 100%makes the image fill the container's width.Step 2: Maintain aspect ratio
Settingheight: autolets the browser calculate height to keep the image's original proportions.Final Answer:
width: 100%; height: auto; -> Option DQuick Check:
Width 100% + height auto = responsive proportional image [OK]
- Setting fixed height breaks aspect ratio
- Using auto width with fixed height distorts image
- Setting both width and height fixed loses responsiveness
Ready to prove it?
Pass the test and this pattern moves unknown
