Adding Images
Use the img element to show an image on your page.
The Image Source and Alternative Text
The src attribute tells the browser where the image file is saved.
Save images in the same folder as your html file. Or create a sub-folder within your project folder.
The example below shows a src value that points to a file named planet-earth.jpg that is stored in a sub-folder named images.
The alt attribute describes the image for people who cannot see it or for when it does not load.
The img element does not need a closing tag.
<img src="images/planet-earth.jpg" alt="Earth seen from space">
Troubleshooting
If your image does not display:
- Check
srcto confirm that you spelled the file name correctly (especially.jpgor.jpeg). - Confirm that
srcpoints to the correct location of your image. - Confirm that your file extension is
.jpg,.jpeg, or.png, and not.webpor.aviv.
Sizing Images
Use the style attribute to set an image's width. Setting the height to auto keeps the image's proportions.
<img src="images/planet-earth.jpg"
alt="Earth seen from space" style="width: 300px; height: auto;">
Centering Images
To center an image, make it a block and set its left and right margins to auto.
<img src="images/planet-earth.jpg"
alt="Earth seen from space"
style="display: block; margin: 0 auto;">
Note: These examples use inline styling: CSS declarations written in an HTML element's style attribute. You could accomplish the same effect by using CSS in a separate stylesheet.