My fabulous document

Doctypes and markup styles

The doctype appears just above the tag, at the very start of each document you write:

     . etc

The doctype explains what type of HTML is to be expected and therefore what spec validators (for example the W3C HTML validator) should validate your document against. In this article of the Web Standards Curriculum we will explore the different doctypes you are likely to come across on your journey around the Web, as well as looking at how XHTML and HTML differ.

Standards versus quirks mode

The doctype also serves to make the browser render the page in so called «standards mode». In standards mode, browsers generally try to render the page according to the CSS specifications — they trust that the person who created the document knew what they were doing.

On the other hand, if a browser finds an outdated, incomplete of missing doctype at the start of the page, they use “quirks mode”, which is more backwards compatible with old practices and old browsers. Quirks mode assumes that the document is old or that it has not been created with web standards in mind — it means that the web page will still render, but the browser will work a bit harder in doing so, and you’ll likely get a strange or ugly result, which you weren’t quite expecting. The differences are mostly related to how CSS is rendered, and only in a few cases about how the actual HTML is treated. As a web designer or developer, you will get the most consistent results by making sure that all browsers use their Standards rendering mode, hence you should stick to web standards, and use a proper doctype!

Читайте также:  Javascript check if function called

The HTML5 doctype

In this course, we are sticking with the HTML5 docype:

There is no disadvantage to using this doctype, and it is certainly a lot easier to remember than the others! This is the one we’d recommend you use going forward, as even if you don’t plan to start using any of the new HTML5 features in your work yet, it will still validate most of the HTML 4/XHTML 1.0 features (there are a couple of exceptions where features have been removed from the spec, but as you’ll learn later, validation is merely a useful tool, and not the be all and end all of everything), and will be future proof for when you do start using new HTML5 features. There are however, other doctypes that you may come across when working on various projects. Let’s look at some of the others you might bump into.

The HTML 4.01 strict doctype

The HTML 4.01 strict doctype validates against the HTML 4.01 spec, although it doesn’t allow any presentational markup or deprecated elements (such as elements) or framesets to be used. It validates loose HTML style markup, such as minimized attributes and non-quoted attributes (eg required , rather than required=»required» ).

The HTML 4.01 transitional doctype

The HTML 4.01 transitional doctype validates against the HTML 4.01 spec. It allows some presentational markup and deprecated elements (such as elements) but not framesets. Again, it validates loose HTML style markup.

The XML 1.0 strict and transitional doctypes

These are the exact XHTML 1.0 equivalents of the HTML 4.01 doctypes we discussed above, so functionally they are the same, except that they won’t validate loose HTML style markup: it all needs to be well formed XML.

The HTML 4.01 and XML 1.0 frameset doctypes

If you want to use framesets and still have your markup validate, you can use one of these two doctypes:

They are functionally the same as HTML 4.01 transitional and XHTML 1.0 transitional respectively, but they allow the use of framesets. But please don’t use framesets: they are evil and outdated. We don’t even tell you have to use them anywhere in this course. Everytime you use a frameset on a web page, a fairy dies. So please don’t. Think of the fairies.

Other doctypes

You may also occasionally see other doctypes on your travels, but they are not mentioned here because they are outdated. Don’t use any other doctypes. Not by choice, anyway.

HTML versus XHTML syntax

Table 1 shows the main syntax differences between HTML and XHTML:

HTML XHTML
Elements and attributes are case insensitive, eg is the same thing as . Elements and attributes are case sensitive; they are all lowercase.
Certain elements don’t need a closing tag (eg paragraphs,

), while others (called “empty elements”) shouldn’t have a closing tag (eg images, ).

All elements must be explicitly closed (eg

A paragraph

). Elements without content should be closed using a slash in the start tag (eg and


mean the same thing).

Attribute values may be written without being enclosed in quotes. Attribute values must be enclosed by quotes.
Shorthand can be used for certain attributes (ie ). The full attribute form must be used for all attributes (eg ).

Table 1: Differences between HTML and XHTML.

