What are Links in HTML?
Links (hyperlinks) are the foundation of the web - they connect pages and allow navigation. Created with the <a> (anchor) tag, the href attribute defines the destination.
Basic Syntax
<a href="destination-url">Link Text</a>
Types of Links
1. External Links (to other websites)
<a href="https://www.google.com">Visit Google</a>
<a href="https://www.wikipedia.org" target="_blank">Wikipedia (new tab)</a>
2. Internal Links (within your site)
<a href="about.html">About Us</a>
<a href="/products/shoes.html">Shoes Category</a>
<a href="../index.html">Home</a>
3. Anchor Links (same page navigation)
<a href="#contact">Jump to Contact Section</a>
<a href="#top">Back to Top</a>
<!-- Target element needs an id -->
<section id="contact">Contact Info...</section>
4. Email Links
<a href="mailto:hello@example.com">Send Email</a>
<a href="mailto:support@example.com?subject=Help Request">Email Support</a>
5. Phone Links
<a href="tel:+911234567890">Call: +91 123 456 7890</a>
6. Download Links
<a href="document.pdf" download>Download PDF</a>
<a href="report.pdf" download="Annual-Report-2024.pdf">Download Report</a>
Important Link Attributes
| Attribute | Purpose | Example |
|---|---|---|
| href | Destination URL (required) | href="https://example.com" |
| target | Where to open link | target="_blank" (new tab) |
| title | Tooltip on hover | title="Visit our homepage" |
| rel | Relationship/security | rel="noopener noreferrer" |
| download | Triggers file download | download="filename.pdf" |
Best Practices for Links
- Descriptive text - Use meaningful link text, not "click here"
- Security - Add
rel="noopener noreferrer"when usingtarget="_blank" - Accessibility - Make link purpose clear from text alone
- Visual feedback - Style links distinctly (underline, color)
- Test links - Ensure all links work correctly
❌ Bad Link Text:
<a href="services.html">Click here</a> to see our services.
✅ Good Link Text:
View our <a href="services.html">web development services</a>.
Complete Link Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Links Example</title>
</head>
<body>
<h1>Navigation Examples</h1>
<!-- External link with security -->
<a href="https://github.com" target="_blank" rel="noopener noreferrer">
Visit GitHub (opens in new tab)
</a>
<!-- Internal link -->
<a href="about.html">About Our Company</a>
<!-- Email link with subject -->
<a href="mailto:info@company.com?subject=Inquiry">Contact Us</a>
<!-- Phone link -->
<a href="tel:+15551234567">Call: (555) 123-4567</a>
<!-- Download link -->
<a href="brochure.pdf" download>Download Our Brochure</a>
<!-- Anchor link -->
<a href="#footer">Jump to Footer</a>
<footer id="footer">
<p>Footer content here</p>
</footer>
</body>
</html>
Links Quick Quiz
Q1. Which attribute defines the destination URL in a link?
Q2. How do you open a link in a new tab?
Q3. What attribute should you add with target="_blank" for security?
Images in HTML
Images make websites visually appealing and informative. The <img> tag is a self-closing element that embeds images.
Basic Syntax
<img src="path/to/image.jpg" alt="Description of image">
Essential Image Attributes
| Attribute | Purpose | Required? |
|---|---|---|
| src | Image file path or URL | ✅ Yes |
| alt | Alternative text (accessibility & SEO) | ✅ Yes |
| width | Image width in pixels | ❌ Optional |
| height | Image height in pixels | ❌ Optional |
| loading | Lazy loading (loading="lazy") | ❌ Optional |
| title | Tooltip on hover | ❌ Optional |
Why Alt Text Matters
- Accessibility - Screen readers announce alt text to visually impaired users
- SEO - Search engines use alt text to understand images
- Fallback - Shows when image fails to load
- Context - Provides meaning when images are disabled
Writing Good Alt Text:
<!-- ❌ Bad: Too vague -->
<img src="dog.jpg" alt="image">
<!-- ❌ Bad: Redundant -->
<img src="sunset.jpg" alt="Image of a sunset">
<!-- ✅ Good: Descriptive -->
<img src="golden-retriever.jpg" alt="Golden retriever playing with ball in park">
<!-- ✅ Good: Context-aware -->
<img src="ceo-photo.jpg" alt="Jane Smith, CEO of TechCorp">
Image Examples
<!-- Basic image -->
<img src="logo.png" alt="Company Logo">
<!-- Image with dimensions -->
<img src="product.jpg" alt="Red sneakers" width="300" height="200">
<!-- Lazy loading for performance -->
<img src="large-photo.jpg" alt="Team photo" loading="lazy">
<!-- Responsive image (CSS) -->
<img src="banner.jpg" alt="Welcome banner" style="width:100%;max-width:800px;height:auto;">
<!-- Image as link -->
<a href="https://example.com">
<img src="clickable-banner.jpg" alt="Visit our store - Special offer">
</a>
Responsive Images with <picture>
The <picture> element allows different images for different screen sizes or formats:
<picture>
<!-- WebP format for modern browsers -->
<source srcset="image.webp" type="image/webp">
<!-- Mobile image (smaller file) -->
<source srcset="image-small.jpg" media="(max-width: 600px)">
<!-- Desktop image (larger file) -->
<source srcset="image-large.jpg" media="(min-width: 601px)">
<!-- Fallback image -->
<img src="image.jpg" alt="Product showcase">
</picture>
Image Formats
- JPEG (.jpg) - Photos, complex images with many colors
- PNG (.png) - Transparent backgrounds, logos, icons
- WebP (.webp) - Modern format, smaller file size, good quality
- SVG (.svg) - Vector graphics, logos, icons (scalable)
- GIF (.gif) - Simple animations, low color images
Complete Image Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Images Example</title>
<style>
img { max-width: 100%; height: auto; }
</style>
</head>
<body>
<h1>Our Product Gallery</h1>
<!-- Product image with details -->
<img src="laptop.jpg"
alt="Silver laptop with 15-inch display"
width="500"
height="400"
loading="lazy"
title="MacBook Pro 2024">
<!-- Clickable image -->
<a href="product-details.html">
<img src="phone.jpg" alt="View smartphone details">
</a>
<!-- Responsive image -->
<picture>
<source srcset="hero-mobile.jpg" media="(max-width: 768px)">
<source srcset="hero-desktop.jpg" media="(min-width: 769px)">
<img src="hero-default.jpg" alt="Welcome to our store">
</picture>
</body>
</html>
Images Quick Quiz
Q4. Which attribute specifies the image file path?
Q5. Which attribute provides text when an image fails to load?
Q6. Which element allows different images for different screen sizes?
Q7. Which format is best for logos with transparent backgrounds?
Practice Tasks
- Navigation Menu:
- Create links to Home, About, Services, Contact pages
- Add an external link to GitHub (new tab with security)
- Add email and phone links
- Image Gallery:
- Add 3 images with proper alt text
- Make images responsive (max-width: 100%)
- Add lazy loading to images
- Make one image clickable (links to larger version)
- Product Card:
- Product image with dimensions 300x300
- Product name as heading
- Price with old price crossed out
- "Buy Now" link button
- Responsive Hero Image:
- Use <picture> element
- Mobile image for screens under 768px
- Desktop image for larger screens
- Proper alt text describing the hero image
Lesson Summary
Key Takeaways:
- Links (<a>) - Use href for destination, target="_blank" for new tab, rel="noopener noreferrer" for security
- Link types - External, internal, anchor, email (mailto:), phone (tel:), download
- Images (<img>) - Always include src and alt attributes
- Alt text - Critical for accessibility, SEO, and when images fail
- Responsive images - Use <picture> for different screen sizes
- Image formats - JPEG (photos), PNG (transparency), WebP (modern), SVG (vectors)
- Performance - Use loading="lazy" for images below the fold
- Best practices - Descriptive link text, meaningful alt text, responsive design
Links and images are fundamental to web pages. Always prioritize accessibility with proper alt text and security with appropriate link attributes!