โšกLearnIT
Fundamentals ยท Topic 1

๐Ÿ“„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 ignore width/height and vertical margins.
  • inline-block flows inline but respects width/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: GET puts data in the URL query string (searches, bookmarkable, not for secrets); POST puts 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 -->
AttributeDownloadExecutionOrder preserved?Use for
(none)Blocks parserImmediatelyYesRarely; blocks render
asyncParallelAs soon as readyNoIndependent scripts (analytics)
deferParallelAfter DOM parsed, before DOMContentLoadedYesApp 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.
  • alt on images โ€” describe the content; use alt="" 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

CookieslocalStoragesessionStorage
Capacity~4 KB~5โ€“10 MB~5โ€“10 MB
Sent to serverYes, every requestNoNo
ExpiryConfigurableNever (until cleared)On tab close
AccessJS (unless HttpOnly)JS onlyJS only
Use forAuth/session tokensPersistent client prefsPer-tab temporary state
  • HttpOnly cookies can't be read by JS โ€” the standard defense for auth tokens against XSS.
  • Secure sends the cookie only over HTTPS; SameSite mitigates CSRF.
  • localStorage is synchronous and string-only (JSON.stringify your 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 (or aspect-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.
  • robots meta โ€” noindex/nofollow to 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 questions

1What 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?

Answered 0/5