Invictus
Invictus

Reputation: 3

XHTML: DOCTYPE, link, and meta closed with forward slash?

I couldn't find an answer to my question using a search engine; some topics came close but did not quite answer my question. ChatGPT3 was making self-contradictory statements and was reasoning in circles. I hope you will abide my neophyte question.

I understand XHTML will one day replace HTML, so I decided to learn XHTML. I understand one of the main differences between XHTML and HTML is that the stand-alone tags (probably not using the right nomenclature) that are closed in XHTML but not in HTML, i.e.:
XHTML: <img src="./demo.gif" /> <--- note the forward slash at the end
HTML: <img src="./demo.gif"> <--- note the absence of the forward slash at the end

Now, I was wondering if in XHTML the DOCTYPE tag and some header elements need to be closed with a forward slash as well. I.e.:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" /> <--- note the forward slash at the end
<html>
<head>

<meta charset="utf-8" /> <--- note the forward slash at the end**
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <--- note the forward slash at the end**
<link rel="stylesheet" href="./stylesheet.css" /> **<--- note the forward slash at the end

</head>
<body>

Hello.

</body>
</html>

Thank you for your patience,

SilverC3ll

I have tried using correct XHTML syntax in the pre-HTML location of the document (DOCTYPE above <html>) and have tried to apply correct XHTML syntax to the meta and link elements. I have closed these tags with a forward slash. I expected that even though there may be an error in syntax the browser will still display the file as intended, so I cannot verify if whether my syntax is correct or not.

Upvotes: 0

Views: 110

Answers (2)

Quentin
Quentin

Reputation: 943240

I understand XHTML will one day replace HTML

It was expected to replace HTML in the early 2000s.

Then HTML 5 came along and work on XHTML 2 was discontinued.

Browsers aim to implement the Living HTML standard. XHTML 1.x is obsolete.

Now, I was wondering if in XHTML the DOCTYPE tag

The DOCTYPE is not a tag.

some header elements need to be closed with a forward slash as well

In XHTML:

  • Every element needs to be explicitly ended
  • <foo /> and <foo></foo> are equivalent

Note that if you serve your XHTML with a text/html Content Type then you should follow the Appendix C compatibility guidelines which make the XHTML as close as possible to HTML so it doesn't break when parsed as HTML.


I strongly recommend not using XHTML.

Upvotes: -1

Alohci
Alohci

Reputation: 82976

I understand XHTML will one day replace HTML

That understanding is incorrect. There is no expectation that XHTML will replace HTML. There was a time, around 20 years ago, when that expectation was widely held, but it has long since passed. It is however, a valid alternative syntax.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" /> <--- note the forward slash at the end

In XHTML, the DOCTYPE MUST NOT have a forward slash at the end. However, the DOCTYPE can be omitted entirely. If you choose to use a DOCTYPE with XHTML, the correct form is exactly the same as with the HTML syntax: <!DOCTYPE html>.

XHTML is not identified via the DOCTYPE, but by the content type in the HTTP content-type header. This should be application/xhtml+xml. If you do not do this, the browser will treat your file as HTML, not XHTML, and your attempt to use XHTML will be in vain.

<meta charset="utf-8" /> <--- note the forward slash at the end**

Meta and link elements, and all other void elements, must be explicitly closed, either via the forward slash at the end, or by a close tag e.g. </meta> immediately following the start tag.

I expected that even though there may be an error in syntax the browser will still display the file as intended, so I cannot verify if whether my syntax is correct or not.

If you use Firefox, it will not display the XHTML file at all if it contains a syntax error, and will only display an error message. Otherwise, use a validator. Again, make sure that the file is served with the correct content type.

Upvotes: -1

Related Questions