Working with Images in HTML

Images are an important part of web pages. They can make your content more attractive and provide valuable information to users.

Adding Images

To add an image in HTML, you use the <img> tag. This tag is self-closing and requires the src attribute to specify the image source, and the alt attribute to provide alternative text for screen readers.

<img src="path/to/image.jpg" alt="Description of image">
    

Image Attributes

The <img> tag has several attributes:

Example:

<img src="image.jpg" alt="My Image" width="300" height="200">
    

Image Formats

Common image formats supported in HTML include:

Responsive Images

To create responsive images that adapt to different screen sizes, use CSS. The following code sets the image to scale with its container:

<img src="image.jpg" alt="My Image" style="max-width: 100%; height: auto;">
    

Using Images as Links

You can also make images clickable by wrapping them in an anchor (<a>) tag:

<a href="https://example.com">
    <img src="image.jpg" alt="My Image">
</a>
    

Working with Images in HTML

Images are an essential part of web design. They can enhance the visual appeal of a website and provide useful information to users.

HTML Image Element

The <img> tag is used to embed images in HTML documents. The src attribute specifies the path to the image you want to display.

<img src="path/to/image.jpg" alt="Description of image">
    

Attributes of the Image Element

Example of an Image Tag

<img src="logo.png" alt="Website Logo" width="200" height="100">
    

Image Formats

HTML supports various image formats:

Responsive Images

To make images responsive and scale with the screen size, you can set the width to 100% and height to auto:

<img src="path/to/image.jpg" alt="Description of image" style="width: 100%; height: auto;">
    

Using Images as Links

Images can also be linked to other pages or websites by wrapping the <img> tag within an <a> tag:

<a href="https://www.example.com">
    <img src="logo.png" alt="Website Logo">
</a>
    

Image Accessibility

Using the alt attribute is crucial for accessibility. It provides a text alternative for screen readers and users who cannot view the images.