Images can help improve the appearance of a web page.
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 instead 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:
The required src
attribute specifies the path (URL) to the image.
Note: When a web page loads; it is the browser, at that moment, that gets the image from a web sserver and inserts it into the page. Therefore, make sure that the image actually stays in the same spot in relation to the web page, otherwise your visitors will get a broken link icon. The broken link icon and the alt
text are shown if the browser cannont find the image.
An example image is shown below:
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.
An example is shown below:
If a browser cannot find an image, it will display the value of the alt
attribute instead
You can use the width
and height
attributes to change the width and height of an image.
The example below changes the width of an image to 200px and the height to 100px:
The width
and height
attributes always defines the width and height of the image in pixels.
If you have images in a sub-folder, you must include the folder name in the src
attribute as shown below:
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 as shown below:
Note: External images might be under copyright. If you do not get permission to use it, you may be in violation of copyright laws. In addition, you cannot control external images, it could be suddenly changed or removed.
Here are the most common image file types, which are supported in all browsers (Chrome, Edge, Firefox, Safari and 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 |