Recommended resources
The freely available resources this lesson will direct you to.
Hello! Welcome back to your front-end programming course.
In our last lesson, we established the skeleton of a webpage using semantic HTML elements like <header>, <main>, and <article>. This provided a meaningful structure for assistive technologies and search engines. Now, it's time to add the visual elements that bring a design to life.
This lesson addresses the learning outcome: Embed images using the <img> element with proper alt text and source attributes.
As a designer, you know that images are fundamental to creating an engaging user experience. Today, you'll learn the technical foundation for embedding images in a way that is not only visible but also accessible, performant, and semantically correct.
1. The Anatomy of an Image Element
To place an image on a webpage, we use the <img> element. This is a special kind of element known as a void element (or empty element), which means it doesn't have a closing tag. All the information it needs is provided through attributes. The two most important attributes are src and alt.
Let's start with a practical demonstration of how to add an image to a project.
Learn HTML images in 6 minutes! 🖼️
This video from Bro Code, 'Learn HTML images in 6 minutes!', provides a great hands-on start. It shows how to add an image file to your project and display it using the <img> tag.
Please watch the first 1 minute and 13 seconds. Focus on how the <img> tag is written and how the src attribute is used to point to the image file.
As you saw, the basic syntax is <img src="path/to/your/image.jpg">.
The src Attribute and File Paths
The src (source) attribute is essential; it tells the browser where to find the image file. The path you provide can be of two types:
- Relative URL: Points to a file within your own website's folder structure. This is the most common and recommended approach.
src="dog.png": The image is in the same folder as the HTML file.src="images/dog.png": The image is inside animagessubfolder.
- Absolute URL: A full web address (e.g.,
https://...) pointing to an image hosted on another server.
It's best practice to host your own images and use relative paths. Relying on images from other websites ("hotlinking") can lead to broken images if the source is moved or taken down, and it uses their bandwidth, which is generally considered poor practice.
2. The alt Attribute: Designing for Everyone
The alt (alternative text) attribute is arguably the most critical attribute from a user experience perspective. It provides a textual description of the image.
This text is used in several key situations:
- Accessibility: Screen readers announce the
alttext to visually impaired users, allowing them to understand the image's content and purpose. - Broken Images: If an image fails to load (due to a wrong path, network error, etc.), the browser will display the
alttext instead. - Search Engine Optimization (SEO): Search engines use
alttext to understand what an image is about, which can help your page rank in image searches.
The next segment of the video you just watched includes an excellent demonstration of how a screen reader uses alt text.
Learn HTML images in 6 minutes! 🖼️
Let's continue with the Bro Code video. This part clearly demonstrates the impact of the alt attribute on accessibility.
Watch from 01:13 to 02:37. Pay close attention to how the screen reader announces the image's alt text, making the content accessible.
How to Write Good alt Text
Writing effective alt text is a design skill. It's about conveying the content and function of an image concisely.
HTML images - Learn web development | MDN
The MDN Web Docs article 'HTML images' provides expert guidance on writing meaningful alt text. It explains what to consider based on the image's context.
Please read the section titled 'Alternative text'. It details why alt text is important and offers a guide on what to write depending on the image's purpose (e.g., decoration, content, link).
Key guidelines for alt text:
- Be descriptive and concise: Describe what's in the image. For example,
alt="A golden retriever puppy playing with a red ball in a grassy field." - Don't be redundant: Avoid starting with "Image of..." or "Picture of...". The
<img>tag already tells the browser it's an image. - Consider the context: If the image is a link, the
alttext should describe the link's destination (e.g.,alt="Read our full company history."). - Use an empty
altattribute for decorative images: If an image is purely for decoration and provides no information, usealt="". This tells screen readers to ignore it, preventing unnecessary noise for the user.
3. Sizing Images and Improving Performance
Have you ever loaded a webpage where the text suddenly jumps down to make room for an image that just appeared? This is called Layout Shift, and it creates a jarring user experience.
You can prevent this by specifying the image's dimensions using the width and height attributes.
<img src="images/puppy.jpg" alt="A golden retriever puppy." width="400" height="300">
When you include these attributes, the browser reserves the correct amount of space for the image before it has even downloaded. This leads to a smoother, more stable page load.
HTML images - Learn web development | MDN
The MDN article on 'HTML images' has an excellent section explaining why setting width and height is so important for user experience.
Read the section 'Width and height'. It clearly illustrates how these attributes prevent layout shifts and improve the perceived loading performance of your page.
Important Note: Use the width and height attributes to tell the browser the image's intrinsic size. You should not use them to resize an image (e.g., displaying a 1000px image at 200px). For resizing and responsive styling, you will use CSS, which we'll cover in a later module.
4. Structuring Images with Captions
In our last lesson, we focused on semantic structure. Images are no exception. When an image has a caption, it's important to semantically link them together. Simply placing a <p> tag under an <img> tag doesn't tell a screen reader that they are related.
The correct way to do this is with the <figure> and <figcaption> elements.
<figure>: A container for a self-contained piece of content, like an image, diagram, or code snippet.<figcaption>: The caption for the content inside the<figure>.
<figure>
<img
src="images/cat.jpg"
alt="A grey cat sleeping on a bookshelf."
width="800"
height="600">
<figcaption>Fig.1 - A cat finds a comfortable, if unusual, napping spot.</figcaption>
</figure>
This structure clearly tells browsers and assistive technologies that the caption belongs to the image.
5. Practice: Adding an Image to a Blog Post
Let's apply what you've learned. Here is the semantic HTML for the blog post from our last lesson. Your task is to add an image to the article.
Instructions:
- Imagine you have an image file named
first-post.jpg(which is 600px wide and 400px high) inside animagesfolder. - Modify the HTML below to embed this image within the
<article>element, right after the heading. - Ensure you include a meaningful
altattribute. - Add the correct
widthandheightattributes to prevent layout shift. - Wrap the image in a
<figure>element and add a simple<figcaption>.
Starting Code:
<body>
<header>
<h1>My Awesome Blog</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<main>
<article>
<h2>My First Post</h2>
<!-- ADD YOUR IMAGE CODE HERE -->
<p>This is the first paragraph of my blog post. It's very exciting!</p>
<p>Here is another paragraph, continuing the story.</p>
</article>
</main>
<footer>
<p>© 2024 My Awesome Blog</p>
</footer>
</body>
Click to see the solution
Solution:
<body>
<header>
<h1>My Awesome Blog</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<main>
<article>
<h2>My First Post</h2>
<figure>
<img
src="images/first-post.jpg"
alt="A person writing in a journal with a cup of coffee on the desk."
width="600"
height="400">
<figcaption>Starting the journey of a new blog.</figcaption>
</figure>
<p>This is the first paragraph of my blog post. It's very exciting!</p>
<p>Here is another paragraph, continuing the story.</p>
</article>
</main>
<footer>
<p>© 2024 My Awesome Blog</p>
</footer>
</body>
Conclusion
Excellent work! You can now add visual content to your web pages in a professional and accessible manner. Properly embedding images is a perfect example of where technical implementation directly impacts user experience—a bridge you are now equipped to build.
Key Takeaways:
- Use the
<img>tag with thesrcattribute to embed images. - Always provide a descriptive
altattribute for accessibility, or an empty one (alt="") for purely decorative images. - Set
widthandheightattributes to prevent layout shifts and improve loading experience. - Use
<figure>and<figcaption>to semantically associate an image with its caption.
In our next lesson, we'll move from displaying content to collecting it. We will learn how to create HTML forms, allowing you to build interactive elements like contact forms, search bars, and login fields.