AD JB Toolbox 01 EN

HTML Tutorial / When to Use div and span

div and span group content without giving it a special meaning. Use them when CSS or JavaScript needs a target and no more specific HTML element describes the content. A div usually groups larger pieces; a span groups part of a sentence.

How are div and span different?

A div can wrap flow content such as paragraphs, lists, or an entire card. A span is for phrasing content within a sentence. Neither says that its content is a heading, a button, or especially important. By default, a div starts as a block and a span stays inline, but CSS can change their display. Choose by the content's structure, not just its appearance.

Do not put a paragraph or div inside a span. If your only goal is a line break, first decide whether you need a new paragraph or a CSS layout change instead of another wrapper.

Group only what needs styling

Put this HTML in a document's body and the CSS in a style element in its head.

<div class="product-card">
  <h2>Travel mug</h2>
  <p>Price: <span class="price">$15</span></p>
</div>
.product-card {
  padding: 16px;
  border: 1px solid #dadada;
}
.price {
  color: #075cb4;
  font-weight: 700;
}

The whole card gets a border, while only the price changes color and weight. The class attribute gives CSS a name to select. Even without CSS, the product name remains a heading and the price remains in a paragraph. If the price does not turn blue, check that class="price" in HTML matches .price in CSS. The dot marks a class selector; it is not part of the HTML class name.

Prefer an element with a real meaning

For primary navigation, consider nav rather than a generic div. For an independent post, consider article. For an action users can activate, use button instead of making a clickable span. The correct native element already conveys meaning and often supplies keyboard behavior.

The price in this example does not need special data processing, so span is enough. You can keep a CSS class while changing its element to a more meaningful one later. See the MDN list of HTML elements when choosing a suitable element.

Common mistakes

Do not build every page region out of div solely to make layout styling easier. Conversely, do not give a wrapper a semantic element whose meaning does not fit. A styled span does not become a heading or button just because it looks like one. Start with the content and interaction, then add generic wrappers only where necessary.

More in This Category
HTML Tutorial / Accessible Tables: caption, scope, and Spanning Cells

HTML Tutorial / Accessible Tables: caption, scope, and Spanning Cells

Make table headings and data relationships clear with caption, th, and scope, and learn when colspan or rowspan makes a table harder to read.

HTML Tutorial / Links: Paths, Page Fragments, and Email

HTML Tutorial / Links: Paths, Page Fragments, and Email

Create HTML links with absolute or relative URLs, jump to a document fragment, and add email, new-tab, or download links. Learn what to verify before publishing.

HTML Tutorial / When to Use div and span

HTML Tutorial / When to Use div and span

Understand the structural difference between div and span, style only the content you need, and choose semantic elements when they better describe the content.

HTML Tutorial / Responsive Images: srcset, sizes, and picture

HTML Tutorial / Responsive Images: srcset, sizes, and picture

Offer image resolutions with srcset and sizes, and switch crops with picture. Downloadable files let you test the examples in a browser.

HTML Tutorial / Table Basics: table, tr, th, and td

HTML Tutorial / Table Basics: table, tr, th, and td

Build a basic HTML data table with rows and header or data cells, then organize its headings and body without using tables for page layout.

HTML Tutorial / Text Semantics: Emphasis, Quotations, Abbreviations, and Code

HTML Tutorial / Text Semantics: Emphasis, Quotations, Abbreviations, and Code

Learn when to use HTML em, strong, q, blockquote, abbr, code, and kbd. Examples show how each element conveys meaning rather than just changing the appearance of text.

HTML Tutorial / Group Images and Captions with figure and figcaption

HTML Tutorial / Group Images and Captions with figure and figcaption

Use figure and figcaption to keep an image and its visible explanation together, and distinguish captions from image alt text.

HTML Tutorial / Lists: ul, ol, dl, and Nested Lists

HTML Tutorial / Lists: ul, ol, dl, and Nested Lists

Choose the right HTML list for ordered items, unordered items, and name–description pairs. Try a complete example, custom numbering, and correctly nested lists.

HTML Tutorial / What Is HTML and What Does It Do on a Web Page?

HTML Tutorial / What Is HTML and What Does It Do on a Web Page?

HTML gives web content structure and meaning. Learn how it differs from CSS and JavaScript, see how basic markup appears in a browser, and make your first HTML file.

HTML Tutorial / Images: img, Alt Text, and Dimensions

HTML Tutorial / Images: img, Alt Text, and Dimensions

Add an image with HTML img, write useful alternative text, and set width and height correctly. A downloadable example helps you test file paths and sizing.