Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<DOCTYPE html> versus <html> - rendering problems firefox and chrome

Tags:

html

doctype

have been using no DOCTYPE but rather simply starting with <html> as per HTML5 standards (as I understood them). everything going ok.

began using Jade, which insists on DOCTYPE. using <!DOCTYPE html> - pages no longer render correctly(?).

as an easy and trivial example (behavior is same on firefox and chrome):

<html>
    <body >
        <div style='height:50%; background-color:pink;'></div>
        <div style='height:50%; background-color:blue;'></div>
    </body>
</html>

render just fine - have page pink, half blue

<!DOCTYPE html>
<html>
    <body >
        <div style='height:50%; background-color:pink;'></div>
        <div style='height:50%; background-color:blue;'></div>
    </body>
</html>

renders two skinny DIV's you can't see.

  1. what's going on?
  2. thought DOCTYPE was being deprecated for HTML5
  3. what should I be doing?
like image 682
cc young Avatar asked Oct 13 '12 05:10

cc young


People also ask

Is DOCTYPE HTML really necessary?

Even though a doctype may look a bit strange, it is required by the HTML and XHTML specifications. If you don't include one you will get a validation error when you check the syntax of your document with the W3C Markup validator or other tools that check HTML documents for errors.

Are there changes to DOCTYPE for HTML5 what browsers support HTML5?

HTML5 is now compatible with all popular browsers (Chrome, Firefox, Safari, IE9, and Opera) and with the introduction of DOCTYPE, it is even possible to have a few HTML features in older versions of Internet Explorer too.

Are there changes to DOCTYPE for HTML?

The only thing that such a doctype change will affect is validation. Other than that, the doctype declaration only affects browser mode (quirks / almost standard / standard), and XHTML 1.0 and HTML5 doctype have the same effect in this respect. If you don't use a validator, there is no reason to change.

What happens when there is no DOCTYPE?

The absence of the DOCTYPE or its incorrect usage will force the browser to switch to quirks mode. It means that the browser will do its best to layout the page that is considered to be old or created against web standards.


2 Answers

Whoever who told you that DOCTYPE is deprecated is either playing a prank on you or is plain ignorant.


W3C on its article HTML5 differences from HTML4 on the section Syntax, sub section 2.2 clearly states this.

The HTML syntax of HTML5 requires a doctype to be specified to ensure that the browser renders the page in standards mode. The doctype has no other purpose and is therefore optional for XML. Documents with an XML media type are always handled in standards mode. [DOCTYPE]

The doctype declaration is <!DOCTYPE html> and is case-insensitive in the HTML syntax. Doctypes from earlier versions of HTML were longer because the HTML language was SGML-based and therefore required a reference to a DTD. With HTML5 this is no longer the case and the doctype is only needed to enable standards mode for documents written using the HTML syntax. Browsers already do this for <!DOCTYPE html>.

And as to, why the behavior when you set <!DOCTYPE html> in your example.

This behavior is expected. This is because body is a block level element. So it has height, by default, in a shrink-to-fit model and width, by default, in an expand-to-fit model. Setting style="height:100%;" in the body tag allows body to take up the whole of the height available and displays your two divs.

like image 174
naveen Avatar answered Oct 21 '22 17:10

naveen


  1. Without a DOCTYPE, the page is rendered in Quirks Mode. This means dozens of oddities. The one you have encountered is #3 in my list of Quirks Mode phenomena: percentage heights for elements are applied, using the available height as reference, even when the enclosing block has height: auto (the default); by the specs, the height is determined by the require­ments of the content.
  2. No, DOCTYPE is not deprecated according to HTML5 drafts – on the contrary, it is required, but any form other than <!DOCTYPE html> is declared obsolete.
  3. For new pages, design them to work in “Standards Mode” (and of course use <!DOCTYPE html>). For old pages, it depends.

In the given case, to make the browser’s rendering area divided in the intended way, set the height of the html and body elements, thereby making percentage heights apply to elements inside the body even in “Standards Mode”:

<style>
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}
</style>
like image 29
Jukka K. Korpela Avatar answered Oct 21 '22 15:10

Jukka K. Korpela