Loading

T1 HTML · Lesson 2

Elements and the structure of a page

Tags, elements and nesting; the skeleton every page has; headings, paragraphs, emphasis and importance; grouping with div and span; and attributes, ids and comments.

  • say what a tag, an element and its content are, and nest elements correctly
  • write the skeleton every page has: doctype, html, head, title and body
  • mark up text with headings, paragraphs, line breaks, em and strong
  • group parts of a page with div and span, and tell block from inline
  • add attributes, give a part a unique id, and leave comments
About 60 minutesChecked against Node.js on the laptop (the kit runs every code block with it) 22.23.1 (2026-10-26)Play this lesson's games

The back cover

A web page is plain text with labels around its parts. The labels, called tags, tell the browser what each part is: a heading, a paragraph, a word that matters. The browser reads them and draws the page.

These pages show how tags wrap content into elements, and how elements fit inside one another like boxes. They show the skeleton every page has, the ladder of headings, and the difference between a word that is important and a word that is stressed.

Then come two boxes with no meaning of their own, div and span, for grouping things. Last come attributes: small details written inside a tag, such as a unique name or a language.

Before you start

The story: Build one that stands

The Masons' Hall smells of stone dust and oil. Master Tobin sets a box of pieces in front of you: frame stones, heading stones, plain blocks for text.

"A panel is not painted," he says. "It is built. Every piece sits inside another, like the walls of a house inside its plot." He points to a panel on the wall. It is a mess: a heading the size of a door, text running off the edge, a piece left open at one end.

"Corin built that one," Tobin says. Corin waves from across the hall. "Build me one that stands," says Tobin.

Lesson 1 followed a page on its journey: the address, DNS, the request, and the response whose body was the page itself. This lesson opens that body and reads it. You will write your first real web page, part by part, and see each part appear as you add it. It builds on these ideas; each has a tiny example.

What you already knowA tiny exampleWhere it comes back
A response has a body.The tea stall's /menu answered with the text Chai 15, Samosa 20.A web page's body is written in HTML, the language of this lesson.
Content-Type says what the body is.text/html means "this body is a web page".Everything you write here would travel with that header.
A letter has fixed parts.The address at the top, the date, a greeting, the body, a signature.A web page has fixed parts too, in a fixed order.
Labels tell you what something is.A jar labelled Sugar, a file labelled Bills 2026.HTML labels each piece of a page: this is a heading, this is a paragraph.
Boxes go inside boxes.A tiffin carrier: one carrier, several boxes, food inside each box.Every part of a page sits inside another part.
A book has chapter titles and section titles.Chapter 3, then 3.1, then 3.2.Pages have headings of six levels, from the biggest to the smallest.
Bold and italic in a message.*urgent* in a chat becomes bold.HTML marks words as important or stressed.
Name tags must be unique in a class.Two students called Asha get Asha P. and Asha K.An id names one part of a page, and only one.
Notes in the margin.A pencil note beside a sum, not part of the answer.HTML comments: notes the visitor never sees.
Run and Edit from T0.Run shows what code makes; Edit lets you change it.Here Run shows the page your HTML makes.

By the end of this lesson you will be able to:

  • say what a tag, an element and its content are, and write elements inside elements;
  • write the skeleton every page has: the doctype, html, head, title and body, and say why each is there;
  • mark up text with headings, paragraphs, line breaks, emphasis and strong importance;
  • group parts of a page with div and span, and say how the two differ;
  • add attributes to elements, give a part a unique id, and leave comments for yourself.

In one line

This lesson opens the body of a response and writes it yourself: you need only lesson 1's journey of a page, and the habit of labelling things.

Tags, elements and what goes inside them

At a glance

HTML, the HyperText Markup Language, is plain text with labels. An opening tag is a name between angle brackets, such as <h1>. Then comes the content, and then a closing tag with a slash, </h1>. The three together make an element: here, a heading.

Elements can sit inside other elements, like boxes inside boxes, and the inner one must close first. A few elements, such as <br> for a line break, hold nothing at all, so they have no closing tag. The tags tell the browser what each part is; they do not change the words inside.

Take a guess: If the slash is forgotten in a closing tag, does the heading end, or does a second heading begin?

Picture a kirana shop's shelf. A jar on it has a label, Sugar, and inside the jar is the sugar itself. The label does not change the sugar; it tells everyone what the sugar is. HTML works the same way: it puts labels around the words of a page, so that the browser knows what each piece is.

HTML

HyperText Markup Language: the language every web page is written in. Markup means labels added around content. HyperText means text with links that jump to other text.

Here is a complete piece of HTML. Press Run: the browser reads it and shows the page it describes, in a frame below the code.

<h1>Asha Stores</h1>

Checked in Chrome

  • there is a main heading
  • it reads Asha Stores

Read it piece by piece.

