Images are a key component of web design and help make content more engaging. In HTML, the <img>
tag is used to insert images into a webpage. The <img>
tag is an empty element, meaning it does not have a closing tag. This article will cover how to use the <img>
tag, along with attributes that help control the display of images.
The <img>
tag requires at least one attribute, src
, which specifies the path to the image file. Another important attribute is alt
, which provides alternative text for the image. The basic syntax for the <img>
tag is:
<img src="image.jpg" alt="Description of the image">
Here is an example of adding a simple image to a webpage:
<img src="https://example.com/path/to/image.jpg" alt="Example Image">
In this example, src
specifies the URL of the image, and alt
provides a description that will appear if the image cannot be displayed.
The <img>
tag has several attributes that control how the image is displayed:
You can set the width and height of an image using the width
and height
attributes:
<img src="https://example.com/path/to/image.jpg" alt="Example Image" width="300" height="200">
In this example, the image will be displayed at 300 pixels wide and 200 pixels tall.
The title
attribute adds a tooltip that appears when the user hovers over the image:
<img src="https://example.com/path/to/image.jpg" alt="Example Image" title="Hover text for the image">
In this case, when the user hovers over the image, they will see the tooltip "Hover text for the image."
The src
attribute can point to a local image file or an external URL.
<img src="images/photo.jpg" alt="Local Photo">
This example assumes the image file photo.jpg
is located in an "images" folder in the same directory as the HTML file.
<img src="https://example.com/image.jpg" alt="External Image">
This example uses an external URL to load an image from another website.
The <img>
tag in HTML is a simple yet powerful tool for adding images to a webpage. By using attributes like src
, alt
, width
, height
, and title
, you can control how images are displayed and improve the accessibility of your website. Images are an essential part of web design, and understanding how to use the <img>
tag will help enhance the visual appeal of your webpages.