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 / 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 / Syntax

HTML Tutorial / Syntax

Learn the essential rules of HTML syntax, including elements, tags, attributes, void elements, comments, nesting, and the structure of a complete HTML document.

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 / 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 / Choose Input Types for Email, Numbers, Dates, and Choices

HTML Tutorial / Choose Input Types for Email, Numbers, Dates, and Choices

Choose input types based on the value being collected and understand how checkboxes and radio groups behave when submitted.

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.

HTML Tutorial / Form Validation with required, Length, and Range Limits

HTML Tutorial / Form Validation with required, Length, and Range Limits

Apply built-in required, length, and numeric constraints, test error states, and understand why server-side validation remains necessary.

HTML Tutorial / Add Placeholder Text to Form Fields

HTML Tutorial / Add Placeholder Text to Form Fields

Learn how to add placeholder hints to HTML input and textarea fields without confusing them with labels or initial values. Includes a complete form example, required-field checks, and an explanation of empty values in submitted data.