Use <div> as a generic block-level container to group sections of a page for layout or styling, and use <span> as a generic inline container to style or label part of a line of text. The html div vs span rule is simple: <div> starts on a new line and takes the full width, while <span> stays in the flow of the text. Reach for either one only when no meaningful element fits.
Both have no meaning of their own. They exist so that you can attach a class, an id or an attribute to a piece of content.
A <div> is a block-level element: it starts on a new line and stretches to the full width of its parent, and you can set width, height, margins and padding on it. A <span> is inline: it only takes up the space of its content, does not create a line break, and ignores width and height.
The two <div> elements appear on separate lines, while the <span> sits inside the sentence. These are the browser default display values, and you can change them with the CSS display property.
A <div> groups several elements so that you can style them together or position them with CSS Grid or Flexbox.
The container applies one set of rules to the whole group. A wrapper like this is also what you need for components in React or Vue, or when a script must find a region of the page.
A <span> marks a few words so you can change their style or hook a script to them.
A span is also useful for language changes, with <span lang="fr">bonjour</span>, and for text that a script updates, such as <span id="count">0</span>.
Because a <div> or <span> says nothing about the content, search engines and screen readers get no help from them. Before using one, check whether a semantic element fits.
| Instead of | Use | Reason |
|---|---|---|
<div class="header"> | <header> | Marks the page or section header |
<div class="nav"> | <nav> | Marks navigation links |
<div class="main"> | <main> | The main content landmark |
<div class="footer"> | <footer> | Footer landmark |
<div class="article"> | <article> | Self-contained content |
<div class="button"> | <button> | Keyboard and screen reader support built in |
<span class="strong"> | <strong> | Marks strong importance |
<span class="em"> | <em> | Marks stress emphasis |
<span class="date"> | <time> | Machine-readable date |
The button row is the most important: a clickable <div> does not get focus, Enter or Space handling, or a role, so it fails for keyboard users. If you must make one, you have to add all of that yourself, and a real <button> is better.
A <div> or <span> has no role, so assistive technology ignores it and reads only the content. If you build a custom widget from these elements, you need ARIA roles, states and keyboard handling. Use classes to describe what content is, for example class="price", and not how it looks, for example class="red-bold". Avoid deep stacks of nested <div> elements, which people call div soup, because they make the markup hard to read and slow to maintain. Modern CSS often removes the need for extra wrappers.
<div> inside a <p>: a paragraph can contain only inline content, so the browser closes the paragraph early. Use a <span> or change the structure.<span>: it has no effect unless you change display to inline-block or block.<div> for text that should be a heading, which breaks the outline of the page.<span> elements for spacing instead of CSS margins or padding.Not as valid HTML in most cases: an inline element should not contain block elements. Use a <div> or change the wrapper to a block element with CSS.
Yes. Set display: inline or inline-block in CSS. The element is still a generic container.
They have no direct effect. Semantic elements help engines and readers understand structure, so use them where they apply.
It makes pages harder to maintain and to navigate for assistive technology. Use landmarks and headings, and add wrappers only when needed.
Use <div> for generic blocks and <span> for generic inline pieces, and only when no semantic element describes the content. Remember that block and inline behavior can be changed in CSS, keep class names meaningful, and use real buttons, headings and landmarks for accessibility.