Can a
element have a closing tag?

2019-04-18 20:24发布

问题:

My colleague doesn't really know or understand html. Her job is to input information into the CMS and I've noticed she keeps closing her <hr /> tags like this <hr></hr>.

I've had a Google but I can't find anywhere that says this isn't allowed or could cause problems. I know it's supposed to be <hr /> but is it worth me telling her or is it unnecessary but valid markup?

NB The doctype for our website is XHTML 1.0 Transitional if that makes any difference.

EDIT

@Jeff had a good idea about validating. I used the following code and apparently this is valid XHTML 1.0 Transitional

<!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>
        <hr></hr>
    </body>
</html>

回答1:

OK, <hr></hr> is actually a valid XHTML 1.0, too.

So, for XHTML 1.0:

  • <hr /> is valid
  • <hr></hr> is valid
  • <hr> is not valid

... for HTML 4.0:

  • <hr /> is valid
  • <hr></hr> is not valid
  • <hr> is valid

therefore the best option is to use <hr />, which is always valid.



回答2:

HTML 4 says:

Start tag: required, End tag: forbidden

And as XHTML basically means that HTML tags need to have a closing tag, I would say <hr /> is the only format you should consider.

As the others say, <hr></hr> is valid XHTML (they even use it as example) but for compatibility reasons I would not use it.



回答3:

<hr /> is merely shorthand for <hr></hr>; both are acceptable in XHTML documents. However, neither are acceptable in HTML documents, where <hr> should be used instead, which in turn is invalid in XHTML.



回答4:

No. <hr /> should not have a closing tag.

It is invalid HTML.

It is valid XML and therefore technically it's valid xhtml, but you still shouldn't use it, even if you're using xhtml.

This is because all the browsers actually use their HTML parser even when rendering xhtml code, and therefore the a closing </hr> tag is seen as an error. Some browsers may even mis-interpret it as an additional <hr> element.

The only cross-browser compatible way of doing it is either <hr> (ie plain HTML) or <hr /> if you want to have a valid xhtml document.



回答5:

As far as I know there is no closing hr tag. The tag is purely self closing and the Doc Type Definition will reflect this.



回答6:

Since it's an empty element, it makes no sense to have open and close tags.

However, XML requires all tags to be closed, hence the <hr /> form.

I suppose in HTML, not XML, <hr></hr>is valid, but unnecessary.



回答7:

The HR element draws a horizontal rule. This is a block element and does not require a closing tag.

Ref: http://msdn.microsoft.com/en-us/library/bb159725.aspx



标签: html xhtml