๐HTML Fundamentals
HTML is the structure layer of the web. Interviewers use it to check whether you understand semantics, accessibility, forms, and how the browser actually loads a document โ not whether you can memorize every tag.
1.1Semantic HTML & Why It Matters
Semantic HTML means using elements that describe the meaning of content, not just its appearance. <header>, <nav>, <main>, <article>, <section>, <aside>, <footer>, <figure> โ versus wrapping everything in <div> ("div soup").
Why it matters:
- Accessibility โ screen readers build a navigable landmark map from semantic tags. A
<nav>is announced as navigation; a<div class="nav">is not. - SEO โ search engines weight content in
<main>/<article>and use headings to understand structure. - Maintainability โ the markup is self-documenting.
<!-- Div soup -->
<div class="header"><div class="nav">โฆ</div></div>
<div class="main"><div class="article">โฆ</div></div>
<!-- Semantic -->
<header><nav>โฆ</nav></header>
<main><article>โฆ</article></main>
Heading order matters: exactly one <h1> per page (the page's topic), and don't skip levels (<h1> โ <h3>) โ assistive tech uses the outline.
Interviewers probe: "What's the difference between <section> and <div>?" โ <section> is a thematic grouping that should have a heading; <div> carries no meaning and is a pure styling/scripting hook.
1.2The Document, DOCTYPE & `<head>`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Page Title</title>
<meta name="description" content="Summary for search results" />
</head>
<body>โฆ</body>
</html>
<!DOCTYPE html>โ triggers standards mode. Without it, browsers fall back to quirks mode (legacy, buggy box model). It is not an HTML tag; it's an instruction to the parser.lang="en"โ helps screen readers pick the right pronunciation and helps translation tools.<meta charset="UTF-8">โ must appear early so bytes are decoded correctly.- The viewport meta โ essential for responsive design; without it, mobile browsers render at a fake 980px width and zoom out.
1.3Block vs Inline (and `inline-block`)
- Block elements (
<div>,<p>,<h1>,<section>) start on a new line and take full available width. Width/height/margins all apply. - Inline elements (
<span>,<a>,<strong>,<em>) flow within text, take only as much width as their content, and ignorewidth/heightand vertical margins. inline-blockflows inline but respectswidth/height/margins โ useful for things like nav items.
This is really a CSS display concept, but it originates from an element's default HTML behavior.
1.4Forms & Input Types
Forms are the highest-value HTML topic for interviews because they touch UX, validation, and accessibility.
<form action="/submit" method="post">
<label for="email">Email</label>
<input id="email" name="email" type="email" required autocomplete="email" />
<label for="pw">Password</label>
<input id="pw" name="pw" type="password" minlength="8" required />
<button type="submit">Sign in</button>
</form>
Key points:
<label>+for/idโ clicking the label focuses the input and screen readers announce it. Always pair them (or wrap the input in the label).nameโ the key sent to the server; without it, the field isn't submitted.- Input types carry semantics + mobile keyboards + free validation:
email,number,tel,url,date,search,password,file,checkbox,radio,range,color. - Native validation attributes:
required,minlength/maxlength,min/max,pattern,type="email". Style with:valid/:invalid. method:GETputs data in the URL query string (searches, bookmarkable, not for secrets);POSTputs it in the body (mutations, sensitive data).
<button type> gotcha: inside a form, a <button> defaults to type="submit". Use type="button" for buttons that shouldn't submit (e.g. a "show password" toggle).
1.5Loading Scripts: `async` vs `defer`
Where and how you load JS dramatically affects performance.
<script src="app.js"></script> <!-- blocks parsing while it downloads + runs -->
<script src="app.js" async></script> <!-- downloads in parallel, runs ASAP (order not guaranteed) -->
<script src="app.js" defer></script> <!-- downloads in parallel, runs after HTML parse, in order -->
| Attribute | Download | Execution | Order preserved? | Use for |
|---|---|---|---|---|
| (none) | Blocks parser | Immediately | Yes | Rarely; blocks render |
async | Parallel | As soon as ready | No | Independent scripts (analytics) |
defer | Parallel | After DOM parsed, before DOMContentLoaded | Yes | App code that needs the DOM |
Rule of thumb: put <script defer> in the <head>, or a plain <script> at the end of <body>. defer is the safe default for app bundles.
1.6Accessibility (a11y) Essentials
- Semantic tags first โ a real
<button>is focusable, keyboard-activatable, and announced correctly. A<div onclick>is none of these. alton images โ describe the content; usealt=""for purely decorative images so they're skipped.- ARIA โ
aria-label,aria-labelledby,aria-describedby,role,aria-expanded,aria-hidden. First rule of ARIA: don't use ARIA if a native element does the job. - Keyboard navigation โ everything interactive must be reachable and operable with
Tab/Enter/Space. Manage focus in modals. - Labels & names โ every form control needs an accessible name (label,
aria-label, etc.). - Color contrast โ WCAG AA needs 4.5:1 for normal text.
1.7`data-` Attributes
Custom attributes to attach data to elements without abusing class or id:
<button data-product-id="42" data-price="9.99">Buy</button>
const el = document.querySelector("button");
el.dataset.productId; // "42" (camelCased)
el.dataset.price; // "9.99"
Great for hooking JS behavior or passing small bits of state to the DOM. Don't store large or sensitive data here โ it's visible in the markup.
1.8Storage: Cookies vs localStorage vs sessionStorage
| Cookies | localStorage | sessionStorage | |
|---|---|---|---|
| Capacity | ~4 KB | ~5โ10 MB | ~5โ10 MB |
| Sent to server | Yes, every request | No | No |
| Expiry | Configurable | Never (until cleared) | On tab close |
| Access | JS (unless HttpOnly) | JS only | JS only |
| Use for | Auth/session tokens | Persistent client prefs | Per-tab temporary state |
HttpOnlycookies can't be read by JS โ the standard defense for auth tokens against XSS.Securesends the cookie only over HTTPS;SameSitemitigates CSRF.- localStorage is synchronous and string-only (
JSON.stringifyyour objects).
1.9Images, Media & Responsive Assets
srcset+sizesโ serve the right resolution per device, saving bandwidth:<img src="small.jpg" srcset="small.jpg 480w, large.jpg 1080w" sizes="(max-width: 600px) 480px, 1080px" alt="โฆ" /><picture>โ art direction / modern formats with fallback:<picture> <source srcset="hero.avif" type="image/avif" /> <source srcset="hero.webp" type="image/webp" /> <img src="hero.jpg" alt="โฆ" /> </picture>loading="lazy"โ defer offscreen images until near the viewport.- Always set
width/height(oraspect-ratio) to reserve space and avoid layout shift (CLS).
1.10Meta, SEO & Open Graph
<title>and<meta name="description">โ what shows in search results.- Open Graph (
og:title,og:image,og:description) โ controls link previews on social platforms. - Canonical (
<link rel="canonical">) โ tells search engines the primary URL to avoid duplicate-content penalties. robotsmeta โnoindex/nofollowto control crawling.
Interviewers probe: "How do you make a single-page app SEO-friendly?" โ server-render (SSR/SSG) so crawlers get real HTML, set proper titles/meta per route, and use semantic markup.
๐ง Quiz: HTML Fundamentals
5 questions1What does the <!DOCTYPE html> declaration do?
2Which script-loading attribute downloads in parallel AND preserves execution order after HTML is parsed?
3Why prefer a native <button> over a <div onclick>?
4Which storage is automatically sent to the server on every request?
5What links a <label> to its input for accessibility?