PieceRead it asWhat it is
<h1>the opening tag h1an opening tag: a name between angle brackets; h1 means "heading, level 1"
Asha Storesthe contentthe content: the words the label is around
</h1>the closing tag h1a closing tag: the same name with a slash first, saying "the heading ends here"

The whole thing, opening tag, content and closing tag together, is called an element: the h1 element. People often say "tag" when they mean the element; strictly, the tags are only the two labels at either end.

A common mistake

Forgetting the slash in the closing tag. <h1>Asha Stores<h1> opens a second heading instead of closing the first. Every closing tag starts with </.

Elements inside elements

An element's content can be text, or other elements, or both. This is called nesting, like boxes inside a tiffin carrier. The rule is that an element must close inside the element that contains it: the inner box shuts before the outer one.

<p>Fresh <strong>tomatoes</strong> every morning.</p>

Checked in Chrome

  • the strong element is inside the paragraph
  • only the word tomatoes is strong

The <p> element is a paragraph. Inside it is plain text, then a <strong> element around one word, then more text. The strong element opens and closes inside the paragraph: <p> ... <strong> ... </strong> ... </p>. The browser shows tomatoes in bold, because strong means "this is important".

Correct nestingWrong nesting
<p>Fresh <strong>tomatoes</strong> today.</p><p>Fresh <strong>tomatoes</p> today.</strong>
the inner element closes first, then the outerthe outer element closes while the inner one is still open

Browsers are forgiving: they try to repair wrong nesting rather than show an error. But their repair is a guess, and a guess can put text where you did not mean it. Writing the nesting correctly is the only way to be sure what the browser builds.

Elements with nothing inside

A few elements never have content, so they have no closing tag. They are called empty elements (or void elements). The line break, <br>, is one: it means "start a new line here", and there is nothing to put inside it.

<p>Asha Stores<br>Shop 4, MG Road<br>Pune 411001</p>

Checked in Chrome

  • two line breaks
  • the address shows on three lines

An address is a good use of <br>: it is one piece of text, one paragraph, that must break onto several lines. You may see <br /> with a slash, in older pages; today both forms are the same.

Worked example 1

Write the HTML for a heading that says Today's Offers, followed by a paragraph that says Basmati rice is 10% off, with only 10% off marked as important.

What we have: two pieces of text, one heading and one paragraph, and one phrase that matters most

What we need: the elements, correctly nested

The plan: an h1 for the heading; a p for the paragraph; a strong around 10% off, inside the p

<h1>Today's Offers</h1>
<p>Basmati rice is <strong>10% off</strong></p>

