Home >Web Front-end >CSS Tutorial >Are Self-Closing HTML Spans Valid in All Cases?

Are Self-Closing HTML Spans Valid in All Cases?

Susan Sarandon
Susan SarandonOriginal
2024-11-02 02:42:02862browse

Are Self-Closing HTML Spans Valid in All Cases?

Self-Closing HTML Spans: Valid or Invalid?

Can HTML spans be closed using the self-closing syntax, i.e., ? The validity of this approach depends on your document type.

XHTML

In XHTML (HTML that adheres to XML standards), self-closing tags are supported by all major browsers. For instance, the following code is valid in XHTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <h2 id="test">Test</h2>
    <p>Some stuff <span class="drop" /></p>
</body>
</html>

HTML

However, in HTML (without the XHTML doctype), self-closing tags are only valid for a limited set of elements. To determine if a specific element can be self-closed, refer to the W3C spec for that element (e.g.,

can be closed without the end tag).

Additional Notes:

  • Certain browsers may incorrectly interpret self-closing tags in HTML. To ensure compatibility, it's best to use the full closing tag syntax, i.e., .
  • As servers typically serve pages as text/html, the vast majority of cases should use the traditional closing syntax.

Helpful References:

  • [W3C Validator](https://validator.w3.org/)
  • [XHTML vs HTML](https://www.w3.org/2002/Talks/1206-xhtml2/history.html)

The above is the detailed content of Are Self-Closing HTML Spans Valid in All Cases?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn