skip to content

Semantic Elements

Choosing elements that describe meaning rather than appearance: sections and landmarks, heading rank, text-level markup, lists, tables, and the honest cases where a div is right. Interviewers use semantics to test whether your markup would still be usable with the stylesheet removed.

part ofHTMLoverview, primer and where to startread it →
on this pageshow

explore

questions

page 1 of 2

In HTML, when is a <div> or a <span> the right element to use, and how do you choose between the two?

level: juniorimportance: must knowfreq 65%

answer

  1. the two elements with no meaning
  2. one wraps blocks, one wraps text
  3. content model, not the rendering box
  4. last resort after the real element
  5. both map to the generic ARIA role

basics

~20 s

Reach for div or span only when no element with meaning fits — they carry no semantics. div is a flow-content container for blocks of content; span is a phrasing-content container for a run of text inside a line.

solid answer

~40 s

`div` and `span` are the two deliberately meaningless elements in HTML: they exist so you can group or mark up content when nothing in the language describes what it is. The choice between them is a content-model choice, not a styling choice. `div` accepts flow content — paragraphs, lists, headings, other divs — so it wraps a chunk of the document. `span` accepts only phrasing content, so it wraps text and text-level elements inside a line, and putting a `<p>` or a `<div>` inside it is invalid. Both map to the ARIA `generic` role, which is the point: assistive technology gets nothing from them, so if the content really is a list, a paragraph, a heading or a button, that element is the correct answer and the wrapper is not.

code

html · 16 lines
html
<!-- generic: no meaning conveyed to anyone -->
<div class="heading">Recent orders</div>
<div class="list">
  <div class="item">Order 1201</div>
  <div class="item">Order 1202</div>
</div>

<!-- meaningful: heading + list are exposed to assistive technology -->
<h2>Recent orders</h2>
<ul>
  <li>Order 1201</li>
  <li>Order 1202</li>
</ul>

<!-- a legitimate span: marking a fragment of text -->
<p>Shipping to <span class="country">Portugal</span> takes 3 days.</p>

go deeper

for a junior

Be able to say plainly that div and span carry no meaning, that div wraps chunks and span wraps text inside a line, and that you reach for them only when no element describes the content.

for a middle

Explain the choice as a content-model rule — div takes flow content, span takes phrasing content — and show you know that rendering as block or inline does not change the category or where the element may legally appear.

for a senior

Show the production cost of generic wrappers: lost keyboard behaviour, lost landmarks and heading navigation, and rebuild work when a styled div has to become a real control. Be ready to argue where a wrapper is still the right call.

for a principal

Own the standard across a codebase: how component libraries make the semantic element the default and the wrapper the explicit opt-out, and how you keep that enforceable without turning markup review into taste debates.

