Pages need grouping, so that parts can be styled and scripted together. HTML5 also lets authors name the parts of a page by their meaning.

Div and Span

The <div> element is a generic block container. It groups several elements so that they can be styled or moved as a unit. The <span> element is a generic inline container, used to mark a few words inside a line.

Definition: Div. A generic block element used to group content when no semantic element fits.

Neither element has any meaning of its own. They give CSS and JavaScript something to attach to, usually through a class or id attribute.

Semantic Elements

A semantic element has a name that describes its purpose. HTML5 added a family of them, described below.

Definition: Semantic element. An HTML5 element whose name describes the meaning of its content, such as nav or footer.

Table 1.9: Common semantic elements

Element Meaning
<header> Introductory content at the top of a page or section
<nav> A group of navigation links
<main> The main content, used once per page
<section> A group of related content with a heading
<article> Content that makes sense on its own, such as a news item
<aside> Side content, such as a related link box
<footer> Closing content, such as contact details

Why Semantics Matter

Semantic names help in three ways. Search engines understand which part of the page is the main content. Screen readers let users jump straight to the navigation or the main content. Developers also find the code easier to read than a long stack of divs. The rule is simple: choose a semantic element first, and use div or span only when no suitable semantic element exists.

Example 1.9: Converting divs to semantic elements

Before, every part is a div with a class name:

<div class="top">College Portal</div>
<div class="menu">Home Courses</div>
<div class="content">News of the day</div>
<div class="bottom">Contact us</div>

After, each part uses an element that states its meaning:

<header>College Portal</header>
<nav>Home | Courses</nav>
<main><article>News of the day</article></main>
<footer>Contact us</footer>

Output in the browser:

Naming Parts with class and id

The class attribute gives a group of elements a shared name, and one element may carry several classes. The id attribute gives one element a unique name, and no two elements on a page may share an id. CSS uses these names to choose elements for styling, and JavaScript uses them to find elements, as later units show. Choose names that describe the purpose, such as menu or notice, and not the appearance, such as red-box, because the appearance may change later.

Common mistakes

  • Building a whole page from div elements when semantic elements are available.
  • Using more than one main element in a page.
  • Using section only for styling, without a heading that names the section.
  • Using div where an inline container such as span is enough.

Key takeaways

  • Div groups block content and span groups inline content, and neither has meaning.
  • Semantic elements such as header, nav, main, section, article and footer name the parts of a page.
  • Semantics help search engines, screen readers and developers.
  • Prefer a semantic element and use div only as a last choice.

Practice questions

  1. What is the difference between div and span?
  2. Name three semantic elements.
  3. How many main elements should a page have?

Answers to the practice questions

  1. Div is a block container, and span is an inline container.
  2. For example header, nav and footer.
  3. One.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit I