HTML Images: Embedding Pictures in Your Web Pages

HTML Images: Embedding Pictures in Your Web Pages

Continuing from our previous blog, "Creating Links in HTML: Hyperlinks Made Easy," we now move on to enhancing your web pages with images. Embedding images is a key aspect of web development that can make your content more engaging and visually appealing.

The Basics of Adding Images to HTML

To add images to your HTML documents, you use the <img> tag. This tag is an empty element, meaning it does not have a closing tag, and it is used to embed images in your web pages. The <img> tag requires two primary attributes: src and alt.

  1. src (Source): The src attribute specifies the path to the image you want to display. This path can be an absolute URL (linking to an image on a different website) or a relative URL (linking to an image within your project directory).
<img src="images/sample.jpg" alt="Sample Image">
  • In this example, the src attribute points to an image file named sample.jpg located in an images folder relative to the current HTML file.
  • alt (Alternative Text): The alt attribute provides alternative text for the image, which is displayed if the image cannot be loaded. This text is also used by screen readers to describe the image to visually impaired users.
<img src="images/sample.jpg" alt="A beautiful sample image">

Adding Titles and Captions

To provide additional context or description for your images, you can use the title attribute or wrap your image in a <figure> tag with a <figcaption>.

  • title Attribute: The title attribute displays additional information when the user hovers over the image.
<img src="images/sample.jpg" alt="A beautiful sample image" title="Hover text">

<figure> and <figcaption>: The <figure> element is used to group the image and its caption, while the <figcaption> element provides a caption for the image.

<figure>
    <img src="images/sample.jpg" alt="A beautiful sample image">
    <figcaption>A beautiful sample image</figcaption>
</figure>

Sizing Images

You can control the size of your images using the width and height attributes. These attributes can be set to specific pixel values or percentages.

<img src="images/sample.jpg" alt="A beautiful sample image" width="300" height="200">

Responsive Images

To ensure your images look good on all devices, you can use responsive techniques. The srcset attribute is useful for specifying different image files for different screen resolutions.

<img src="images/sample-small.jpg" srcset="images/sample-medium.jpg 768w, images/sample-large.jpg 1024w" alt="A beautiful sample image">

In this example, the browser selects the appropriate image based on the device's screen resolution, ensuring the best quality and performance.

Wrapping Up

By understanding how to embed and style images in your HTML documents, you can create more dynamic and visually appealing web pages. Next, we'll dive into organizing content with lists in our upcoming blog, "HTML Lists: Organizing Content with Ordered and Unordered Lists."

Stay tuned as we continue to build on these foundational skills in HTML and move towards creating more sophisticated web pages.

Happy coding!

Author: Aryan Kumar is a web developer specializing in HTML, CSS, and JavaScript, working at Asecurity. Contact here (Instagram) : @aryan_geek .

#webdevelopment #html #css #javascript