Links (<a>) and images (<img>) form the foundational hypermedia fabric of the World Wide Web. Modern web development demands high performance, zero layout shift (CLS), mobile responsiveness, and security hardening.
Hyperlinks: Security & Tabnabbing Protection
The anchor element creates hyperlinks via the href attribute:
<!-- Standard Internal Link -->
<a href="/courses/javascript">Explore JavaScript Course</a>
<!-- Document Fragment Anchor (Smooth scrolling to section ID) -->
<a href="#curriculum-section">Jump to Curriculum</a>
<!-- Telephone & Email Protocols -->
<a href="tel:+18005550199">Call Support</a>
<a href="mailto:contact@codeacademix.dev?subject=Inquiry">Email Admissions</a>
<!-- Secure External Link with Reverse Tabnabbing Protection -->
<a href="https://github.com" target="_blank" rel="noopener noreferrer">
View GitHub Repository
</a>
CRITICAL SECURITY NOTE: Reverse Tabnabbing When opening external links with
target="_blank", the target page gains partial access to your page via the JavaScriptwindow.openerobject. A malicious external site can silently redirect your original tab to a phishing clone usingwindow.opener.location = "https://phishing.site". Always includerel="noopener noreferrer"when usingtarget="_blank"(modern browsers do this by default, but explicit declaration remains enterprise best practice).
The Modern <img> Element: Zero-CLS & Native Lazy Loading
Images represent the largest payload on most web pages. Unoptimized images destroy Google Core Web Vitals (Largest Contentful Paint and Cumulative Layout Shift):
<img
src="/images/hero-laptop.jpg"
alt="Student writing TypeScript code in CodeAcademix in-browser IDE"
width="1200"
height="675"
loading="lazy"
decoding="async"
fetchpriority="high"
>
- Explicit
widthandheight: Modern browsers use these attributes to calculate the aspect ratio ($1200 / 675 = 16:9$) before the image bytes download, reserving physical layout space and completely eliminating Cumulative Layout Shift (CLS)! loading="lazy": Defers image download until the user scrolls within proximity of the viewport, drastically reducing initial page load weight.decoding="async": Decodes image raster pixels off the main browser UI thread, preventing scroll hitching.fetchpriority="high": Tells the browser preload scanner to prioritize the LCP hero image above other assets.
Resolution Switching: srcset and sizes
Mobile phones with Retina/OLED displays (2x or 3x device pixel ratio) require higher-density images, while desktop screens on cellular connections need appropriately sized downloads:
<img
src="/images/card-fallback.jpg"
srcset="
/images/card-400w.jpg 400w,
/images/card-800w.jpg 800w,
/images/card-1200w.jpg 1200w
"
sizes="(max-width: 640px) 100vw, (max-width: 1024px) 50vw, 33vw"
alt="Interactive coding challenge preview"
width="800"
height="450"
loading="lazy"
>
The browser evaluates the screen's physical pixel density and current layout width, automatically downloading the smallest optimal image byte size!
Format Switching & Art Direction with <picture>
Next-generation image formats like AVIF and WebP provide 30% to 50% smaller file sizes than legacy JPEG at identical perceptual quality. The <picture> element allows seamless fallback for older browsers:
<picture>
<!-- 1. Ultra-compressed modern AVIF format -->
<source srcset="/images/hero.avif" type="image/avif">
<!-- 2. Highly efficient WebP format -->
<source srcset="/images/hero.webp" type="image/webp">
<!-- 3. Universal JPEG Fallback -->
<img
src="/images/hero.jpg"
alt="Developer dashboard interface"
width="1600"
height="900"
>
</picture>
Accessible Alt Text Rules
- Informative Images: The
alttext must convey the essential message, not just describe pixels. - Bad:
alt="Image of a button" - Good:
alt="Submit registration application" - Decorative Images: If an image is purely decorative (e.g., background swirl, flourish), use an empty
alt="". This tells screen readers to skip the element cleanly. Omitting thealtattribute entirely causes screen readers to read out the full image URL filename!
Exercises
- Build a responsive picture element offering AVIF, WebP, and PNG fallbacks for a brand logo.
- Implement an image with
srcsetandsizesthat serves an 800px image on mobile and a 1600px image on high-DPI desktop displays. - Explain why specifying
widthandheightattributes on an<img>prevents Cumulative Layout Shift (CLS) even when CSS overrides the display size withwidth: 100%; height: auto;.