<span class="drop" />
Can HTML spans be closed like this?
Whether or not this is valid depends on your doctype, basically whether or not you're using XHTML or HTML.
When using XHTML, all major browsers will support self closing tags like the example you provided. Take the following example, this is valid because I'm specifying the page is using XHTML (in other words, HTML that is valid XML).
Update: Based on the very good comments below, browsers will only interpret all self closing tags correctly if the mime type is text/xml
or application/xhtml+xml
, see here for the details. For pages served as text/html
(the vast majority), see here here for the tags that can be self closing.
This example will validate:
<!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>Will test page</h2>
<p>some stuff <span class="drop" /></p>
</body>
</html>
However, this example is not valid, because I've switched the doctype to HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<h2>Will test page</h2>
<p>some stuff <span class="drop" /></p>
</body>
</html>
A few helpful references:
some stuff I should not be part of the span!
'. Based on the XML document the span tag is closed, so the rest of the content should be part of the parent paragraph and not the span itself. However, when you open it in Chrome and inspect the DOM it will look like this: 'I should not be part of the span!' which is incorrect. – Jose Cifuentes Feb 26 '16 at 17:22No, this isn't supported by all browsers.
Here's an example with divs: http://jsbin.com/upovu
Testing the following fragment on validator.w3.org:
<p><span class="drop" /></p>
Validating as HTML 4.01 Strict
# end tag for "SPAN" omitted, but its declaration does not permit this
Validating as XHTML 1.0 Strict
# The uploaded document was successfully checked as XHTML 1.0 Strict.
You must write HTML compatible XHTML if you wish to serve it as text/html, and you must serve it as text/html if you want it to work in IE <= 8.
Since the tag provides no visual change by itself, it doesn't make sense to me to have an auto-closing span block with no content. The tag provides a way to add a hook to a part of a text or a part of a document. When the text is hooked in a span element you can add styles to the content, or manipulate the content with for example JavaScript.
However, to answer your question, yes the html code block posted is valid.
This is what the spec says for HTML 4.01 and for XHTML 1.0.
No, it's a container unlike an image or (deprecated) horizontal rule.
No http://www.w3schools.com/tags/tag_span.asp
The span
tag is useful for hooking css onto a particular segment of text or part of a document. I can't think of any useful/sensible reason that a span
tag would self close.
This is the paragraph we need to link to, with either of two anchor names.
– Dave Burton Jun 04 '18 at 23:54No, it's not. You can close like this ONLY if you can't insert in tag something For example: you can`t insert tag or text inside tag img, so you can close tag like this