HTML Images
Images can improve the design and the appearance of a web page.
Example
|
1 2 3 4 5 6 7 8 9 |
<!DOCTYPE html> <html> <body> <h3>HTML Image</h3> <img src="pic_trulli.jpg" alt="Trulli" width="500" height="333"> </body> </html> |
Result
HTML Image

HTML Images Syntax
The HTML <img> tag is used to embed an image in a web page.
Images are not technically inserted into a web page; images are linked to web pages. The <img> tag creates a holding space for the referenced image.
The <img> tag is empty, it contains attributes only, and does not have a closing tag.
The <img> tag has two required attributes:
- src – Specifies the path to the image
- alt – Specifies an alternate text for the image
Syntax
|
1 |
<img src="url" alt="alternatetext"> |
The src Attribute
The required src attribute specifies the path (URL) to the image.
Example
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <body> <h3>Alternative text</h3> <p>The alt attribute should reflect the image content, so users who cannot see the image get an understanding of what the image contains:</p> <img src="img_chania.jpg" alt="Flowers in Chania" width="460" height="345"> </body> </html> |
Result
Alternative text
The alt attribute should reflect the image content, so users who cannot see the image get an understanding of what the image contains:

The alt Attribute
The required alt attribute provides an alternate text for an image,
if the user for some reason cannot view it (because of slow connection, an error in the src attribute, or if the user uses a screen reader).
The value of the alt attribute should describe the image:
Example
|
1 2 3 4 5 6 7 8 9 10 |
<!DOCTYPE html> <html> <body> <p>If a browser cannot find the image, it will display the alternate text:</p> <img src="wrongname.gif" alt="Flowers in Chania"> </body> </html> |
Result
If a browser cannot find the image, it will display the alternate text:

Images in Another Folder
If you have your images in a sub-folder, you must include the folder name in the src attribute:
Example
|
1 2 3 4 5 6 7 8 9 10 11 |
<!DOCTYPE html> <html> <body> <h3>Images in Another Folder</h3> <p>It is common to store images in a sub-folder. You must then include the folder name in the src attribute:</p> <img src="/images/html5.gif" alt="HTML5 Icon" style="width:128px;height:128px;"> </body> </html> |
Result
Images in Another Folder
It is common to store images in a sub-folder. You must then include the folder name in the src attribute:

Images on Another Server/Website
Some web sites point to an image on another server.
To point to an image on another server, you must specify an absolute (full) URL in the src attribute:
Example
|
1 2 3 4 5 6 7 8 9 10 |
<!DOCTYPE html> <html> <body> <h3>Images on Another Server</h3> <img src="https://yt3.googleusercontent.com/O4xRQKtnk0uye2nOJgaaI"alt="codenbytes.com" style="width:104px;height:142px;"> </body> </html> |
Result
Images on Another Server
Common Image Formats
Here are the most common image file types, which are supported in all browsers (Chrome, Edge, Firefox, Safari, Opera):
| Abbreviation | File Format | File Extension |
|---|---|---|
| APNG | Animated Portable Network Graphics | .apng |
| GIF | Graphics Interchange Format | .gif |
| ICO | Microsoft Icon | .ico, .cur |
| JPEG | Joint Photographic Expert Group image | .jpg, .jpeg, .jfif, .pjpeg, .pjp |
| PNG | Portable Network Graphics | .png |
| SVG | Scalable Vector Graphics | .svg |