HTML HTML Tutorial HTML Forms HTML Graphics HTML Media HTML APIs HTML Tags



HTML Images

Images are an essential part of any website. They help to make the website more visually appealing and engaging. HTML provides a way to add images to a web page using the <img> tag. In this tutorial, we will learn how to add images to a web page using HTML.

The <img> Tag

The <img> tag is used to add images to a web page. It is an empty tag, which means it does not have a closing tag. The <img> tag has two required attributes: src and alt.

  • The src attribute specifies the URL of the image.
  • The alt attribute specifies an alternate text for the image, which is displayed if the image cannot be loaded.

Here is an example of the <img> tag:

<img src="image.jpg" alt="A beautiful sunset">

In the above example, the image.jpg file is located in the same directory as the HTML file. If the image file is located in a different directory, you need to specify the path to the file in the src attribute.

Image Formats

There are several image formats that can be used on the web. The most common image formats are JPEG, PNG, and GIF.

  • JPEG (Joint Photographic Experts Group) is a compressed image format that is best suited for photographs and complex images.
  • PNG (Portable Network Graphics) is a lossless image format that is best suited for images with transparent backgrounds.
  • GIF (Graphics Interchange Format) is a compressed image format that is best suited for simple images with few colors.

Image Size and Alignment

You can specify the size of an image using the width and height attributes of the <img> tag. The values of these attributes are specified in pixels.

You can also align an image using the align attribute of the <img> tag. The align attribute can have one of the following values:

  • left - aligns the image to the left of the page
  • right - aligns the image to the right of the page
  • center - centers the image on the page

Here is an example of an image with a width of 500 pixels, a height of 300 pixels, and aligned to the right:

<img src="image.jpg" alt="A beautiful sunset" width="500" height="300" align="right">

Image Links

You can make an image clickable by wrapping it in an <a> tag. Here is an example:

<a href="https://www.example.com">
  <img src="image.jpg" alt="A beautiful sunset">
</a>

In the above example, the image is wrapped in an <a> tag with a href attribute that specifies the URL to link to. When the user clicks on the image, they will be taken to the specified URL.

Conclusion

Images are an important part of any website. HTML provides a simple way to add images to a web page using the <img> tag. By specifying the src and alt attributes, you can add images to your web page that are both visually appealing and accessible to all users.

References

Activity