- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- HTML Images
HTML Images and Media
HTML Images
Images go on a page with the img tag: src says which file, alt says what it shows. Add width and height too, so the page does not jump while the image loads. Those four attributes cover almost every image you will ever add.
Every attribute is described in detail in the img tag. This lesson walks through the everyday cases.
The <img> Tag
Basic Image Tag
html
<img src="image.jpg" alt="Description of the image">srcis the image file: a path likeimages/photo.jpgor a full address.altis the text that replaces the image for screen readers, and when the image cannot load. Describe what the image shows, or usealt=""if it is purely decorative.
Adding Image Width and Height
Image Example
html
<img src="image.jpg" alt="A sample image" width="300" height="200">These numbers are in pixels and written without units. They do not stop you from resizing the image with CSS; they tell the browser the image's shape before it arrives, so it can reserve the space. Without them, text jumps down when the image loads. See responsive design for making images fit any screen.
Image from a Folder
A path without a full address is looked up relative to the page:
Image from Folder
html
<img src="images/picture.jpg" alt="Picture inside folder">images/picture.jpg means "in the images folder next to this page". ../picture.jpg goes up one folder. /images/picture.jpg, with a leading slash, starts from the root of the site. Wrong paths are the most common reason an image does not show.
If the image is hosted online, you can use an absolute URL:
Image from Online
html
<img src="https://example.com/photo.jpg" alt="Online image">Image as a Link
Image as a Link
html
<a href="https://html5andcss3.org">
<img src="/assets/images//assets/images/html5_and_css3_logo.png" alt="HTML5 & CSS3 Logo" width="150">
</a>The whole image becomes a link. Its alt text is read as the link's text, so describe where the link goes.
Image Title Attribute
Image Title Example
html
<img src="flower.jpg" alt="Rose" title="A beautiful rose">The tooltip only appears on hover with a mouse. Do not put information there that is not also elsewhere.
Responsive Images with CSS
Responsive Image Example
html
<img src="mountain.jpg" alt="Mountain view" style="width:100%; height:auto;">width: 100%; height: auto; makes the image fill its container and keep its proportions. In a stylesheet, img { max-width: 100%; height: auto; } is the safer version: images shrink on small screens but never grow beyond their real size.
Image Alignment (Modern Way)
Image Alignment Example
html
<img src="/assets/images//assets/images/html5_and_css3_logo.png" alt="Photo" style="display:block; margin:auto;">display: block together with margin: auto centers the image. The old align attribute is obsolete.
Example: Complete Image Code
Complete Image Example
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML Image Example</title>
</head>
<body>
<h2>Example of an Image in HTML</h2>
<a href="https://html5andcss3.org">
<img src="/assets/images//assets/images/html5_and_css3_logo.png" alt="HTML5 Logo" width="200" style="display:block; margin:auto;">
</a>
<p>This is an example of how to add and center an image in HTML.</p>
</body>
</html>Good to know
- Compress images and use modern formats like WebP or AVIF. Large images are the most common reason pages load slowly.
- Add
loading="lazy"to images further down the page, but not to the main image at the top. - For different crops or formats on different screens, see the picture element.
Browser Support
Chrome | Edge | Firefox | Safari | Opera | IE9+ |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
HTML Images exercises
Create an accessible image figure
Improve the image by adding the exact alternative text “HTML and CSS workspace”. Then place it inside a figure and add a figcaption.
Result
