Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Built-in DOM Components

The div and span elements in React

Reference for div and span in React JSX: block versus inline containers, style and className, event handlers, nesting rules and when to use a semantic element.

<div> and <span> are the two generic containers of HTML. A <div> is a block-level box that starts on a new line and spans the available width; a <span> is an inline box that flows inside a line of text. They have no meaning of their own, so you reach for them when no semantic element fits, to group nodes for styling, layout or an event handler.

In React they are ordinary host components. You pass any global attribute in camelCase (className, tabIndex), a style object, event props such as onClick, and children. They have no props of their own and no special React behavior, which makes them the baseline every other DOM component is compared with.

JSX
<div className="card" style={{ padding: 12 }} onClick={handleClick}>
  Status: <span className="status">ready</span>
</div>

Try it

Block, inline and a click counter

The clickable div needs role="button", tabIndex and a key handler to work for keyboard users. A real <button> gives you all three for free, which is why it is the better choice.

Invalid nesting

React itself builds the DOM with API calls, so on the client the <div> ends up inside the <p> and only the console warns about it. Server HTML is parsed by the browser, which closes the paragraph before the div. The parsed tree no longer matches what React expects, and hydration reports a mismatch.

Syntax

JSX
<div {...props}>children</div>
<span {...props}>children</span>
TypeScript
type DivProps = React.HTMLAttributes<HTMLDivElement>;   // children, className, style, id, hidden, ...
type SpanProps = React.HTMLAttributes<HTMLSpanElement>;
Prop Description
children Any renderable content: text, elements, arrays, fragments.
className CSS class string. Writing class instead logs a warning that suggests className.
style An object of camelCase properties. A string throws an error.
id, title, lang, dir, hidden, tabIndex, role, aria-*, data-* Global attributes, passed through.
onClick, onKeyDown, onPointerDown, ... Event handlers, delegated by React at the root.
ref A ref object or callback that receives the HTMLDivElement / HTMLSpanElement. In React 19 it is an ordinary prop.
dangerouslySetInnerHTML Raw HTML. You cannot pass it together with children: that throws.

Returns: a React element. Nothing is rendered for null, undefined or booleans as children.

Usage notes

  • Pick the semantic element first. Use <section>, <nav>, <ul>, <button>, <label> or <p> when they describe the content; screen readers and search engines use them. A "div soup" works visually and fails accessibly.
  • Nesting rules still apply. A <div> cannot sit in a <p>, a <span> cannot hold block content in valid HTML, and neither belongs directly in a <table>, <tbody> or <tr>. React warns in development.
  • Wrapper divs are not needed to return several siblings; use a fragment (<>...</>) so you do not add a node that changes CSS selectors, grid or flex layouts.
  • Keys go on the outermost element a list produces, for example <div key={item.id}>.
  • Click handlers on non-interactive elements need a role, focus and keyboard support, as in the example.
  • Inline styles are good for values computed at runtime; static styling is usually better as classes, which can be cached and use media queries.
  • span is a good target for highlighting parts of text (matches in search results, currency symbols) without breaking the sentence.
  • Whitespace: JSX trims whitespace around line breaks. Write {' '} when you need an explicit space between inline elements.