HTML Basics

The language that describes what's on a page and how it's structured, not how it looks.

What is it?

Before a page can be styled or made interactive, something has to say what's on it: a heading, a paragraph, a picture, a link. That description is written in HTML (HyperText Markup Language).

HTML doesn't handle colors, fonts, or spacing; that's CSS's job. HTML only says what each piece of content is and how the pieces relate: this is the main heading, this is a list, this text links to another page.

You write HTML with tags: short instructions in angle brackets, like <p> for a paragraph. Most tags come in pairs, an opening tag and a closing tag with content between them. Together they form an element. Tags can also carry attributes, extra details such as which image file an image tag should show.

Choosing tags that match the content's meaning (a heading tag for a heading, a button tag for a button) instead of generic tags is called semantic HTML. It lets screen readers, search engines, and other tools understand your page, not just display it.

Explain like I'm 10

HTML is like a document's labeled outline ('this is the title,' 'this is a bullet list,' 'this is a footnote'), written before anyone picks a font or color.

Examples

A minimal page

<!DOCTYPE html>
<html>
  <head>
    <title>My Page</title>
  </head>
  <body>
    <h1>Welcome</h1>
    <p>This is a paragraph of text.</p>
  </body>
</html>

Every HTML page has this shape: a declaration of the document type, a head with page metadata, and a body with the visible content.

Attributes and semantic elements

<img src="cat.jpg" alt="A sleeping orange cat">

<nav>
  <a href="/">Home</a>
  <a href="/about">About</a>
</nav>

<button type="submit">Send</button>

src and alt are attributes that give the image tag extra detail. nav and button are semantic elements: they tell the browser and assistive tools what role the content plays, not just how it looks.

How it works

The browser reads the HTML tags from top to bottom and builds a tree from them. Each element becomes a node, nested inside the element that contains it.

The browser renders from that tree, and JavaScript can change it later. The HTML text is just the starting blueprint.

Why does it exist?

HTML separates what content is from how it looks and how it behaves. With one simple language for structure and meaning, browsers, search engines, screen readers, and other tools can all read any page the same way, whoever built it and however it's styled.

When to use it

Every webpage needs HTML; it's the one layer that's never optional. Use the semantically correct tag whenever one exists: a button for a clickable action, a heading tag for a real heading, a list for a real list. Don't style a generic element to look the part.

When not to use it

