Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to insert images in HTML

Understanding HTML Images

Images are a crucial part of any website. They add depth, context, and visual interest to your page, making it more engaging for your visitors. In HTML, the language that defines the structure of web pages, images are included using the <img> tag.

The <img> tag is an empty tag, which means it doesn't have a closing tag like <img></img>. Instead, it closes within the tag itself, like <img />. It's kind of like a self-contained package that doesn't need anything else to complete it.

The Basics of the <img> Tag

The <img> tag uses two important attributes: src and alt.

The src attribute stands for source, which is where the image file is located. This could be a path to a file in your website's directory, or a URL to an image on the internet. Think of the src attribute as the address where your image lives.

<img src="myImage.jpg" />

The alt attribute is used to describe the image. This is particularly important for visually impaired users who rely on screen readers and for instances where the image cannot be loaded. It's like a short description or caption for your image.

<img src="myImage.jpg" alt="A beautiful sunset over the mountains" />

Adding Width and Height

You can also specify the width and height of an image using the width and height attributes. These values are defined in pixels.

<img src="myImage.jpg" alt="A beautiful sunset over the mountains" width="500" height="600" />

It's like resizing a picture in a photo editing app: the larger the numbers, the bigger the picture.

Using CSS for Image Styles

While you can use HTML to add basic styles to your images, like width and height, it's generally better to use CSS (Cascading Style Sheets) for more advanced styles. CSS is like the stylist or designer of a website, while HTML is like the architect.

For instance, you can use CSS to add a border to your image:

<img src="myImage.jpg" alt="A beautiful sunset over the mountains" style="border:1px solid black;" />

Understanding Image Formats

There are various image formats you can use in HTML: JPEG, PNG, GIF, SVG, and more. Each has its own strengths and weaknesses. For example, JPEGs are great for photographs but not for images with text, while SVGs are perfect for logos and icons because they remain sharp regardless of size. It's like choosing the right tool for a job, where the job is displaying your image perfectly.

Just like you can turn text into a clickable link, you can also turn an image into a link. All you have to do is wrap the <img> tag inside an <a> (anchor) tag, which is used to create links.

<a href="">
   <img src="myImage.jpg" alt="A beautiful sunset over the mountains" />

It's like turning your image into a doorway that leads to another page or website.

Image Maps

Image maps allow you to make certain areas of an image into links. This is useful for images like maps, where you might want different areas to link to different destinations.

<img src="map.jpg" alt="World map" usemap="#worldmap" />

<map name="worldmap">
  <area shape="rect" coords="34,44,270,350" href="africa.html" alt="Africa" />
  <area shape="circle" coords="430,198,60" href="europe.html" alt="Europe" />

It's like creating a treasure map, where different spots lead to different treasures.

Responsive Images

Responsive images resize themselves based on the size of the viewer's screen. This is crucial in today's world, where people are viewing websites on a wide range of devices, from desktops to mobile phones.

<img src="myImage.jpg" alt="A beautiful sunset over the mountains" style="max-width:100%;height:auto;" />

Think of responsive images like water: they take the shape of whatever container they're in, ensuring they always look their best.

In conclusion, images are a crucial part of HTML and web design. They add visual interest to your pages and can also serve functional purposes, like links and maps. Understanding how to use the <img> tag and its various attributes and styles will greatly enhance your web development skills.