Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Style Guide Google vs W3Schools (Omit Optional Tag)

Tags:

html

I was reading some style guides and saw a conflicting recomandation regarding the Optional Tags.

Google says:

Omit optional tags (optional). For file size optimization and scannability purposes, consider omitting optional tags. The HTML5 specification defines what tags can be omitted.

(This approach may require a grace period to be established as a wider guideline as it’s significantly different from what web developers are typically taught. For consistency and simplicity reasons it’s best served omitting all optional tags, not just a selection.)

W3CSchools says:

Close All HTML Elements In HTML5, you don't have to close all elements (for example the <p> element).

We recommend closing all HTML elements:

And

We do not recommend omitting the < html > and < body > tags.

This means Google prefers:

<!-- Recommended -->
<!DOCTYPE html>
<title>Saving money, saving bytes</title>
<p>Qed.

W3CSchools prefers:

<!DOCTYPE html>
<html>
<title>Page Title</title>

<body>
  <h1>This is a heading</h1>
  <p>This is a paragraph.</p>
</body>

</html>

it is also considered "bad looking" to write this whereas google would recommend it.

<section>
  <p>This is a paragraph.
  <p>This is a paragraph.
</section>

I found it very interesting that W3CSchools makes a difference regarding the head tag

Is there any good reason to stop using the optional Tags ? Personally I found the code then less readable but that is purely opinion based and I guess with some training I would prefer one over the other.

Google stated that its for size optimization and scannability purposes but is that really a good reason ? The articles below stated some suggestions but seemed to me more opinion based and I am looking for good reasons to stop using the optional Tags

Here the resources:

  1. Google Style guid
  2. HTML5 Style Guide
  3. html-include-or-exclude-optional-closing-tags
  4. Omitting optional tags of html
like image 566
Bongo Avatar asked May 04 '16 14:05

Bongo


People also ask

Is HTML tag is optional tag?

Basically, that's the idea of optional HTML tags: you don't have to write an HTML tag if you can easily infer it from the context. For instance, there's no point in writing the <html> tag. The document is shown in the browser, so we already know it's an HTML document.

Which tags is not optional in HTML5?

The start and end tags for the html , head , and body elements are optional. An html element's start tag can be omitted if the first thing inside the html element is not a comment. An html element's end tag can be omitted if the html element is not immediately followed by a comment.

Which tag is optional tag?

The <option> tag defines an option in a select list. <option> elements go inside a <select>, <optgroup>, or <datalist> element.

Which tags are not used in HTML?

Some attributes from HTML4 are no longer allowed in HTML5 at all and they have been removed completely. img and iframe. caption, iframe, img, input, object, legend, table, hr, div, h1, h2, h3, h4, h5, h6, p, col, colgroup, tbody, td, tfoot, th, thead and tr. table, tr, td, th and body.


1 Answers

Many times we use the optional closing tags because it makes the document more readable. As Google says, removing them reduces file size but, then, most of us don't have the traffic Google does. That suggestion is for those who do. Then, again, reducing download size is always a good thing.

I often leave out the body tag altogether because even the opening tag is optional in most cases. However, there is a danger that leaving that out, and leaving out closing tags, may cause issues later on. I would say putting body tags in and closing all elements removes the possibility of causing those issues. For example, you can only leave the html and body tags out under certain situations.

Reading the spec:

An html element's start tag can be omitted if the first thing inside the html element is not a comment. An html element's end tag can be omitted if the html element is not immediately followed by a comment.

For some, this is very important. To others it's not.

It can be more of an issue for dynamically generated sites where the content is created on the fly and the surrounding elements may not be known. Does one really know that the following element will cause a div element to be closed?

like image 190
Rob Avatar answered Oct 22 '22 13:10

Rob