Don't use HTML to control appearance (that's CSS) or behavior (that's JavaScript). Adding tags just to force a visual effect produces confusing, hard-to-maintain markup.

Common mistakes

  • Styling a generic element to look like a button instead of using a real button element, which breaks keyboard and screen-reader support.

  • Forgetting to close tags, so the browser misreads how content is nested.

  • Treating an image's alt attribute as optional decoration instead of a real description that screen readers use.

Practice exercises

  1. Easy:

    Write a minimal HTML page with a title, one heading, and one paragraph.

  2. Medium:

    Build a simple page with a navigation bar (nav), a list of three items, and an image with meaningful alt text.

  3. Hard:

    Take a page built only from generic div elements and rewrite it with semantic tags (header, nav, main, button, footer) where they fit. Explain each change.

Interview questions

What is HTML responsible for, and what is it not responsible for?

HTML describes the structure and meaning of content: headings, paragraphs, links, images. Visual styling is CSS's job, and interactive behavior is JavaScript's.

What's the difference between a tag, an element, and an attribute?

A tag is the bracketed instruction, like <p> or </p>. An element is the opening tag, its content, and its closing tag together. An attribute is extra information inside an opening tag, such as src or href, that tells the browser more about the element.

What is semantic HTML, and why does it matter?

Semantic HTML uses tags that describe what content is (<nav> for navigation, <button> for a clickable action) instead of generic tags styled to look the part. Screen readers, search engines, and browsers use that meaning to decide how to present or announce the content.

What does the `<!DOCTYPE html>` declaration do, and what happens if it's missing?

It tells the browser to use the modern HTML standard. Without it, some browsers fall back to quirks mode, an older, less consistent mode kept for backward compatibility. That can subtly change things like how box sizing is calculated.

What's the difference between the `<head>` and `<body>` of a document?

The <head> holds information about the page (title, linked stylesheets, metadata) and isn't shown as content. The <body> holds everything the user sees.

Why does the `alt` attribute on an `<img>` matter, beyond being read aloud by screen readers?

It's shown in place of the image if the file fails to load. Search engines also use it to understand the image, since they can't 'see' the pixels.

What is a void element, and why don't tags like `<img>` or `<br>` have closing tags?

A void element can never have content or children, so HTML doesn't need or allow a closing tag. <img src="cat.jpg"> is complete on its own; there's nothing to wrap.

If you forget to close a `<p>` tag before opening another one, does the browser throw an error?

No. HTML parsers are deliberately forgiving and follow a defined error-recovery algorithm. Some elements, like <p>, close automatically when an incompatible tag appears. The page still renders, just maybe not with the structure you meant.

Given `<ul><li>A<li>B</ul>` with no closing `</li>` tags, how many list items does the browser actually render?

Two. The parser's error-recovery rules close an <li> as soon as another <li> starts, so the missing closing tags are inferred. No items are merged or dropped.

Why is a `<div>` with a click handler a worse choice than an actual `<button>` for a clickable action?

A <button> comes with keyboard support for free: you can reach it with Tab, activate it with Enter or Space, and screen readers announce it as a button. A plain <div> has none of that; you'd have to rebuild focus, keyboard activation, and the accessible role yourself.

What's the difference between an element's `id` and its `class` attribute?

An id identifies one unique element on the page. A class is reused across many elements that share a role or style. id is also used for in-page links (#section) and is usually the fastest way to look up an element.

What happens if two elements on a page share the same `id`?

The HTML is invalid, but the browser still renders it. document.getElementById and CSS #id selectors match only the first one and silently ignore the duplicate, so the bug is easy to miss.

What's the difference between a block-level element and an inline element?

A block-level element, like <p> or <div>, starts on its own line and takes the full available width by default. An inline element, like <span> or <a>, flows within the text and is only as wide as its content. CSS's display property can override this for any element.

Why does heading order (`<h1>` through `<h6>`) matter, beyond making text bigger?

Screen reader users jump between headings to navigate a page, the way sighted users scan section titles. Skipping levels, or using headings just for their font size, breaks that navigation.

What does the default `type` of a `<button>` inside a `<form>` do, and why does it surprise people?

A <button> with no type defaults to type="submit", so clicking it submits the form and reloads or navigates the page. If you just want the button to run some JavaScript, set type="button" explicitly.

What's the difference between hiding content with an HTML comment versus hiding it with CSS?

Content inside an HTML comment (<!-- ... -->) is never parsed into the page; it doesn't exist as an element. Content hidden with CSS is still a real element, just not shown (and, depending on the technique, may still affect screen readers or layout).

You need a piece of text that, when clicked, takes the user to a different page. Should you use `<a>` or `<button>`?

<a href="...">. Anchors are for navigation: they update the URL, support right-click and open-in-new-tab, and are announced as links. <button> triggers an action on the current page, like submitting a form or opening a modal, and has no navigation behavior.

Why does the browser build a tree out of HTML tags instead of just keeping the raw text around?

A tree shows exactly which elements are nested inside which. That lets the browser (and later JavaScript) work out layout, apply styles based on structure, and target single elements. None of that works on a flat string of text.

What's wrong with writing an entire page's content using only `<div>` and `<span>` elements?

It renders fine, but the page loses all semantic meaning. Screen readers can't find headings, landmarks, or buttons, search engines can't tell what's important, and other developers lose the hints that tags like <nav> or <h1> give.