Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to put images in HTML

Understanding HTML Images

HTML, which stands for HyperText Markup Language, is the standard language for creating web pages and web applications. In this article, we will be focusing on one specific aspect of HTML: embedding images. Images are not just decorations; they are an integral part of web design that greatly enhances user experience. But how do we include images in an HTML document? Let's find out.

The Image Element

HTML uses elements, which are individual components of a webpage, to structure and display content. To insert an image, we use the <img> element. The <img> element is an empty element, meaning it does not have a closing tag. Here's a simple example:

<img src="myImage.jpg">

In this example, src is an attribute of the <img> element, and it specifies the source (i.e., the location) of the image. The value of the src attribute is the URL of the image you want to insert. This URL can be either an absolute URL (pointing to an image somewhere on the internet) or a relative URL (pointing to an image on your own site).

The Importance of the Alt Attribute

Another crucial attribute for the <img> element is the alt attribute. This attribute provides alternative text for an image if the image cannot be displayed. This could be due to a broken link, a slow connection, or the user using a screen reader due to vision impairment. Here's an example of using the alt attribute:

<img src="myImage.jpg" alt="A description of the image">

The alt attribute improves accessibility and is considered a best practice in web development. It's like a backup singer that steps up when the lead can't perform.

Managing Image Size

How do we control the size of an image? HTML provides width and height attributes for this purpose. These attributes define the width and height of the image:

<img src="myImage.jpg" alt="A description of the image" width="500" height="600">

In this example, the image's width is set to 500 pixels, and the height is set to 600 pixels. It's important to maintain the image's aspect ratio (the ratio of width to height) to prevent the image from getting distorted. It's like resizing a photo in a photo-editing tool - if you only stretch it horizontally, the photo will look warped.

Aligning Images

We can align images in HTML using the style attribute with float property. The float property can take the following values: left, right, or none.

<img src="myImage.jpg" alt="A description of the image" style="float:right">

In this example, the image will float to the right of the container, and the text will wrap around the left side of the image. It's similar to aligning images in a word processor.

Did you know that you can use an image as a hyperlink? By wrapping the <img> element with an <a> (anchor) element, we can make the image clickable, and redirect users to another page when they click on the image.

<a href="">
  <img src="myImage.jpg" alt="A description of the image">

In this example, clicking on the image will take the user to "". It's like those interactive infographics or ads you see on websites, where clicking on different parts of the image leads to different pages.

Final Words

Incorporating images into your HTML pages is a crucial skill for any budding web developer. Not only do images make your website more engaging, but they also improve the user's experience by breaking up blocks of text and making information easier to digest. Remember, the key is not just adding images, but also optimizing them for all users, including those with slow internet connections or visual impairments. And remember, HTML is like the skeleton of a website, and images are like the clothing that makes it more appealing. Happy coding!