Adding Images to HTML: A Step-by-Step Guide
In the digital age, images are as crucial to websites as text. They enhance user experience, break up content, and can even improve SEO. Adding images to your HTML is a straightforward process. Let's dive into the details.
Understanding the HTML Image Tag
The HTML image tag, <img>, is used to embed images in web pages. It's an empty tag, meaning it doesn't have a closing tag like </img>. Here's a basic structure:
<img src="URL" alt="Alternative Text">
Understanding the Attributes
- src: The source (src) attribute specifies the URL of the image. It's mandatory.
- alt: The alternative text (alt) attribute provides alternative information for an image if a user for some reason cannot view it. It's also crucial for accessibility and SEO.
- width and height: These attributes specify the width and height of the image. They're optional, but they can help control the layout of your page.
Adding an Image to Your HTML
Let's add an image to a simple HTML page. Suppose we want to add an image of a cat from an external URL:

<img src="https://example.com/cat.jpg" alt="A cute cat">
If the image is located on your own server, the URL would look something like this: src="images/cat.jpg".
Controlling Image Size
You can control the size of the image using the width and height attributes. However, it's generally better to use CSS for this, as it allows for more flexibility and better control over your page layout. Here's how you might do it with CSS:
.image-class {
width: 300px;
height: auto;
}
Then, in your HTML, you'd simply add a class to your image tag:

<img src="https://example.com/cat.jpg" alt="A cute cat" class="image-class">
Responsive Images
With the rise of mobile devices, it's important to make sure your images are responsive, i.e., they adjust their size based on the screen they're viewed on. This can be achieved using CSS media queries or the srcset attribute. Here's an example using srcset:
<img srcset="cat.jpg 300w, cat@2x.jpg 600w" sizes="(max-width: 600px) 300px, 600px" alt="A cute cat">
Conclusion
Adding images to your HTML is a simple process that can greatly enhance your web pages. Whether you're using external or internal images, and whether you're controlling their size with HTML attributes or CSS, the process is straightforward and powerful. Happy coding!