Cyber Code Academy
Tutorials Academy Courses
Home / html-tutorials-md / HTML Div and Span

HTML Div and Span

> Understand generic containers, block and inline flow, reusable class names, and when to choose semantic alternatives.

**Cyber Code Academy · HTML Tutorial · Lesson 17 · Beginner**

## 1. What is a div?
The div element is a generic container. It has no special semantic meaning by itself. It is commonly used to group elements for CSS layout or JavaScript operations.

“`html

HTML Fundamentals

Learn the structure of web pages.

View course

“`

Here, the div groups a heading, paragraph, and link into one course card.

## 2. What is a span?
The span element is a generic inline container. It is useful when you need to identify a small portion of text without creating a new semantic element.

“`html

Course status: Open for admission

“`

The span is inside the paragraph and remains part of the same text flow by default.

## 3. Default display behaviour

By default:
– div participates in block layout and normally starts on a new line.
– span participates in inline layout and flows with surrounding text.

CSS can change display behaviour:
“`css
.inline-card { display: inline-block; }
.span-block { display: block; }
“`

Changing display does not change the element’s semantic meaning.

## 4. Use class attributes for reusable styling

“`html

HTML
CSS
JavaScript

“`

“`css
.course-card {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.75rem;
margin-bottom: 1rem;
}
“`

All three div elements receive the same style because they share the course-card class.

## 5. Use id when one element needs a unique identifier

“`html

Available courses

“`

The id should be unique in the document. Use class for reusable groups and id for unique identification or fragment navigation.

## 6. Avoid unnecessary div wrappers

Overusing div elements creates deeply nested markup that can be difficult to maintain.

Instead of:
“`html

“`

Prefer meaningful elements:
“`html

“`

## 7. Common mistakes
– Using div for every element without considering meaning.
– Using span to group large page sections.
– Repeating the same id on multiple elements.
– Expecting div and span to have special accessibility meaning.
– Choosing elements by their default visual appearance instead of their purpose.

## 8. Practice exercises
1. Create three course cards using div and a reusable class.
2. Use span to highlight a course status within a paragraph.
3. Add a unique id to the course list.
4. Replace a generic header div with header and nav.
5. Use CSS to change a span to display block and observe the layout.

## 9. Knowledge check
1. What is the purpose of div?
2. What is the purpose of span?
3. How do their default display behaviours differ?
4. When should you use class instead of id?
5. Does changing display change semantic meaning?

**Next lesson:** HTML Audio.

Want to learn this professionally?

Explore practical IT training at Cyber Code Academy.

View Academy Courses