Welcome back! So far, youβve learned how to add text content and format it beautifully. Now itβs time to take your webpage to the next level by adding images. Images make your content more engaging, break up long text, and help communicate ideas visually.
Letβs dive into how to add, style, and optimize images in HTML!
πΌ The <img>
Tag β The Basic Syntax
HTML uses the <img>
tag to display images. Unlike headings and paragraphs, this tag is self-closing, meaning it doesnβt need a closing tag.
Basic syntax:
<img src="image.jpg" alt="Description of image">
π· What do these attributes mean?
src
(source): The path to your image file. This can be a local file or a URL.alt
(alternative text): A description of the image. This is important for screen readers (accessibility) and also displays if the image fails to load.
π Using Local vs. Online Images
Local Image Example
<img src="images/my-photo.jpg" alt="My Profile Photo">
Make sure the image file is in the correct folder relative to your HTML file.
Online Image Example
<img src="https://example.com/photo.jpg" alt="Example Photo">
Be careful when using online images β they may be removed or change without notice.
π Controlling Image Size
You can change the width and height directly in the tag:
<img src="cat.jpg" alt="Cute Cat" width="300" height="200">
π‘ Tip
- Use either width or height to maintain the aspect ratio automatically.
- Itβs often better to control size with CSS for flexibility.
Example with CSS:
<img src="dog.jpg" alt="Happy Dog" style="width: 100%; max-width: 500px;">
β¨ Adding Styling to Images
You can style images just like any other element using CSS.
Example:
<img src="flowers.jpg" alt="Colorful Flowers" style="border: 2px solid black; border-radius: 8px;">
This adds a black border and rounded corners to your image.
π§ Aligning Images
You can use CSS to align images:
<img src="mountain.jpg" alt="Beautiful Mountain" style="display: block; margin: 0 auto;">
This centers the image on the page.
π‘ Why alt
Text is Important
- Helps screen readers describe images to visually impaired users.
- Displays if the image canβt load.
- Improves SEO by giving search engines more context.
Always write meaningful and concise alt text!
π‘ Example: Complete HTML Page with Images
<!DOCTYPE html>
<html>
<head>
<title>My Photo Gallery</title>
</head>
<body>
<h1>Welcome to My Gallery</h1>
<p>Here are some of my favorite pictures.</p>
<img src="sunset.jpg" alt="Sunset at the beach" width="400">
<img src="mountain.jpg" alt="Snowy mountain peak" style="width: 300px; border: 3px solid #333;">
<p>This is my dog:</p>
<img src="dog.jpg" alt="Smiling dog" style="width: 250px; display: block; margin: 0 auto;">
</body>
</html>
β Summary
- Use the
<img>
tag to display images. - Always include descriptive
alt
text for accessibility and SEO. - Control size and styling with HTML attributes or CSS.
- Align images to make your layout look clean and professional.
π Whatβs Next?
In the next blog, weβll explore how to add links and buttons to make your page interactive. Youβll learn how to create navigation menus, link to other pages, and add call-to-action buttons.
Stay tuned and happy coding! π