HTML Notes
Master HTML links and images — anchor tags, href types (absolute, relative, mailto, tel), target attribute, images with src and alt, figure, figcaption, responsive images with srcset, and image optimization best practices.
Links and images are arguably the two most-used elements in HTML. Links are what makes the web a "web" — interconnected documents. Images provide the visual richness. Both have more depth than beginners expect.
The target Attribute
Controls where the link opens:
| Value | Behaviour |
|---|---|
_self (default) | Opens in the same tab |
_blank | Opens in a new tab or window |
_parent | Opens in the parent frame |
_top | Opens in the full body of the window |
Security Warning for target="_blank"
When you open a new tab with target="_blank", the opened page gets access to window.opener — it can potentially redirect the page that opened it. Fix this with rel="noopener noreferrer":
noopener— prevents access towindow.openernoreferrer— also hides the referrer URL from the destination site
Always use both when linking to external sites with target="_blank".
Link States (CSS context)
HTML links have four states that CSS can style:
a:link { color: blue; } /* Unvisited link */
a:visited { color: purple; } /* Visited link */
a:hover { color: darkblue; } /* Mouse over */
a:active { color: red; } /* Being clicked */HTML Images — The <img> Tag
The <img> element embeds an image. It is self-closing (no closing tag needed).
<img src="photo.jpg" alt="A mountain landscape at sunset">The src Attribute
Specifies the image source — relative path, absolute URL, or data URL:
The alt Attribute — Critical for Accessibility and SEO
The alt attribute provides alternative text — shown when the image cannot be displayed, and read by screen readers for visually impaired users.
Width and Height Attributes
Always specify width and height on images. This lets the browser reserve space before the image loads, preventing Cumulative Layout Shift (CLS) — a Core Web Vital metric:
<img src="hero.jpg" alt="Hero banner" width="1200" height="630">These are the intrinsic dimensions of the image in pixels. CSS can override the visual size, but the browser still uses these to reserve space.
Loading Attribute
Use loading="lazy" on all images that are not visible immediately when the page loads.
Responsive Images
The srcset Attribute
Provide multiple image resolutions so the browser picks the right one for the device:
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w,
photo-800.jpg 800w,
photo-1200.jpg 1200w"
sizes="(max-width: 600px) 400px,
(max-width: 900px) 800px,
1200px"
alt="Responsive photo"
width="1200"
height="800"
>srcset— lists available images with their widths (wdescriptor)sizes— tells the browser how wide the image will be displayed at different screen sizes- The browser picks the most appropriate image automatically
The <picture> Element
Use <picture> for art direction — showing different images on different screen sizes or supporting modern formats with fallbacks:
Figure and Figcaption
Wrap images (or other media) with <figure> and add a caption with <figcaption>:
<figure>
<img
src="bar-chart.png"
alt="Bar chart showing monthly sales from Jan to Dec 2026, with December being highest at ₹8.4 crore"
width="800"
height="500"
>
<figcaption>
Figure 1: Monthly sales performance in 2026. Source: WoHoTech Analytics.
</figcaption>
</figure><figure> is a semantic element — it groups the image with its caption and signals that they belong together. Screen readers and search engines understand this relationship.
Image Formats Guide
| Format | Best Use | Pros | Cons |
|---|---|---|---|
| JPEG | Photos, complex images | Small file size | Lossy compression, no transparency |
| PNG | Screenshots, logos, icons | Lossless, transparency | Larger than JPEG for photos |
| SVG | Icons, logos, diagrams | Scalable, tiny file | Not suitable for photos |
| WebP | General use (modern browsers) | 25-35% smaller than JPEG/PNG | Older browsers don't support |
| AVIF | General use (cutting-edge) | 50% smaller than JPEG | Limited browser support |
| GIF | Simple animations | Wide support | Limited 256-colour palette, large |
Best practice: Use WebP with JPEG fallback via <picture> for photos. Use SVG for icons and logos.
Complete Navigation Example
A real navigation bar combining links and images:
<header>
<nav aria-label="Main navigation">
<a href="/" class="logo-link">
<img src="/logo.svg" alt="WoHoTech" width="120" height="40">
</a>
<ul role="list">
<li><a href="/subjects">Subjects</a></li>
<li><a href="/problems">Problems</a></li>
<li>
<a href="https://github.com/wohotech"
target="_blank"
rel="noopener noreferrer">
GitHub
<img src="/icons/external.svg" alt="(opens in new tab)" width="12" height="12">
</a>
</li>
</ul>
<a href="/signup" class="btn-primary">Sign Up Free</a>
</nav>
</header>Summary
| Feature | Element/Attribute | Key Notes |
|---|---|---|
| External link | <a href="https://..."> | Use rel="noopener noreferrer" with target="_blank" |
| Internal link | <a href="/path"> | Use root-relative paths for reliability |
| Anchor link | <a href="#id"> | Target element needs matching id |
| Email link | <a href="mailto:..."> | Opens email client |
| Download | <a href="..." download> | Forces file download |
| Image | <img src="..." alt="..."> | Always include meaningful alt |
| Lazy loading | loading="lazy" | Add to all below-fold images |
| Responsive | srcset + sizes | Browser picks best resolution |
| Art direction | <picture> + <source> | Different images for different screens/formats |
| Caption | <figure> + <figcaption> | Semantically associates image with description |
*Next: HTML Lists*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for HTML Links and Images — Anchor Tags, href, src, alt.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this HTML Complete Guide topic.
Search Terms
html-complete-guide, html complete guide, html, complete, guide, basics, links, and
Related HTML Complete Guide Topics