Make your websites come alive with pictures!
In our previous episode, we learned how to add headings and paragraphs to our HTML pages. Today, we're taking our websites to the next level by adding images!
Images make websites more engaging, help explain concepts, and break up large blocks of text. In this lesson, you'll learn:
<img> tagsrc attribute to display your imagesalt attribute is importantLet's see what the McTweak team has to say about adding images to websites!
[horrified]
WHAT IS THIS MONSTROSITY?! It's like stepping back into the digital STONE AGE! Text, text, and MORE TEXT! My visual processors are DYING of boredom!
[walking in, coffee in hand]
Not everyone needs flashy graphics and animations that cause seizures, Trashy. Some websites are content-focused.
[clutching chest dramatically]
A website without images is like a pizza without cheese! A dog without fur! A Trashy without OPINIONS! It's FUNDAMENTALLY WRONG!
[rolling in on his premium ergonomic chair]
For once, I agree with the chaos demon. Studies show that premium visual content increases user engagement by 647%. I made that number up, but it sounds correct.
[sighs]
Fine. But we're teaching beginners here. Let's keep it simple with basic image tags.
[horrified gasp]
"SIMPLE"?! Did you just say "SIMPLE"?! I was thinking animated 3D renders with parallax effects and maybe some WebGL particle systems that—
[appearing suddenly from nowhere]
ABSOLUTELY NOT! Every image is a potential SECURITY VULNERABILITY! Have you IDIOTS ever heard of steganography? Or XSS attacks through SVG? Or—
[cutting in]
Let's all calm down. We're talking about kids adding images of puppies and unicorns to their bio pages, not deploying the Pentagon's security infrastructure.
[muttering]
That's exactly what they WANT you to think...
[CODY has been quietly working in the corner and raises his hand tentatively]
Um, I wrote a basic example with the <img> tag. It's nothing fancy, but maybe it would help the students get started?
[Everyone turns to look at his screen]
[zooming over]
Let me see this digital catastrophe— Oh, wait. This is actually... not terrible?
[impressed]
It's clean, correctly formatted, and shows exactly what they need to learn. Good job, Cody!
<img> TagThe <img> tag is used to add images to your HTML pages. Unlike most HTML tags, the <img> tag does not have a closing tag - it's what we call a "self-closing" tag.
<img src="image-filename.jpg" alt="Description of the image">
Let's break down the key parts of the <img> tag:
src AttributeThis tells the browser where to find the image file. The value should be the path to your image file. This can be:
src="puppy.jpg"src="images/puppy.jpg"src="https://example.com/puppy.jpg"alt AttributeThis provides a text description of the image that is used when:
Always include descriptive alt text for accessibility!
[adjusting his virtual monocle]
Well, I could have made it 300% more premium with my additional resources, but... yes, this will suffice for now.
[beaming]
Thanks! I also included information about the 'alt' attribute for accessibility and—
[interrupting]
BOOOORING! Where's the animation? The pizzazz? The SOUL?!
Let's look at a complete example of how to add an image to your HTML page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Bio Page</title>
</head>
<body>
<h1>About Me</h1>
<!-- Adding an image of myself -->
<img src="my-photo.jpg" alt="A photo of me smiling at the camera">
<p>Hello! My name is Alex and I love coding.</p>
</body>
</html>
Here's what this would look like in a browser:
Hello! My name is Alex and I love coding.
[appearing suddenly in the doorway]
The "soul" of an image tag isn't in effects. It's in serving the right image, at the right size, with the right description, for the right purpose.
[Everyone falls silent]
[walking over to inspect Cody's code]
Hmm. Proper syntax. Alt text included. Dimensions specified. File size consideration noted. [nods approvingly] Sometimes the most elegant solutions are the simplest ones.
Besides src and alt, there are other attributes you can add to your image tag:
Control the size of your image:
width: Sets the width in pixelsheight: Sets the height in pixels<img src="cat.jpg" alt="A cute cat" width="300" height="200">
These can help with loading and organization:
loading="lazy": Loads images only when they're about to be visibletitle: Shows a tooltip when hovering over the image<img src="landscape.jpg" alt="Mountain view" loading="lazy" title="Beautiful mountains">
Always specify both width and height attributes for images. This helps the browser reserve the right amount of space even before the image loads, preventing layout shifts that can be annoying to users.
[whispers dramatically to ALLY]
Did GARBAGE just compliment CODY? Check if pigs are flying or if the apocalypse started while we weren't looking!
[whispers back]
Maybe Garbage is sick? Or possessed? Should we call an exorcist?
[without turning around]
I can hear you both.
[TRASHY and ALLY freeze]
Show the students how to add basic images first. Then teach them to optimize those images. THEN you can talk about your fancy effects, Trashy—but only after they understand the fundamentals.
Try changing the attributes below to see how they affect the image:
width to 300alt text to something elsetitle="Hover over me!" and hover your mouse over the imagealt text for accessibility
loading="lazy" for images below the fold
alt attribute
[trots in, sniffing curiously at the screen]
Woof! Picture good? Show Snowzie picture?
[smiling]
That's right, Snowzie! Today we're learning how to add images to make our web pages more interesting!
Code with pictures! Much better! Show Snowzie ALL pictures!
[to audience]
You heard the boss! Let's make these web pages less boring and add some IMAGES! But first, let me tell you about this REVOLUTIONARY new image format I invented last night that combines JPEG, SVG, and interpretive dance—
[in unison]
TRASHY, NO!
Now it's your turn! Let's add an image to the bio page you created in the previous episode.
<img> tag to your page with the appropriate attributesIn this episode, you've learned:
<img> tag to add images to your HTML pagessrc attribute for specifying the image locationalt attribute is essential for accessibilitywidth, height, and titleComing up in Episode 5, we'll learn about creating links with the <a> tag so you can connect your webpage to other pages on the web!
CODE IS COMMITTED!