Computer >> Computer tutorials >  >> Programming >> HTML

Declaring an HTML Doctype

When starting a new HTML document, one of the first things to do is write your document type declaration. A doctype declaration tells the browser that the page to be rendered is written in HTML. To declare an HTML5 doctype, `<!DOCTYPE html>` is required in the first line of your HTML document. Doctype declaration for HTML5 is not case sensitive and does not require a closing tag.

What to Include in a New HTML Document

In addition to doctype declaration, you’ll also need to include `html`, `head`, and `body` tags.

<!DOCTYPE html>
<html>
    <head>
        <!-- Metadata for the site. -->
    </head>
 
    <body>
        <!-- Content the viewer will see. -->
    </body>
</html>

HTML Element

The HTML tag communicates to the web browser that everything within the opening html tag, `<html>` and the closing html tag, `</html>` is HTML text.

Head Element

The head element stores metadata for the browser. This metadata could include information like what language the page is written in, a title for the web page, or a link to a style sheet.

Body Element

The body element contains the information that the viewer will see. There are several tags included within the body element. These might include a <p> tag for paragraphs, <ol> tag for ordered lists, <li> tag for list items, or the <a> tag for links.

Other Doctypes You May Find

Declaring an HTML Doctype

For HTML5 documents, you can declare the doctype with the code `<!DOCTYPE html>`, but you may also find legacy code written for previous doctypes. The doctype declaration prior to HTML5 was more complicated because it referred to a DTD, or Document Type Definition. Previous versions of HTML doctype declarations include:

HTML 4.01 Strict

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"https://www.w3.org/TR/html4/strict.dtd"> 

HTML 4.01 Transitional

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"https://www.w3.org/TR/html4/loose.dtd"> 

HTML 4.01 Frameset

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" 
"https://www.w3.org/TR/html4/frameset.dtd"> 

XHTML 1.0 Strict

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

XHTML 1.0 Transitional

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

XHTML 1.0 Frameset

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" 
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> 

XHTML 1.1

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
"https://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 

The good news? There is no reason to use one of the older doctypes if you are writing a new HTML document!

Conclusion

In conclusion, each HTML document must begin with a doctype declaration. This declaration communicates to the browser that the web page will be rendered in HTML. If you’re new to HTML, check out our guide here: Learn HTML: A Guide to Learning Hypertext Markup Language, or read our article How Long Does It Take to Learn HTML?

81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.

The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job.