HTML Links and Images
1. Introduction
Welcome to Chapter 3! The true power of the "World Wide Web" lies in its interconnectedness. In this chapter, you will learn how to create hyperlinks to navigate between pages and how to embed images to make your websites visually appealing.2. Learning Objectives
By the end of this chapter, you will be able to:- Create internal and external hyperlinks.
- Make links open in a new tab.
- Create email and phone links.
-
Embed images using the
<img>tag.
-
Understand the importance of
alttext for accessibility and SEO.
- Turn images into clickable links.
3. HTML Links (The Anchor Tag)
Links in HTML are created using the<a> (anchor) tag. The most important attribute of the <a> tag is href (Hypertext Reference), which indicates the destination.
4. Internal vs External Links
-
External Links: Point to a completely different website (requires the full
https://URL).
- Internal Links: Point to another page on the *same* website. You can use relative paths.
5. Opening Links in a New Tab
To force a link to open in a new browser tab, use thetarget="_blank" attribute.
6. Email and Phone Links
You can create links that automatically open the user's default email client or trigger a phone call on mobile devices usingmailto: and tel:.
7. HTML Images
Images are embedded using the self-closing<img> tag. It requires two main attributes:
-
1.
src: The source path to the image file.
-
2.
alt: Alternative text displayed if the image fails to load (vital for screen readers).
8. Relative vs Absolute Paths for Images
Just like links, images can be loaded from external URLs (absolute) or from your project folder (relative).9. Adjusting Image Size
You can usewidth and height attributes directly in HTML to resize images, though using CSS is the modern best practice.
10. Clickable Images
You can turn an image into a link by nesting the<img> tag inside an <a> tag.
11. Image Optimization Basics
Best Practice: Always compress your images before uploading them. Large images (like 5MB photos) will make your website incredibly slow, hurting your SEO and annoying users. Use formats like WebP or compressed JPEG.12. Best Practices & Common Mistakes
Best Practices:-
ALWAYS include descriptive
alttext for images. If the image is purely decorative, usealt="".
- Ensure link text is descriptive. Instead of "Click here", use "Read our privacy policy".
Common Mistakes:
-
Forgetting the
https://in external links.
-
Forgetting the closing
</a>tag, which accidentally turns the rest of the page into a giant link.
- Using massive images and scaling them down via HTML width/height instead of cropping them first.
13. Mini Project: Portfolio Gallery Page
Task: Create a simple portfolio layout with a clickable image gallery.14. MCQ Quiz
- 1. Which attribute specifies the destination of a link?
-
A)
src
-
B)
href
-
C)
link
-
D)
url
- 2. What is the correct HTML for inserting an image?
-
A)
<img href="image.gif" alt="MyImage">
-
B)
<image src="image.gif" alt="MyImage">
-
C)
<img src="image.gif" alt="MyImage">
-
D)
<img>image.gif</img>
- 3. How do you open a link in a new tab?
-
A)
target="new"
-
B)
target="blank"
-
C)
open="blank"
-
D)
href="_blank"
15. Interview Questions
Q: Why is thealt attribute essential for <img> tags?
A: It provides descriptive text for visually impaired users using screen readers, displays text if the image fails to load over a slow connection, and helps search engines understand the image content for SEO.
Q: What is the difference between absolute and relative paths?
A: An absolute path includes the full URL (e.g., https://example.com/page.html), making it point to an exact location on the internet. A relative path points to a file relative to the current file's location (e.g., /images/pic.jpg).
16. FAQs
Q: Can I use GIFs in the<img> tag?
A: Yes! The <img> tag supports JPEGs, PNGs, GIFs, WebP, and SVG formats perfectly.