## The two elements that mean nothing Almost every element in HTML tells a consumer something: `<p>` says paragraph, `<nav>` says navigation, `<button>` says activatable control. `div` and `span` are the two elements defined to say nothing at all. The specification describes `div` as an element with "no special meaning at all" and `span` likewise as a generic wrapper. That is not a defect — it is the escape hatch the language needs so authors are not forced to abuse a meaningful element for a grouping that has no meaning. Both are mapped to the ARIA `generic` role, so they add nothing to the accessibility tree. A screen-reader user gets no information from either. ## div vs span is a content-model decision The real difference is what each is allowed to contain, and where each is allowed to appear: - `div` has a content model of **flow content** — nearly anything that can live in the body: headings, paragraphs, lists, tables, other divs, and text. - `span` has a content model of **phrasing content** — the text-level subset: text nodes, `a`, `em`, `strong`, `code`, `img`, `br`, and other spans. Because phrasing content is itself a subset of flow content, a `span` can go anywhere a `div` can, but not the reverse. A `span` is valid inside a `<p>`; a `div` is not, because `p` only accepts phrasing content. ```html <!-- valid: span is phrasing content, so it belongs inside a paragraph --> <p>Total: <span class="amount">42</span> items</p> <!-- invalid: div is flow content and p only accepts phrasing content --> <p>Total: <div class="amount">42</div> items</p> ``` A common misreading is that this is about being "block" or "inline". Those are rendering concepts controlled by the stylesheet, and changing how an element renders does not change its category in the HTML content model. A `span` you render as a block is still phrasing content, and a `div` you render inline is still flow content and still invalid inside a `<p>`. ## When a wrapper is genuinely correct Legitimate uses of `div` and `span`: - A grouping that exists purely for layout or for a style hook, and that names nothing in the document's structure — a row, a column, a card shell around content that already carries its own semantics. - A hook for scripting or for a `data-*` attribute on a chunk of content that has no better home. - Marking a fragment of text so it can be styled or targeted — highlighting a substring, wrapping a currency symbol, isolating a word. Illegitimate uses — where a real element exists: - A clickable `div`, when `<button>` gives focusability, Enter/Space activation and a button role for free. - A `div` per row of a bulleted list, when `<ul>`/`<li>` conveys the list and its item count. - A `div` with large text acting as a title, when `<h2>` puts the title in the document's heading structure. - A `span` used for emphasis, when `<em>` or `<strong>` conveys it. The rule of thumb interviewers want to hear: **choose the element that describes the content; fall back to `div`/`span` only when no element does.** The generic element is a last resort, not a default. ## Why "div soup" is a real cost, not a style opinion When semantics are replaced by generic wrappers, three things quietly disappear. First, behaviour: a real control comes with keyboard activation and form participation you would otherwise have to rebuild. Second, structure: assistive technology navigates by headings, landmarks and lists, and a page of divs offers no landmarks to jump to. Third, resilience: consumers other than your own stylesheet — reader modes, translation tools, search crawlers, future maintainers — read the element names, not the class names. None of that says wrappers are bad. A page will legitimately contain many divs. It says a `div` should be the answer to "there is no element for this", and never the answer to "I did not check whether there is one". ## Practical check Before writing a wrapper, ask what you would call the thing out loud. If the answer is a word HTML already knows — list, paragraph, heading, navigation, button, table, quote, form field — use that element. If the honest answer is "a box" or "a bit of text", `div` and `span` are exactly right, and using them is not a compromise.

  • Does giving a span a block rendering in the stylesheet change where it may legally appear in the markup?
    No. The content model is a specification category fixed by the element, not by how it is rendered. A `span` stays phrasing content however it is displayed, and a `div` stays flow content — so a `div` rendered inline is still invalid inside a `<p>`, and a `span` rendered as a block is still valid there.
  • If div and span add nothing to the accessibility tree, why does adding lots of them still matter to a screen-reader user?
    Because of what they replace. Assistive technology navigates by structure — headings, landmarks, lists, controls. Every heading rendered as a styled `div` is one fewer jump target; every list of divs loses the item count; every clickable `div` loses keyboard activation. The wrappers themselves are inert, but they crowd out the elements that were not.
  • Is it ever right to wrap content that already has a semantic element in an extra div?
    Yes. If you need a grouping purely for layout or as a script or `data-*` hook, and that grouping names nothing in the document, a `div` around an existing `<article>` or `<ul>` is correct — you are adding a box, not replacing meaning. The defect is substituting a `div` for the semantic element, not surrounding it.

div and span are the plain cardboard boxes of HTML: perfect when nothing on the shelf fits, wasteful when the labelled box was right there.

saying these in an interview costs you the question

  • div is for block content, span is for inline content
  • Adding role and tabindex makes a div as good as a button
  • Using div everywhere is fine because CSS handles the look
  • span is just a div that does not break the line
  • Semantic elements are only about SEO

context

open as a page

In HTML, what decides whether a piece of text should be an <h2> or an <h3>, and why is "it renders at the size the design wants" the wrong reason to choose one?

level: juniorimportance: must knowfreq 72%

basics

~20 s

Heading rank encodes document structure: an h2 is a top-level section, an h3 a subsection of the h2 above it. Visual size is CSS's job, so choose the rank the outline needs and restyle it.

open as a page

In HTML, which elements are allowed as direct children of <ul> and <ol>, and where must a nested sublist be placed?

level: juniorimportance: must knowfreq 60%

basics

~10 s

Only <li> elements may be direct children of <ul> and <ol>, plus the script-supporting elements <script> and <template>. A nested sublist belongs inside the <li> it hangs off, never between two <li> elements.

open as a page

Which HTML elements expose implicit landmark roles, and what do landmarks give a screen-reader user that a page built from <div>s does not?

level: juniorimportance: must knowfreq 68%

basics

~20 s

A top-level <header>, <nav>, <main>, <aside>, <footer>, <search> and a named <form> expose the banner, navigation, main, complementary, contentinfo, search and form landmark roles. Screen readers list landmarks so users jump between regions instead of reading everything.

open as a page

In HTML, what is the difference between <strong> and <b>, and between <em> and <i>, and what does choosing correctly actually change?

