Links connect the web. Images make it beautiful. Together, they transform plain text into an engaging experience. In this tutorial, you’ll master both.
What you’ll learn:
Creating hyperlinks with the <a> element
Absolute vs. relative URLs
Special link types (email, phone, anchors)
Embedding images with the <img> element
Image alt text and accessibility
Responsive images with the srcset attribute
Prerequisites:
The anchor element <a> creates clickable links to other pages or websites.
Interactive code playground requires JavaScript. Here's the code:
<p>Visit <a href="https://example.com">Example.com</a> to learn more.</p>
<p><a href="https://google.com">Search Google</a> for anything.</p>
The href attribute specifies where the link goes. Notice:
Links are blue and underlined by default
Text inside the <a> tag is what users click
The URL comes after href="
External links go to websites outside your own. Always include the full URL:
Interactive code playground requires JavaScript. Here's the code:
<p>
Visit <a href="https://www.wikipedia.org">Wikipedia</a> for knowledge.
</p>
<p>
Check out <a href="https://github.com">GitHub</a> for code sharing.
</p>
Always use https:// (or http://). Without it, browsers treat it as a relative link.
Internal links navigate within your own website. Use relative paths:
Interactive code playground requires JavaScript. Here's the code:
<p><a href="/about">About Us</a></p>
<p><a href="/contact">Contact</a></p>
<p><a href="/">Back to Home</a></p>
Relative URL examples:
about.html — File in the same folder
pages/about.html — File in a subfolder
../about.html — File in the parent folder
/about — File from the website root
Use mailto: to create email links:
Interactive code playground requires JavaScript. Here's the code:
<p>Email us at <a href="mailto:hello@example.com">hello@example.com</a></p>
<p><a href="mailto:info@company.com?subject=Hello">Contact our team</a></p>
Adding ?subject=Hello automatically fills the email subject.
Use tel: to make clickable phone numbers:
Interactive code playground requires JavaScript. Here's the code:
<p>Call us: <a href="tel:+1-555-123-4567">+1 (555) 123-4567</a></p>
On mobile devices, this opens the phone dialer.
Link to specific sections within a page using anchor IDs:
Interactive code playground requires JavaScript. Here's the code:
<h2 id="about">About Section</h2>
<p>Content about us...</p>
<h2 id="services">Services</h2>
<p>What we offer...</p>
<p>
<a href="#about">Go to About</a> |
<a href="#services">Go to Services</a>
</p>
First create an ID with id="name", then link to it with href="#name".
Use target="_blank" to open links in a new tab:
Interactive code playground requires JavaScript. Here's the code:
<a href="https://example.com" target="_blank">
Open in new tab
</a>
Add a tooltip that appears on hover:
Interactive code playground requires JavaScript. Here's the code:
<a href="https://example.com" title="Learn more about Example">
Example.com
</a>
Images make your pages visual. The <img> element embeds images directly:
Interactive code playground requires JavaScript. Here's the code:
<img src="https://images.unsplash.com/photo-1453728691169-d74f74d3c161"
alt="A laptop keyboard with code on screen"
width="300"
height="200">
<p>An image of a laptop keyboard.</p>
Attribute Purpose srcPath to the image file (required) altText if image doesn’t load (required) widthImage width in pixels heightImage height in pixels
The alt attribute is crucial for users with visual impairments who use screen readers.
<!-- Describes the image content -->
< img src = " dog.jpg " alt = " A golden retriever playing in a park " >
<!-- Describes what the image shows -->
< img src = " chart.jpg " alt = " Bar chart showing sales growth from 2020 to 2024 " >
<!-- For decorative images that add no meaning, use empty alt -->
< img src = " decoration.jpg " alt = "" >
< img src = " dog.jpg " alt = " Image " >
<!-- Redundant (already in caption) -->
< img src = " dog.jpg " alt = " Dogs " > < p > Dogs </ p >
<!-- Missing entirely -->
Alt Text is Accessibility
Every image MUST have an alt attribute. Even decorative images need alt="" (empty). This helps screen reader users understand your content.
You can wrap an image in a link:
Interactive code playground requires JavaScript. Here's the code:
<a href="https://example.com">
<img src="https://images.unsplash.com/photo-1453728691169-d74f74d3c161"
alt="Click to visit Example.com"
width="300"
height="200">
</a>
This makes the entire image clickable.
For modern responsive design, use srcset to serve different images on different screen sizes:
This tells the browser which image to load based on screen width:
Phones see image-small.jpg
Tablets see image-medium.jpg
Desktops see image-large.jpg
Interactive code playground requires JavaScript. Here's the code:
<div style="border: 1px solid #ccc; padding: 15px; width: 300px;">
<h3>Amazing Product</h3>
<img src="https://images.unsplash.com/photo-1453728691169-d74f74d3c161"
alt="A laptop showing code on the screen"
width="100%"
height="200">
<p>High-quality laptop for developers and designers.</p>
<p>
Price: <strong>$999</strong>
</p>
<p>
<a href="/product-details">Learn more</a> |
<a href="mailto:sales@example.com">Contact sales</a>
</p>
</div>
Create a page with links and images:
Interactive code playground requires JavaScript. Here's the code:
<h1>My Favorite Websites</h1>
<h2>Search Engines</h2>
<p>
<a href="https://google.com" target="_blank">Google</a>
</p>
<h2>Social Media</h2>
<p>
<a href="https://twitter.com" target="_blank">Twitter</a>
</p>
<h2>Add an image below</h2>
<img src="https://images.unsplash.com/photo-1453728691169-d74f74d3c161"
alt="Replace this with your own image"
width="300"
height="200">
Experiment with:
Adding more internal links
Creating email or phone links
Changing image URLs to different images
Creating anchor links to sections
Using target=_blank Everywhere
Opening every link in a new tab frustrates users. Use it only for external links.
Images Without Dimensions
Always specify width/height or use responsive techniques. This prevents layout shifting when images load.
<a> tags create clickable links with the href attribute
Use absolute URLs for external links, relative URLs for internal links
Special URLs: mailto: for email, tel: for phone, # for page anchors
<img> embeds images with required src and alt attributes
Alt text is essential for accessibility and SEO
target="_blank" opens links in new tabs
Responsive images use srcset for different screen sizes
Lists
Organize information with ordered and unordered lists.
Continue →
Basic Structure
Learn semantic page structure with header, nav, main, and footer.
Continue →