React Reference ยท JSX Syntax and Rules
Self-closing tags in JSX
Why every JSX tag must be closed, how to self-close void elements and components, and the error React throws when a void element gets children.
HTML lets you leave some tags open: <br>, <img src="a.png"> and <input> have no end tag, and browsers also forgive an unclosed <li> or <p>. JSX is stricter. It follows XML rules, so every tag must be closed, either with a matching closing tag or by ending the opening tag with />.
A self-closing tag means "this element has no children". It works for HTML void elements, for any other HTML element, and for components.
<br /> <img src="/samples/sunset.svg" alt="A sunset" /> <input type="text" /> <Avatar user={user} /> // a component with no children <div /> // an empty div; same as <div></div>
Try it
Syntax
<Name attributes /> <Name attributes>children</Name>
| Case | Allowed forms |
|---|---|
HTML void elements (area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr) |
Self-closing only. Children cause an error. |
| Other HTML elements | Self-closing or open and close tag |
| Components | Self-closing or open and close tag |
| Fragments | <Fragment /> or <></> |
Errors: an opening tag with no closing tag is a compile error ("Unterminated JSX contents" or "Expected corresponding JSX closing tag"). A void element with children throws at render time in development and production.
Usage notes
- Self-closing is purely syntax.
<div />and<div></div>compile to the same call and produce the same DOM. - Slash placement. The
/goes right before>. A space before it is the usual style. - Omitted end tags do not exist. There is no
<li>that ends at the next<li>; close each one. - Components with children.
<Card />passes nochildrenprop (it isundefined), while<Card></Card>also passes none. Only content between the tags sets it. - Elements like
scriptandtextarea.<textarea />is fine, but its value is a prop (value/defaultValue), never text between the tags. - Formatters such as Prettier turn empty pairs into self-closing tags automatically.