Home > Article > Web Front-end > 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.,
Additional Notes:
Helpful References:
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!