level: juniorimportance: must knowfreq 76%

basics

~20 s

The strong element marks importance and em marks stress emphasis that changes a sentence's meaning. The b and i elements set text apart without importance: b draws attention, i signals an alternative voice such as a foreign phrase or technical term.

open as a page

Can an HTML page have more than one <h1>, and does placing an <h1> inside a <section> automatically demote it to a lower level?

level: middleimportance: must knowfreq 58%

basics

~20 s

Nothing demotes it. A nested <h1> is still level 1 to browsers and screen readers, because the HTML5 outline algorithm that promised auto-demotion was never implemented anywhere and has since been dropped. Write explicit ranks; keep one h1.

open as a page

In HTML, how do you decide between <article>, <section>, and <div> for a block of page content?

level: middleimportance: must knowfreq 74%

basics

~20 s

Use <article> when the block would still make sense on its own elsewhere, <section> for a thematic part of a document that has its own heading, and <div> when the grouping exists only for styling or scripting.

open as a page

In an HTML data table, what does the scope attribute on a <th> do, and what changes for a screen-reader user when it is missing?

level: middleimportance: must knowfreq 70%

basics

~20 s

The scope attribute declares which cells a <th> heads: scope="col" the column beneath it, scope="row" the row beside it. Without it, assistive technology has to guess the association from position, and in two-way tables it guesses wrong.

open as a page

In an HTML <table>, what does the <caption> element do, and where in the markup must it appear?

level: juniorimportance: should knowfreq 55%

basics

~20 s
<caption> gives an HTML table a visible title that also becomes the table's accessible name, so a screen-reader user knows what the table holds before entering it. It must be the first child of <table>, before any colgroup or row.
open as a page

In HTML, when is <br> the correct element to use, and what does <wbr> do?

level: juniorimportance: should knowfreq 32%

basics

~20 s

Use br only for line breaks that are part of the content itself, such as the lines of an address or a poem — never for spacing. The wbr element marks a place where the browser may break a long unbroken string, and renders nothing otherwise.

open as a page

Why is the markup `<p>Intro <div>Block</div></p>` invalid HTML, and what DOM does the browser actually build from it?

level: middleimportance: should knowfreq 50%

basics

~20 s

The p element accepts only phrasing content, and div is flow content, so the nesting is invalid. The parser closes the open p when it sees the div start tag, so the div becomes a sibling of the paragraph — and the trailing </p> produces a second, empty paragraph.

open as a page

In HTML's content model, what is the difference between flow content and phrasing content, and how does that distinction decide what may nest inside an element?

level: middleimportance: should knowfreq 45%

basics

~20 s

Flow content is nearly everything allowed in the body; phrasing content is its text-level subset — span, em, a, img, input. Each element's specification content model names the category it accepts, and that is what makes some nestings valid and others invalid.

open as a page

In HTML, what is a <dl> for, and what are the rules for pairing <dt> and <dd> inside it?

level: middleimportance: should knowfreq 35%

basics

~20 s

A <dl> is a description list of name-value groups. Each group is one or more <dt> terms followed by one or more <dd> descriptions, so several names can share a description or one name can have several. Optionally each group may be wrapped in a single <div>.

open as a page

In HTML, what do the <ol> attributes start, reversed and type do, and what does the value attribute on an <li> change?

level: middleimportance: should knowfreq 40%

basics

~20 s

start sets the ordinal of the first item, reversed counts down instead of up, and type chooses the marker series (1, a, A, i, I). value on an <li> overrides that one item's ordinal, and later items continue counting from it.

open as a page

How many <main> elements may an HTML document contain, and what does <main> do for users and assistive technology?

level: middleimportance: should knowfreq 54%

basics

~20 s

A document may have only one <main> that is not hidden, and it must not sit inside <article>, <aside>, <footer>, <header> or <nav>. It marks the dominant content and is what skip links and screen-reader shortcuts jump to.

open as a page

Why is using an HTML <table> purely for page layout treated as an accessibility defect, and what must the markup do if a layout table is unavoidable?

level: middleimportance: should knowfreq 40%

basics

~20 s

A <table> asserts that its content is tabular data: screen readers announce row and column counts and offer grid navigation, so a layout table makes meaningless structure audible. If one is unavoidable, add role="presentation" and give it no caption and no <th>.

open as a page

What do <thead>, <tbody> and <tfoot> add to an HTML table, and where does <tfoot> go in the current HTML standard?

level: middleimportance: should knowfreq 45%

basics

~20 s

They group a table's rows into header, body and footer sections, which lets browsers repeat header rows across printed pages, lets assistive tech report row groups, and gives scope="rowgroup" something to refer to. In current HTML, <tfoot> comes last.

open as a page

In HTML, what does the title attribute on <abbr> provide, and why do accessibility practitioners warn against relying on it?

level: middleimportance: should knowfreq 34%

basics

~20 s

It supplies the abbreviation's expansion, which desktop browsers show as a hover tooltip. That reaches mouse users only: abbr cannot take focus, touch devices have no hover, and screen-reader handling of the attribute is inconsistent.

open as a page

In HTML, when do you use <q> versus <blockquote>, and what do the cite attribute and the <cite> element each do?

level: middleimportance: should knowfreq 30%

basics

~20 s

Use q for a short quotation inside a sentence and blockquote for one that stands as its own block. The cite attribute holds a source URL that browsers never display, while the cite element marks the title of a work.

open as a page

In HTML, what does the datetime attribute on the <time> element do, and when may you omit it?

level: middleimportance: should knowfreq 42%

basics

~20 s

The datetime attribute holds a machine-readable version of the date, time or duration the element's visible text describes, using the formats HTML defines. You may omit it only when the element's own text is already a valid one of those strings.

open as a page

A card component wraps its entire markup in an <a href> and that markup contains a <button>. Why does HTML's content model forbid this, and what would you do instead?

level: seniorimportance: should knowfreq 40%

basics

~20 s

The a element's content model forbids interactive content descendants, and button is interactive content, so nesting a control inside a link is invalid. Restructure the markup: link only the non-interactive content, and place the button as a sibling outside the anchor.

open as a page

A screen reader user reports that a page is hard to navigate: its only <h1> is the site logo and every section title is a styled <div>. How do such users normally move through a page by heading, and what specifically breaks here?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Screen reader users jump heading to heading with a shortcut key or pull up a headings list to skim a page. A logo as h1 makes every page's top entry identical, and styled divs never enter the list at all, so the page reads as one undifferentiated block.

open as a page

In Safari with VoiceOver, a menu marked up as a <ul> stops being announced as a list once CSS removes its bullets. Why does that happen, and what is the markup fix?

level: seniorimportance: should knowfreq 28%

basics

~10 s

WebKit deliberately drops list semantics for a list whose markers are removed with list-style, treating it as presentational. Adding an explicit role="list" to the <ul> re-declares the semantics and restores the announcement.

open as a page

A page has three <nav> elements — the primary menu, a breadcrumb trail, and an on-page table of contents. How do you make them distinguishable in a screen reader's landmark list?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Give each landmark an accessible name with aria-label or aria-labelledby, so the list reads "Primary navigation", "Breadcrumb", "On this page" instead of three identical entries. Leave the word navigation out — the role is announced already.

open as a page

An HTML table has two stacked rows of header cells, some spanning several columns. How do you associate each data cell with the right headers when scope is not enough?

level: seniorimportance: should knowfreq 32%

basics

~20 s

Give each header cell an id, then list the relevant ids in each data cell's headers attribute. That explicit association replaces positional guessing and handles multi-row headers and spanning cells that scope alone cannot describe.

open as a page

In a shared component library, a card component renders its own title, but pages nest that card at different depths so the correct heading rank varies. How would you design the component's markup contract, and what are the tradeoffs of each option?

level: principalimportance: should knowfreq 28%

basics

~20 s

Make the heading rank an input the page supplies, since only the page knows the nesting depth. Deriving rank from a composition context automates it but hides breakage; hardcoding one rank everywhere flattens outlines; role="heading" with aria-level is the last resort.

open as a page

The HTML specification gives the <a> element a transparent content model. What does transparent mean, and what may an <a> therefore wrap?

level: middleimportance: nice to knowfreq 28%

basics

~20 s

A transparent content model means the element accepts whatever its own parent accepts. So an <a> inside a div may wrap flow content such as a whole article, while the same <a> inside a p may wrap only phrasing content.

open as a page

In current HTML, what is the <hgroup> element for, and how would you mark up a page title that has a subtitle or tagline underneath it?

level: middleimportance: nice to knowfreq 26%

basics

~20 s

Modern <hgroup> wraps exactly one heading plus one or more <p> elements holding its subtitle or tagline. It groups them as related content; it does not create a heading of its own or change the heading's level.

open as a page

showing 1–30 of 31