DEV Community

Cover image for Web Standards: A Declaration (Doctypes and Charsets)
Setor Kwame
Setor Kwame

Posted on

Web Standards: A Declaration (Doctypes and Charsets)

Doctype and charset are two meta tags that declare to the browser the type of document the browser is to expert. Doctype declares to the browser about the author's view about a document such as whether a document is XHTML or HTML and it also dictates the code that a browser should proceed with. The charset is used by the browser so that it knows which character encoding should be read by it.

Image description

The doctype declaration is the first line of a document and contains information about the type of document being used. It is also known as the Document Type Declaration or DTD.

The most common doctypes are HTML5 and XHTML 1.1. The HTML5 doctype has been around since 2004, but it was not supported by all browsers until sometime around 2010/2011. The XHTML 1.1 doctype is also widely used today.

There are many other types of doctypes that can be used for different purposes, such as for XML documents or even for CSS stylesheets.

The main reason why you should use a doctype declaration is to inform the browser what type of document it should expect to receive from you (in case there's no content-type header sent). Without a doctype declaration, browsers would simply assume that they were receiving plain text and would render your page differently (and probably incorrectly) than expected.

In addition to informing the browser about what kind of document it should expect, a doctype declaration usually also provides some additional information about how it should interpret your markup (things like which elements can be used and what attributes they may have).

Without a doctype declaration, the browser will assume that you don't care about how it renders your page (and therefore don't care about the markup being valid or not).

The doctype declaration for HTML5 is pretty simple:

<!DOCTYPE html>

Enter fullscreen mode Exit fullscreen mode

This basically informs the browser that it should interpret any markup it finds as HTML5. This makes sense since it's likely that you're using HTML5 to build your page in the first place.

In the end, which doctype you chose will depend on which one best fits your website and your audience. Is it a blog, a portfolio site, or a news site? This will help determine which doctype you choose. But in the end, there is no wrong answer—each doctype has its own positives and negatives. It's easy to get caught up in doctype wars with developers—but really, neither side is right or wrong. Ultimately, it comes down to what works for you.

Top comments (0)