Checked in Chrome

  • the heading reads Today's Offers
  • 10% off is strong, inside the paragraph
  • The heading and the paragraph sit one after the other; they are siblings, at the same level.
  • The strong element opens and closes inside the paragraph.
  • An h1, then a p containing a strong around 10% off.

    Check: every opening tag has its closing tag, and the strong element closes before the paragraph does.

    Worked example 2

    A friend's page shows the whole rest of the paragraph in bold. Here is the HTML. Find the slip.

    What we have: a paragraph whose bold runs on too far

    What we need: the missing piece

    The plan: pair every opening tag with its closing tag

    <p>Sale on <strong>sugar today. All other prices as usual.</p>

    Checked in Chrome

    • (the slip shows: strong holds more than the word sugar)
  • The <strong> opens before sugar and never closes, so everything after it is strong.
  • <p>Sale on <strong>sugar</strong> today. All other prices as usual.</p>

    Checked in Chrome

    • only the word sugar is strong

    Close the strong element after the word: <strong>sugar</strong>.

    Check: now the browser's strong element holds one word, which is what the page meant.

    When you are stuck: tags and elements

    Read the HTML out loud as "open p, text, close p". Every open needs its close.

    For nesting, check that the last element you opened is the first one you close.

    If the page looks wrong, find where a style (bold, a heading's size) starts running on: an element opened there and did not close.

    Your turn

    What are the three parts of the element <p>Open daily</p>?Tap to reveal
    1. An opening tag, the content, a closing tag.

    The opening tag <p>, the content Open daily, and the closing tag </p>.

    Why does <br> have no closing tag?Tap to reveal
    1. What could go inside a line break?

    It is an empty element: it never has content, so there is nothing to close.

    Is this nesting correct: <p><strong>Hot</strong> chai</p>?Tap to reveal
    1. Which element was opened last, and which is closed first?

    Yes: strong opens inside p and closes before p closes.

    Did you know?

    The first HTML had 18 elements

    The first public description of HTML was a document called HTML Tags, first mentioned on the internet by Tim Berners-Lee in late 1991. It described 18 elements. Its opening lines already set the rule used today: each tag starts with a less-than sign and ends with a greater-than sign, and a closing tag is the same with a slash after the opener. Eleven of those first elements were still part of HTML 4.

    Why it fits here: The opening tag, the closing tag with its slash, and the content between them are exactly the parts this topic names.

    Sources: Wikipedia: HTML; W3C archive: HTML Tags, the first list of tags

    Markup began with an editor's pen

    The word markup is older than computers. Editors used to write instructions on a writer's manuscript, traditionally with a red pen or a blue pencil, so that the printer knew how each part should look. A dictionary still gives that meaning: the collection of detailed stylistic instructions written on a manuscript that is to be typeset. HTML's tags are markup of the same kind, written for a browser in place of a printer.

    Why it fits here: A tag is a label around content that says what the content is, as an editor's mark did on paper.

    Sources: Wikipedia: Markup language; American Heritage Dictionary: markup

    In one line

    An element is an opening tag, its content and a closing tag (with a slash); elements nest like boxes, the inner closing first, and empty elements such as br have no content and no closing tag.

    The skeleton every page has

    At a glance

    Every page has the same skeleton. It starts with the document type declaration, <!DOCTYPE html>, which says that this is a modern HTML page. Then the html element holds everything, and its lang attribute names the language. Inside it, the head holds information about the page, such as the meta charset line and the title shown in the tab, and the body holds the page itself.

    Indentation shows which box sits inside which. The browser squeezes a run of spaces and line breaks into one space, and comments are notes in the source that the page does not show.

    Take a guess: What happens to five spaces typed between two words in HTML?

    Every formal letter has the same parts in the same order: the sender's address, the date, the greeting, the body, the signature. Nobody invents a new order for each letter, because a fixed order lets every reader find each part at once. Every web page has a fixed skeleton in the same way.

    A complete page

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Asha Stores</title>
      </head>
      <body>
        <h1>Asha Stores</h1>
        <p>Fresh vegetables every morning.</p>
      </body>
    </html>

    Checked in Chrome

    • the browser uses its modern (standards) mode
    • the tab's title is Asha Stores
    • the page says it is in English
    • the characters are read as UTF-8
    • the heading is in the body
    LineWhat it doesWhy it is there
    <!DOCTYPE html>the document type declaration: it says "this is a modern HTML page"without it, browsers fall back to an old way of showing pages, called quirks mode, and some layouts break
    <html lang="en">the html element, which holds the whole page; lang says its languageeverything else goes inside it; screen readers use lang to pronounce the words correctly
    <head>the head: information about the page, not shown on itthe browser, search engines and the tab read it
    <meta charset="utf-8">says which alphabet of characters the page is written inso that ₹, é and Devanagari letters show correctly instead of odd symbols
    <title>the title: the name in the browser tab, in bookmarks and in search resultsa page without one shows its address in the tab instead
    <body>the body: everything the visitor seesevery heading, paragraph, picture and link goes here

    Head and body

    The head is about the page (its title, its alphabet, later its styles). The body is the page itself. A common way to remember: the head thinks, the body shows.

    Indentation: showing the boxes

    Look at the spaces at the start of each line. The head and body are pushed in two spaces from html, and their contents two more. This is indentation, and it shows the nesting at a glance: each step to the right is one box deeper. The browser ignores it completely; it is there for people.

    Whitespace: the browser squeezes spaces

    Spaces, tabs and line breaks in HTML are called whitespace. The browser treats a run of whitespace as a single space. So pressing Enter in your HTML does not start a new line on the page, and ten spaces show as one.

    <p>Chai     15 rupees.
       Samosa     20 rupees.</p>

    Checked in Chrome

    • the extra spaces and the line break shrink to single spaces

    To start a new line inside a paragraph, use <br>; to start a new block of text, use a new paragraph. Never rely on spaces or Enter in the HTML to lay out a page; the CSS step teaches layout.

    Comments: notes only you see

    A comment is a note in the HTML that the browser does not show. It starts with <!-- and ends with -->. Use comments to explain why something is there, or to switch a part off for a while, just like // in JavaScript.

    <!-- the prices change every Monday -->
    <p>Chai 15 rupees</p>
    <!-- <p>Biscuits 10 rupees</p> -->

    Checked in Chrome

    • only one paragraph shows; the second is inside a comment
    • the note itself is not shown

    Comments are not secret

    A comment is hidden from the page, not from people. Anyone can open the page's source (Ctrl + U in Chrome) and read every comment. Never put a password, a key or a private note in an HTML comment.

    Worked example 3

    A page shows its web address in the browser tab instead of a name. What is missing, and where does it go?

    What we have: a tab that shows the address

    What we need: the missing element and its place

    The plan: recall what the tab shows, and which part of the skeleton holds it

    1. The tab shows a page's title.
    2. With no title element, the browser has nothing else to show but the address.
    3. The title belongs in the head, because it is information about the page.

    A title element inside the head, such as <title>Asha Stores</title>.

    Check: the complete page above has its title in the head, and its check confirms the tab reads Asha Stores.

    Worked example 4

    Write the skeleton of a page for a school results portal: the title Results 2026, and a main heading Class 10 Results.

    What we have: the title and the heading

    What we need: a complete page with every part of the skeleton

    The plan: start from the doctype, then html, head with meta and title, body with the heading; indent each level

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Results 2026</title>
      </head>
      <body>
        <h1>Class 10 Results</h1>
      </body>
    </html>

    Checked in Chrome

    • the tab reads Results 2026
    • the heading reads Class 10 Results
    • standards mode

    The six parts in order: doctype, html, head (meta, title), body (h1).

    Check: the title and the heading are different on purpose: the title names the page in the tab, the heading starts its content.

    When you are stuck: the skeleton

    Copy the complete page above and change only the title and the body; the rest is the same on every page.

    If the tab shows the address, the title is missing or outside the head.

    If letters such as ₹ show as odd symbols, the meta charset line is missing.

    Your turn

    Which part of the page, head or body, does the title belong in, and why?Tap to reveal
    1. The title is about the page; it is not shown on the page itself.

    The head: it holds information about the page.

    What does the browser show for <p>One two</p>?Tap to reveal
    1. A run of whitespace counts as one space.

    One two, with one space.

    Why is an HTML comment the wrong place for a password?Tap to reveal
    1. Who can see a page's source?

    Because anyone can read the source, comments included.

    Did you know?

    Quirks mode keeps the old web working

    Browsers have more than one way of drawing a page. Without a doctype, they fall back to quirks mode, which copies the behaviour of Netscape Navigator 4 and Internet Explorer 5, to support websites built before web standards were widely adopted. With a full doctype, they use no-quirks mode, which follows the modern HTML and CSS standards. So one short first line decides which rules the whole page is drawn by.

    Why it fits here: The doctype of this topic is that switch: without it, a modern page can be drawn by the old rules and break.

    Sources: MDN: Quirks mode and standards mode; Wikipedia: Quirks mode

    The tags that may be left out

    The HTML standard allows some tags of the skeleton to be left out. An html element's start tag can be omitted if the first thing inside it is not a comment, and its end tag can be omitted if no comment comes right after it. Leaving the tags in keeps the boxes visible to every reader of the code, which is why these lessons always write them.

    Why it fits here: The skeleton of this topic is there either way; writing every tag makes the nesting plain to the next person who reads the page.

    Source: HTML Standard (WHATWG): the html element

    In one line

    Every page is a doctype, then html holding a head (about the page: meta charset, title) and a body (the page itself). Indentation shows the nesting, whitespace shrinks to one space, and comments are notes anyone can read in the source.

    Headings, paragraphs and words that matter

    At a glance

    HTML has six headings, h1 to h6. The h1 is the page's main heading, like a chapter title; h2 marks a section, h3 a part of a section, and so on down to h6. Together they make an outline of the page, like a table of contents: one h1, and no level skipped.

    A p element holds one paragraph. Inside the text, strong marks words that are important, and em marks the word that is stressed, as when it is said a little louder. Each is chosen for its meaning, and the look is left to CSS.

    Take a guess: If an h2 looks too big, should it become an h4, or should CSS make it smaller?

    Open any textbook. The chapter title is the biggest; each section title is smaller; each sub-section smaller again. Your eye uses those sizes to find its way. HTML gives a page the same ladder of headings, and it gives the words between them paragraphs.

    Six levels of heading

    There are six heading elements, <h1> to <h6>. <h1> is the page's main heading, like a chapter title; <h2> is a section, <h3> a part of a section, and so on down to <h6>.

    <h1>Asha Stores</h1>
    <h2>Vegetables</h2>
    <h3>Leafy greens</h3>
    <p>Spinach, methi and coriander, picked this morning.</p>
    <h2>Groceries</h2>
    <p>Rice, dal and atta, by the kilo.</p>

    Checked in Chrome

    • one main heading
    • two sections
    • a part of the first section
    • the h1 is bigger than the h2

    The headings form an outline of the page, like a table of contents: Asha Stores, then Vegetables with Leafy greens inside it, then Groceries. Search engines read that outline to understand the page, and people who use screen readers jump from heading to heading to find what they need.

    A common mistake

    Choosing a heading level for its size. If an <h2> looks too big, the answer is CSS, which changes how it looks, not an <h4>, which changes what it means. Keep one <h1> per page, and never skip a level going down (an h2 is followed by h3, not h5).

    Paragraphs

    A paragraph, <p>, holds one block of text. Each paragraph starts on a new line, with a little space above and below it, which the browser adds for you. Several sentences that belong together are one paragraph; a new idea starts a new one.

    Strong and emphasis: meaning, not looks

    Two elements mark words within a sentence. <strong> means this is important: a warning, a key fact. <em> means stress this word, the way your voice leans on a word when you speak. Browsers show strong in bold and em in italics, but the looks are only a side effect; the meaning is what the elements are for.

    <p><strong>Closed on Sunday.</strong> Open <em>every</em> other day.</p>

    Checked in Chrome

    • the warning is strong
    • the stressed word is em
    • strong shows in bold
    • em shows in italics

    Read the sentence aloud with the stress on every and you hear what em adds: open every other day, not just some. A screen reader can change its voice for em and strong, which it cannot do for words that are only bold or italic in looks.

    ElementMeansUsually shown asUse it for
    <h1> to <h6>a heading of that levellarge bold text, smaller for each levelthe outline of the page
    <p>a paragrapha block of text with space around itrunning text
    <br>a line breaka new linelines that belong to one piece of text: an address, a poem
    <strong>this is importantboldwarnings, key facts
    <em>stress thisitalicsthe word your voice would lean on

    Worked example 5

    Turn this notice into HTML: a main heading Water Supply Notice, then a paragraph saying Water will be off on Tuesday from 10 to 2, with the time marked as important.

    What we have: a heading and a sentence with an important part

    What we need: the right elements, each for its meaning

    The plan: h1 for the notice's name; p for the sentence; strong around the time, because it is the fact people must not miss

    <h1>Water Supply Notice</h1>
    <p>Water will be off on Tuesday <strong>from 10 to 2</strong>.</p>

    Checked in Chrome

    • the notice's heading
    • the time is strong

    An h1, then a p with a strong around the time.

    Check: strong carries the meaning "important", which is why it fits a time people must remember.

    Worked example 6

    A page uses headings like this: h1 Asha Stores, then h4 Vegetables, then h4 Groceries, because h4 "looked the right size". What is wrong, and what is the fix?

    What we have: headings chosen for their size

    What we need: a correct outline

    The plan: the sections directly under the main heading are level 2

    <h1>Asha Stores</h1>
    <h2>Vegetables</h2>
    <h2>Groceries</h2>

    Checked in Chrome

    • the two sections are h2, with no level skipped
  • The outline jumped from 1 to 4, skipping 2 and 3: a screen reader user hears a page with missing sections.
  • The size is a job for CSS, in the next step.
  • Make the sections h2, and change their size with CSS if needed.

    Check: the outline now reads 1, 2, 2, with no gaps.

    When you are stuck: headings and text

    Ask what each piece is, not how it should look: a title, a section, a paragraph, an important fact.

    Check the outline: one h1, then levels going down one step at a time.

    Use strong for importance and em for stress; for looks alone, wait for CSS.

    Your turn

    How many h1 elements should a page have?Tap to reveal
    1. The h1 is the page's main heading, like a chapter title.

    One.

    Which element fits the word not in "Please do not park here", said with stress?Tap to reveal
    1. The voice leans on not; it is not a separate warning.

    <em>, for stress (or <strong> if the whole warning is the key fact).

    What comes after an h2 when you go one level down?Tap to reveal
    1. Levels go down one step at a time.

    An h3.

    Did you know?

    Screen reader users move by headings

    WebAIM regularly asks people who use screen readers, programs that read a page aloud, how they use the web. In its survey of December 2023 and January 2024, 1539 people answered. Asked what they do first to find information on a long page, 71.6% said they move through the headings. Only 6.4% said they read through the page from the top.

    Why it fits here: The outline of this topic is how many readers find their way. A page with clear headings is a page they can use.

    Source: WebAIM: Screen Reader User Survey #10 Results

    Readers scan in the shape of an F

    In April 2006 the usability expert Jakob Nielsen published eyetracking studies of how people read web pages. Their eyes often moved in an F-shaped pattern: two horizontal stripes followed by a vertical stripe. A study 11 years later confirmed it. His advice follows from it: start headings, paragraphs and lists with words that carry information, because those are the words a scanning eye notices.

    Why it fits here: Headings and short paragraphs, as this topic describes, are what a scanning eye catches first.

    Source: Nielsen Norman Group: F-Shaped Pattern For Reading Web Content

    In one line

    h1 to h6 build the page's outline (one h1, no skipped levels), p holds a paragraph, strong marks importance and em marks stress: choose each for its meaning, and leave the looks to CSS.

    Grouping with div and span

    At a glance

    Sometimes a group of things needs a box with no meaning of its own. The div element, short for division, groups blocks. It starts on a new line and stretches across the width. The span element groups a few words inside a line of text and takes only the room they need. Neither tells the browser what its content is.

    So they are used only when no element with a meaning fits. A paragraph may hold only inline content. So a div never goes inside a p: the browser would end the paragraph early to make room for it.

    Take a guess: What does a browser do when it finds a div inside a paragraph?

    When you pack for a trip, you put the toiletries in one pouch and the chargers in another. The pouches add nothing to what is inside, but they keep things together, so you can move or find a whole group at once. HTML has two such pouches: <div> and <span>.

    div

    A division: a box that groups larger parts of a page (several paragraphs, a heading with its text). It has no meaning of its own; it only groups.

    span

    A small box inside a line of text, around a few words. Like div, it has no meaning of its own.

    Why group things with no meaning? Because the next steps will style and move groups: "make this whole offer card green" (CSS), "hide this box when the button is pressed" (JavaScript). They need a box to point at.

    <div>
      <h2>Offer of the day</h2>
      <p>Basmati rice, 5 kg, <span>₹650</span> instead of ₹720.</p>
    </div>
    <p>Prices include GST.</p>

    Checked in Chrome

    • the heading is inside the div
    • the price is a span inside the paragraph
    • the last paragraph is outside the div

    The div holds the offer's heading and paragraph together. The span holds just the new price, inside the sentence. The last paragraph is outside both, so it is not part of the offer.

    Block and inline

    The two boxes behave differently on the page. A div is a block element: it starts on a new line and stretches across the whole width, like a paragraph. A span is an inline element: it sits inside a line of text and takes only the room its words need, like strong and em.

    <p>Price: <span>₹650</span> only.</p>
    <p>Price: <div>₹650</div> only.</p>

    Checked in Chrome

    • a span is inline
    • a div is block
    • the browser ended the second paragraph early, where the div began

    Run it and look at the second line: the price jumps onto its own line. Worse, the browser had to repair the HTML: a paragraph may not contain a div, so it closed the paragraph just before the div, and the words "only." ended up in a paragraph of their own. The check confirms it: three paragraphs, where the HTML wrote two.

    Which box

    A div for a group of blocks: a card, a section of a page, a sidebar. A span for a few words inside a sentence: a price, a name, a date. Never put a div inside a p.

    Meaning first

    Before reaching for a div, ask whether an element with a meaning fits: a heading, a paragraph, and later section, article, nav, header and footer (lesson 6). Use div and span only when nothing with a meaning fits.

    Worked example 7

    Group a product card for a shop's page: the name Toor dal, 1 kg, a paragraph with the price ₹180, and a paragraph In stock. The price should be a span, so that it can be styled later.

    What we have: three pieces that belong together, one with a price inside

    What we need: a div around the card, a span around the price

    The plan: div for the group of blocks; the name as a heading; span around the price inside its paragraph

    <div>
      <h2>Toor dal, 1 kg</h2>
      <p>Price: <span>₹180</span></p>
      <p>In stock</p>
    </div>

    Checked in Chrome

    • the card holds three parts
    • the price is a span

    A div holding an h2 and two paragraphs, with a span around ₹180.

    Check: the div is a block holding blocks, and the span sits inside one line, as each box is meant to.

    When you are stuck: div and span

    Ask: is this a group of blocks (div) or a few words in a line (span)?

    Check that no div sits inside a p.

    Before a div, look for an element with a meaning that fits.

    Your turn

    Which is block and which is inline: div, span, p, strong?Tap to reveal
    1. Block elements start on a new line; inline ones sit inside a line.

    div and p are block; span and strong are inline.

    Why can a div not go inside a p?Tap to reveal
    1. A paragraph may hold only things that sit within its lines.

    Because a paragraph may hold only inline content; the browser ends the paragraph where a div begins.

    You want to colour only the date in "Sale ends 31 October". Which box goes around the date?Tap to reveal
    1. It is a few words inside a sentence.

    A span.

    Did you know?

    div came first, then span

    The div element already appears in HTML 3.2, which the W3C made a Recommendation on 14 January 1997. That version has no span. The span element arrived with HTML 4.0, a Recommendation of 18 December 1997, which describes the DIV and SPAN elements together as a generic way of adding structure to a page. Both were made as plain boxes for grouping.

    Why it fits here: The two boxes of this topic were added to HTML for one purpose: grouping things that have no better element.

    Sources: W3C: HTML 3.2 Reference Specification; W3C: HTML 4.0 Specification; W3C: HTML 4.0 Specification, global structure; Wikipedia: HTML

    The paragraph that closes itself

    The HTML standard lists the elements before which a paragraph ends on its own. A p element's end tag can be left out if the paragraph is immediately followed by an element such as address, article, blockquote, div, form, a heading, hr, nav, ol, table or ul. A browser follows the same rule when it reads a page: a div that starts inside a p first closes the paragraph.

    Why it fits here: This is the repair this topic warns about: the paragraph ends early, and the words after the div lose their box.

    Sources: HTML Standard (WHATWG): the p element; HTML Standard (WHATWG): parsing HTML documents

    In one line

    div groups blocks and span groups words inside a line; neither means anything, so use them only when no element with a meaning fits, and never put a div inside a p.

    Attributes: extra details on an element

    At a glance

    An attribute is an extra detail written inside an opening tag, as a name and a value: name="value". It does not change the content; it tells the browser something about it, like the address on a parcel. The value always goes in quotes.

    An id gives one element a name that no other element on the page shares, like a roll number, with no spaces in it. Links, CSS and JavaScript can then find that element in one step. A class gives a name that many elements share. The lang attribute names a language, such as mr for Marathi.

    Take a guess: Two students in a class have the same roll number: what goes wrong when two elements share an id?

    A parcel has its contents inside, and a few details written on the outside: the address, the weight, "fragile". The details do not change what is inside; they tell the post office how to treat it. HTML elements carry details in the same way, written inside the opening tag.

    Attribute

    A detail added to an element, written inside its opening tag as a name, an equals sign and a value in quotes: lang="en".

    You have already met one: <html lang="en">. The name is lang, the value is en, and it tells the browser the page's language. An element can have several attributes, separated by spaces, in any order.

    <p lang="hi" title="Welcome">नमस्ते, आशा स्टोर्स में आपका स्वागत है।</p>

    Checked in Chrome

    • the paragraph says it is in Hindi
    • its title is Welcome

    Here lang="hi" says this paragraph is in Hindi, even if the rest of the page is English, so a screen reader switches its voice. title="Welcome" adds a small note that appears when the mouse rests on the paragraph.

    Always quote the value

    Browsers accept lang=hi without quotes, but a value with a space then breaks: title=Welcome back gives the title Welcome and a second, meaningless attribute called back. With quotes, title="Welcome back" is one value. Quote every value, every time.

    id: a unique name

    The id attribute gives one element a name that no other element on the page shares, like a roll number in a class. Links can then jump to it, CSS can style just that element, and JavaScript can find it in one step.

    <h2 id="offers">Today's offers</h2>
    <p id="total">Cart total: ₹0</p>

    Checked in Chrome

    • the id offers names the heading
    • exactly one element is called total

    An id must be unique on its page: two elements called total is like two students with the same roll number. The page still shows, but code that looks up total finds only the first one, and the second is silently ignored. An id also cannot contain spaces; use hyphens, as in id="cart-total".

    A second attribute, class, gives a name that many elements can share, such as every price on the page. The CSS step uses classes all the time; for now, remember the difference: an id for one, a class for many.

    AttributeWhat it doesExample
    langthe language of the element's text<p lang="hi">
    titlea note shown when the mouse rests on it<p title="Welcome">
    ida unique name for one element<p id="total">
    classa shared name for a group of elements (the CSS step)<span class="price">

    Worked example 8

    A cart page has two paragraphs, both with id total: one for the cart total and one for the delivery total. A script that updates the cart total sometimes changes the wrong number. Find the slip and fix it.

    What we have: two elements with the same id

    What we need: two unique ids

    The plan: give each element its own name, saying what it is

    <p id="total">Cart total: ₹500</p>
    <p id="total">Delivery: ₹40</p>

    Checked in Chrome

    • (the slip shows: two elements share one id)
  • A lookup of total finds only the first element, so the two numbers are easy to mix up.
  • <p id="cart-total">Cart total: ₹500</p>
    <p id="delivery-total">Delivery: ₹40</p>

    Checked in Chrome

    • each id names exactly one element

    Give each its own id: cart-total and delivery-total.

    Check: each id now names exactly one element, and each name says what it holds.

    Worked example 9

    A tooltip should read Opens at 9 am, but it shows only Opens. Here is the HTML: <p title=Opens at 9 am>Asha Stores</p>. Fix it.

    What we have: an attribute value with spaces and no quotes

    What we need: the full tooltip

    The plan: put the whole value in quotes

    <p title="Opens at 9 am">Asha Stores</p>

    Checked in Chrome

    • the title is the whole phrase
  • Without quotes, the value stopped at the first space: Opens. The words at, 9 and am became three extra attributes, each meaning nothing.
  • title="Opens at 9 am", with quotes.

    Check: the check reads the title back as the whole phrase.

    When you are stuck: attributes

    Find the opening tag: attributes live only there, never in the closing tag.

    Check the shape: name, equals sign, value in double quotes; spaces between attributes.

    For id, check that no other element on the page has the same one.

    Your turn

    What are the name and the value in lang="mr"?Tap to reveal
    1. The name is before the equals sign; the value is in the quotes.

    The name is lang and the value is mr (Marathi).

    How many elements on a page may have id="total"?Tap to reveal
    1. An id is like a roll number.

    One.

    Should many prices share an id or a class?Tap to reveal
    1. One of them is for exactly one element.

    A class: an id is for one element, a class for many.

    Did you know?

    Every id becomes a name on the window

    Give an element an id, and the browser's window object gains a property of that name: the HTML standard says the window supports named properties, among them the ID of every element that has one. So an element with the id total can be reached in JavaScript as window.total. The standard itself warns that relying on this leads to brittle code, and says to use document.getElementById instead.

    Why it fits here: An id must be unique, as this topic says. The browser itself treats it as a name for exactly one thing.

    Source: HTML Standard (WHATWG): named access on the Window object

    lang tells a screen reader how to speak

    The W3C's Web Content Accessibility Guidelines include a rule called Language of Parts, success criterion 3.1.2 at level AA. It asks that words in a different language be marked, so that browsers and assistive technologies can present them by the presentation and pronunciation rules of that language. The rule covers screen readers, braille displays and other voice browsers as well as ordinary browsers.

    Why it fits here: The lang attribute of this topic is how that marking is done, for a whole page or for a single word.

    Source: W3C WAI: Understanding Success Criterion 3.1.2, Language of Parts

    In one line

    An attribute is a name="value" detail inside an opening tag; always quote the value, give an id to one element only (no spaces), and use a class for a name many elements share.

    Where it is used

    Every page on the web, every email newsletter, and the screens of many phone apps are built from the elements of this lesson.

    What you learnedWhere it is usedA real example
    the skeletonevery page ever serveda train booking site's every page starts with the doctype and has its own title in the tab
    the titletabs, bookmarks, search results, shared linksthe words that show when someone shares a product link on WhatsApp come from the page's title and head
    headingssearch engines, screen readers, a page's own table of contentsa news site's article has one h1 (the headline) and h2s for its sections
    strong and emanything a screen reader reads alouda bank's page marks Do not share your OTP with strong, so it is read with weight
    div and spanevery layout, before CSS styles ita shop's product card is a div; its price is a span that CSS later colours
    idlinks to a part of a page, styling one element, scriptsa results portal jumps straight to a student's row with an address ending in #roll-1042
    commentsnotes between developers beside a table that is updated by hand

    Mistakes and debugging

    Browsers never show an error for broken HTML: they repair it silently and show their best guess. So HTML slips show up as odd-looking pages, not messages. Here are the common ones, and how each looks.

    1. A closing tag left out

    <p><strong>Sale</strong> on rice. <em>Today only.</p>
    <p>Open till 9.</p>

    Checked in Chrome

    • (the slip: em was never closed in the first paragraph)

    Look closely: the em was never closed, and the browser carries the italics on into the next paragraph to repair it. The fix: <em>Today only.</em>. When a style runs on too far, look for an element that opened and never closed.

    2. Wrong nesting

    <strong><em>Hot</strong></em> closes the outer element first. The browser guesses, and the guess may not be what you meant. The fix: close in the reverse order of opening: <strong><em>Hot</em></strong>.

    3. A missing doctype

    <title>Asha Stores</title>
    <p>Welcome</p>

    Checked in Chrome

    • (the slip: without a doctype the browser uses quirks mode)

    The page shows, but in quirks mode, an old way of drawing pages kept for sites from the 1990s. Layouts then break in odd ways in the CSS step. The fix: start every page with <!DOCTYPE html>.

    4. Headings chosen for size

    The fix: choose the level for the outline, one h1 and no skipped levels; change sizes with CSS.

    5. A div inside a p

    The browser ends the paragraph where the div starts, so text after the div falls outside the paragraph. The fix: use a span inside text, or end the paragraph before the div.

    6. A repeated id, or a value without quotes

    The fix: one element per id; quote every attribute value.

    The debugging corner: see what the browser built

    Open the browser's developer tools (F12) and the Elements panel. It shows the page the browser actually built, after its repairs, not the HTML you wrote. Comparing the two finds most HTML slips in a minute; the DevTools step teaches the panel in full.

    Quick card

    The lesson on one card.

    Every element of the lesson

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Asha Stores</title>
      </head>
      <body>
        <!-- a note only you see -->
        <h1>Asha Stores</h1>
        <h2 id="offers">Offers</h2>
        <div>
          <p>Rice is <strong>10% off</strong>, <em>today</em> only: <span>₹650</span>.</p>
          <p>Shop 4, MG Road<br>Pune</p>
        </div>
      </body>
    </html>

    Checked in Chrome

    • the title
    • the id
    • strong inside p inside div
    • standards mode
    Element or attributeWhat it is
    <!DOCTYPE html>the first line: a modern page
    <html lang>, <head>, <body>the whole page; about the page; the page itself
    <meta charset>, <title>the alphabet; the name in the tab
    <h1> to <h6>the outline: one h1, no skipped levels
    <p>, <br>a paragraph; a new line inside it
    <strong>, <em>important; stressed
    <div>, <span>a block group; an inline group
    name="value"an attribute: always quoted
    id, classa unique name; a shared name
    <!-- -->a comment: hidden from the page, not from the source

    The story: The plumb line

    You build it the Mason's way. First the plot: the line that says what kind of panel it is. Then the house: a head that holds the panel's name, and a body that holds what people see. Inside the body, one main heading, then the smaller ones, then the paragraphs, every piece opened and closed.

    Tobin runs a plumb line down your panel and finds nothing crooked. "It stands," he says. Then he looks at Corin's. "That one leans." Corin grins. "It leans with style."

    Reagents gathered: element, tag, attribute, doctype, head and body.

    Sources