BeginnerLesson 9 of 306 min read

Div, span, classes and ids explained

Learn block and inline containers, how to name elements with classes and ids, and avoid div soup.

In this lesson you will

  • Tell block and inline elements apart
  • Use div and span as neutral containers
  • Give elements classes and ids
  • Name things clearly

About 11 minutes

Containers with no meaning

So far every element you have used says something: p is a paragraph, ul is a list. Sometimes you need a plain box to group things or to mark a few words, purely so that CSS or JavaScript can reach them. HTML provides two neutral elements for exactly that.

Element Type Use it to
<div> Block (starts on a new line, fills the width) Group larger sections of the page
<span> Inline (flows inside a line of text) Mark a few words inside a sentence
<div>
  <h2>Special offer</h2>
  <p>Get <span>50% off</span> this week.</p>
</div>

On their own, neither does anything visible. Their power comes when you attach a class or an id.

Block and inline elements

Understanding this split explains a lot of layout behaviour. Block elements, such as div, p, h1, ul and table, begin on a new line and stretch to the full available width. Inline elements, such as span, a, strong, em and img, sit inside the flow of text and are only as wide as their content. A block can contain inline elements. An inline element should not contain a block.

The two boxes below get a colour from CSS. Notice how the div takes a full row and the spans stay in the line. Change one div into a span and watch what happens.

<style>
 div{background:#EFEAFD;padding:8px;margin-bottom:6px}
 span{background:#DDEBFF;padding:2px 6px}
</style>
<div>I am a div and I fill the row.</div>
<p>Words with <span>one span</span> and <span>another span</span> inside.</p>

Replace <div> and its closing tag with <span> and look at how the background shrinks to the text.

<style>
 div{background:#EFEAFD;padding:8px;margin-bottom:6px}
 span{background:#DDEBFF;padding:2px 6px}
</style>
<span>I am now a span.</span>
<p>Words with <span>one span</span> and <span>another span</span> inside.</p>

Classes: reusable labels

A class is a name you give to any element. Many elements can share the same class, and one element can have several, separated by spaces. CSS uses the class to style every matching element at once.

<p class="note">Remember to save your work.</p>
<p class="note important">Back up your files.</p>
.note { background: #F1EDFE; padding: 12px; border-radius: 8px; }
.important { font-weight: 700; }

The dot in front of the name in CSS means “the class called”. The second paragraph gets both looks because it carries both classes.

Ids: one of a kind

An id names a single element. It must be unique on the page. Ids are used for in-page links, for JavaScript that needs to find one specific element, and sometimes for one-off styling.

<h2 id="contact">Contact us</h2>
#contact { color: #5A28C8; }
class id
How many per element Several One
How many elements can share it Unlimited Only one on the page
In CSS .name #name
Typical use Styling groups of elements Jump links and JavaScript targets
Name things by what they are, not how they look

Prefer class="warning" to class="red-box". If you later change the colour to purple, “red-box” becomes a lie, while “warning” stays true. Use lower case and hyphens: product-card, main-menu.

Do not overuse div

Beginners often build whole pages from nested divs, nicknamed “div soup”. It works visually but tells browsers, search engines and screen readers nothing. The next lessons introduce header, nav, main, section, article and footer, which carry meaning. The rule of thumb: use a meaningful element when one exists, and a div when none does.

A realistic example

<div class="card">
  <img src="images/laptop.jpg" alt="Silver laptop on a desk" width="400" height="300">
  <h3>Student laptop</h3>
  <p class="price">Rs <span class="amount">39,999</span></p>
  <a class="button" href="buy.html">Buy now</a>
</div>

The div groups everything belonging to one product. The classes card, price, amount and button are hooks. With three CSS rules you could turn this into a polished card, and with a few more repeat it for every product on a store.

Common mistake

Using the same id on two elements is invalid. Browsers will not complain, but in-page links and JavaScript will pick the first match only, leaving you with a bug that is hard to spot. If you need to repeat a name, use a class instead.

Other global attributes

Class and id are two of many global attributes that work on any element. Others you will use often:

  • title shows a tooltip when the mouse rests on the element.
  • lang sets the language of one piece of text, handy for a French quote on an English page.
  • hidden hides an element from view.
  • style adds CSS directly to one element. Use it for quick tests, not for real projects.
  • data-* stores custom information for JavaScript. You will meet these in the advanced lessons.

Practice exercises

  1. Build a page with three divs, each holding a heading and a paragraph. Give each the class box.
  2. Inside one paragraph wrap two words in a span with the class highlight.
  3. Give the last div an id of last-box and add a link at the top that jumps to it.
  4. Open the browser inspector and find your classes in the Elements panel.

Finish by looking at the source of any website you like. You will see class names on almost every element. Reading how experienced developers name and group things is one of the fastest ways to learn good habits.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

Which element is a block-level container with no meaning?

  1. span
  2. div
  3. em
  4. a

div is the neutral block container. span is its inline equivalent.

How can an element have two classes?

  1. class="a" class="b"
  2. class="a b"
  3. class="a,b"
  4. It cannot

Separate class names with a space inside one class attribute.

Which is true of ids?

  1. Many elements can share one
  2. Each must be unique on the page
  3. They cannot be used in CSS
  4. They start with a dot

An id identifies exactly one element. Dots are for classes, hashes for ids.

Which class name is better?

  1. big-red-text
  2. warning
  3. Class1
  4. RED!

Names describing purpose stay accurate when the design changes.

When should you use a div?

  1. Always, for everything
  2. When no more meaningful element fits
  3. Never
  4. Only inside tables

Prefer meaningful elements. Use div as a generic grouping tool.

Summary

  • div groups blocks; span marks inline text.
  • Classes are reusable labels; ids are unique.
  • Name by purpose, in lower case with hyphens.
  • Use meaningful elements before falling back on div.

Next lesson

A guided mini-project: build a complete personal profile page.

Questions

Is div bad?

No, it is simply neutral. It becomes a problem only when used where a meaningful element such as nav or article would fit better.

Can an id and a class be on the same element?

Yes. An element can have one id and any number of classes.

Do class names matter to browsers?

No, the names are yours to choose. They matter to you and your teammates, so make them clear.

What does div stand for?

Division. It divides a page into sections for grouping and styling.

Progress is saved in this browser.