HTML optional tag while lang attribute necessary

I’ve been looking up best coding practices for HTML on the web.
I found that the html element is optional, as per the information in the html standard document

I omitted the html opening and closing tags and used the W3C code checker
I got a warning:
Consider adding a lang attribute to the html start tag to declare the language of this document.

So, this is a bit confusing here. Should I clean up my code from all unnecessary elements, attributes and quotes, or should I keep everything to avoid the conflict.

Here is the link for the HTML checker:

And, the link for the HTML standard:
1 Like

Be very careful about “optimising” your HTML. Why do you want to remove the <html> tag? Yes, it’s optional in html5 (the browser will automatically add it in), but as the warning indicates, for the sake of avoiding a few keystrokes you have now lost the ability to specify a language for the document (which is important from an accessibility point of view), and [from a code PoV] your HTML document is going to look bizarre to almost anyone else looking at the code

The browser will infer a lot if you do leave optional tags/attributes out, but what you’re often doing by manually stripping them out is losing an ability to control things like the styling (the browser will just guess what you wanted to do). There’s very little point, and in exchange, you start hitting weird issues. For example, this is valid:

<img src=foo alt=Bar>

But it doesn’t do the same as this:

<img src=foo alt=Bar>

This is valid:

  <button>Click me!</button>

But it doesn’t do the same as this:

  <button type="button">Click me!</button>

Etc etc


Thank you very much for the explanation.
I guess I’ll keep all the optional tags anyway.
After reading your examples, I think it’s the safest thing to do.

Out of interest, why are you combing the HTML spec looking for optimisations? HTML isn’t normally something that needs a great deal of optimisation.

By design, it’s quite forgiving – even fairly broken HTML may still produce something legible in the browser. So you technically don’t need to include a lot of stuff. But there are only generally two reasons for not including the optional stuff:

  1. You want to minify the HTML. But for that you want to use a tool that do it automatically. The stuff that is optional has a large set of rules that apply which allow it to be optional. You need to learn all of those rules to do it manually, easier to just use a tool if you really need small HTML file sizes.
  2. For personal entertainment. So it’s a kinda fun puzzle I guess: “what’s the smallest legal amount of HTML that’s a valid webpage?”. But it’s not very practical.

Hi Dan,
Well, I was reading about HTML and found that some tags are optional, so I tried to minimize the size of my code to make it look neat without any unnecessary bulk.
I couldn’t find a tool online to automatically remove HTML optional tags and I think it’s not that necessary.
For the second question, here’s what I came up with. I had to include the HTML tag to add the lang attribute as required by the code checker.

<!DOCTYPE html>
<html lang=en>
<title>Hello World</title>
<h1>Hello World</h1>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.