Читать книгу HTML5, JavaScript, and jQuery 24-Hour Trainer - Cameron Dane - Страница 13

Part I
HTML and CSS
Lesson 1
Introduction to HTML5
An HTML Template

Оглавление

In the previous section, you wrote the simplest web page you could write. In this section, you will write a web page following a basic template that is intended to represent the simplest HTML structure you should write.

I will first present the template, and then I will walk you through it line by line. Open a new document in your text editor, and save the following as template.html:


If you open this in Chrome, and then view the DOM in the developer tools, it will look like the example in Figure 1.3.


Figure 1.3


As you can see, in this case there is far closer alignment between the content you provided in the HTML file and the normalized structure generated by the browser.

Let's now walk through each line in the document and examine its purpose.

The first line in the document is as follows:


This line defines the document type of the page. Because there have been many different HTML standards over the years, the browser uses this line to understand which of these standards the page is using, and then uses the rules applicable for this standard to interpret the content of the page and render it accordingly.

This is the HTML5 document type definition, and comes as a pleasant surprise for developers who may be accustomed to copying and pasting DOCTYPE declarations such as:


The other main surprise about this document type definition is that it does not include a version number: The document type is simply html.

Although the specification is referred to as HTML5, it defines a “living-standard” that will be subject to incremental change as and when browser vendors implement, and agree on, new features. Put another way, there will never be another version of HTML, but HTML will always continue to evolve.

The next line contains the opening html tag, which encapsulates the remainder of the document:


This tag contains an attribute called lang, which has been given the value en. Attributes provide a mechanism for providing extra meaning to tags. This particular attribute is stating that the language of the document is English.

Note

The ISO standard 639-1 defines the set of two-letter codes that can be used for languages. These can also be paired with a country code, for instance en-US. Country codes are defined in the ISO standard 3166.

As with many aspects of HTML5, although the specification defines the attributes and their expected values, it is up to the browser to decide what to do with this information. The browser may use this information to suggest a translation to a non-English speaker, or it may do absolutely nothing with this information.

The next element in the document is the head element. This is the section of the document where you can provide important metadata about the document, along with links to other files needed by the document. The head section never contains any visual components of the web page. In this particular case, the head contains one important piece of metadata:


This specifies that the character encoding of the document is UTF-8. I will not cover character encodings in this section, but the specification recommends setting this.

There is one other element that is commonly added to the head element: the title element. This is the text that the browser will display in the title bar when the web page is loaded. Therefore, add the following inside the head section:


and then view the page in Chrome; the tab header will appear as follows:


Figure 1.4


Next you come to the body element. This is where all the visual elements of the page will be described. In this particular example, the body consists of a single text string, but it is this area of the document that you will enhance in the chapters ahead to create interesting web pages.

HTML5, JavaScript, and jQuery 24-Hour Trainer

Подняться наверх