Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add a picture in HTML

Understanding the Basics

Before diving into how to add a picture in HTML, let's make sure we're all on the same page about what HTML is. HTML, which stands for Hypertext Markup Language, is a language used to create web pages. In other words, it's like the skeleton of a website.

HTML uses various elements, represented by tags, to structure content on a web page. These elements can be anything from paragraphs of text (<p>) to headings (<h1> to <h6>) and more.

Now, one important element we often want to add to our web pages is an image. This is where the <img> tag comes in.

The <img> Tag

The <img> tag is used in HTML to embed an image into a web page. This tag is an empty tag, meaning it doesn't have a closing tag like the <p> or <h1> tags.

Here's an example of what the <img> tag looks like:

<img src="url">

The src attribute is important here. It's what tells the browser where to find the image you want to display. The URL can be a local path on your computer or a web address.

Adding a Picture Using a Web URL

Let's say you found an image online that you want to add to your web page. You would use the <img> tag and the image's web URL as the src attribute. Here's how you would do it:

<img src="">

In this example, is the web URL where your image is stored. When the browser reads this HTML, it will fetch the image from that URL and display it on your web page.

Adding a Picture from Your Computer

What if the image you want to add is on your computer? Then you would use a local path as the src attribute. Here's an example:

<img src="images/myimage.jpg">

In this example, images/myimage.jpg is the local path to the image. The browser will look for a folder named images in the same directory as your HTML file, and inside that folder, it will look for a file named myimage.jpg.

Adjusting the Image Size

The default size of an image in HTML is the size of the image file. However, you can use the width and height attributes to change this.

For example, if you want to set the image width to 500 pixels and the height to 300 pixels, you would do it like this:

<img src="images/myimage.jpg" width="500" height="300">

Just remember to keep the aspect ratio of your image to prevent it from looking stretched or squished.

Alternative Text

The alt attribute is another important attribute for the <img> tag. This attribute provides alternative text for an image if the image cannot be displayed. This can happen if the image file is missing or if the viewer is using a screen reader due to visual impairment.

Here's how to add alternative text to an image:

<img src="images/myimage.jpg" alt="A description of the image">

In summary, adding a picture in HTML is a relatively simple process. By understanding the <img> tag and its src, width, height, and alt attributes, you can start incorporating images into your web pages. Just remember to use clear and descriptive alt text for accessibility and to always keep your images optimized for web use. Happy coding!