In terms of what syntax style you should use, pick something you are comfortable with. We’d recommend that you start off using strict XML syntax, as it is guaranteed to work in any situation. Later on you can adjust your style to suit, when you understand what you are doing a bit better. Just remember these rules:

  • If you are using the HTML5 doctype or an HTML 4.01 doctype, you can use whatever style you want
  • If you are using an XHTML doctype, you need to use XML well-formed syntax
  • Whichever style you are using, best practice is to:
    • Close all open elements, so for example

      paragraph

      , not

      paragraph

    • Nest them properly, for example

      paragraph with bold word

      , not

      paragraph with bold word

    This last rule is very important. If you don’t do this, your HTML will not be well formed, and CSS and JavaScript may not work reliably, as they rely on having a well-formed Document Object Model (DOM). For more on the HTML DOM, read Growing trees.

    Serving «real» XML

    You may also be interested to know that most of the XHTML on the Web is actually HTML written with well-formed XML syntax. Even if the doctype is an XHTML one, it will be sent to the client as HTML unless you:

    • save the file with an .xhtml file extension
    • Include a line of code called the XML declaration before your doctype, which looks like this:

    We wouldn’t recommend that you this however: old versions of Internet Explorer (6 and below) switch into Quirks mode if they find the XML declaration at the start of the document, which is bad, as we looked at earlier. In addition, IE 6-8 will try to download files saved as XHTML rather than display them in the browser, which you definitely do not want!

    Therefore you should just stick to not trying to serve documents as proper XHTML! Carry on people, nothing to see here.

    Summary

    And that’s it for the doctype — pretty much all you need to know.

    Источник

    HTML Declaration

    All HTML documents must start with a declaration.

    The declaration is not an HTML tag. It is an «information» to the browser about what document type to expect.

    In HTML 5, the declaration is simple:

    Browser Support

    Older HTML Documents

    In older documents (HTML 4 or XHTML), the declaration is more complicated because the declaration must refer to a DTD (Document Type Definition).

    HTML Elements and Doctypes

    Tips and Notes

    Tip: The declaration is NOT case sensitive.

    Examples

    Unlock Full Access 50% off

    COLOR PICKER

    colorpicker

    Join our Bootcamp!

    Report Error

    If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

    Thank You For Helping Us!

    Your message has been sent to W3Schools.

    Top Tutorials
    Top References
    Top Examples
    Get Certified

    W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

    Источник

    About doctype in html

    Что за загадочный доктайп и зачем он нужен? Почему не стоит забывать о нём? Рассмотрим современный доктайп и его исторические версии.

    Время чтения: меньше 5 мин

    Обновлено 18 сентября 2021

    Кратко

    Скопировать ссылку «Кратко» Скопировано

    или «доктайп» — это сокращение от «тип документа» (document type). Доктайп говорит браузеру: «работай со страницей в стандартном режиме, пожалуйста». Единственный доктайп, который вам нужно знать — это . Поставьте его первой строчкой HTML-документа, и браузер обработает страницу правильно.

    Пример

    Скопировать ссылку «Пример» Скопировано

               DOCTYPE html> html lang="ru"> head> meta charset="UTF-8"> meta name="viewport" content="width=device-width,initial-scale=1"> title>Докаtitle> link rel="stylesheet" href="/assets/styles/main.css"> head> html>      

    Как пишется

    Скопировать ссылку «Как пишется» Скопировано

      DOCTYPE html>      

    Как понять

    Скопировать ссылку "Как понять" Скопировано

    Есть несколько исторических версий HTML, включая специальную — XHTML. Все они обозначались своими доктайпами и их вариациями, которые вы можете встретить в старом коде:

      DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">      

    Сегодня доктайп по спецификации нужен исключительно потому, что без него браузеры переключатся в режим совместимости со старым кодом (quirks mode). Поверьте, вы не хотите оказаться в режиме совместимости. Чтобы переключить браузер в стандартный режим (standards mode), достаточно написать в самом начале документа: . Это единственный стандартный доктайп для современного HTML. Всё, что сверх этого — только риск лишней путаницы и для автора, и для браузера.

    Теги и атрибуты в HTML не зависят от регистра (хотя доктайп это не тег, строго говоря), поэтому все вариации ниже абсолютно равнозначны, даже последняя — но лучше не надо так писать.

         DOCTYPE html> DOCTYPE HTML> doctype HTML> doctype html> DoCtYPe hTMl>      

    Доктайп стал для HTML чем-то вроде use strict в JS.

    На практике

    Скопировать ссылку "На практике" Скопировано

    Дока Дог советует

    Скопировать ссылку "Дока Дог советует" Скопировано

    🛠 Если сравнивать HTML с устройством человека, то — это его вид. Важно знать, что было раньше, но не надо быть австралопитеком. Единственный вариант, который можно использовать — это , все остальные устарели.

    Алёна Батицкая советует

    Скопировать ссылку "Алёна Батицкая советует" Скопировано

    🛠 Программисты прошлого соревновались в том, кто может написать один из доктайпов по памяти и без ошибок.

    Сейчас такого веселья уже не видать. Во-первых, доктайп усох: — сложно написать это с ошибками. А во-вторых, все разработчики используют плагин Emmet для написания стандартной разметки страницы. Гораздо удобнее напечатать ! и нажать клавишу Tab , чем запоминать доктайп. Аналогичного результата можно добиться, набрав в файле html : 5 и нажав Tab.

    Так что нужно просто знать, что такое определение DOCTYPE должно быть во всех HTML-документах, где оно должно находиться в разметке и за что отвечает. Остальное сделает машина.

    Источник

Оцените статью