Structure - HTML Tutorial
In the previous section you created simple.html. Here it is again:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>A Simple Web Page</title>
</head>
<body>
This is a simple web page.
</body>
</html>
When you opened it in your browser, you only saw one sentence. So what is all the other code doing? Let's go through it line by line.
A Quick Introduction to Tags
Most of the lines contain tags, words surrounded by angle brackets (< and >). Tags usually come in pairs:
- An opening tag, such as
<title>, marks where something starts. - A closing tag, such as
</title>, marks where it ends. Closing tags have a forward slash before the name.
The opening tag, the closing tag and everything between them make up an element. So <title>A Simple Web Page</title> is a title element. You'll learn more about elements in Elements.
The Parts of an HTML Document
<!DOCTYPE html>-
The doctype is always the very first line. It tells the browser that this is a modern HTML document, so the browser displays it using current web standards. It isn't an element, and it doesn't have a closing tag.
<html lang="en">-
The
htmlelement wraps the entire page. Everything else goes inside it.The
lang="en"part tells browsers, search engines and screen readers that the page is written in English. Screen readers use it to choose the right pronunciation, and browsers use it when offering to translate a page. If your page is in another language, use its language code instead, for examplelang="fr"for French orlang="es"for Spanish. <head>-
The
headcontains information about the page, rather than content you see on the page. This includes the title, the character encoding and, later on, links to style sheets. We'll explore it more in The Head Element. <meta charset="utf-8">-
This tells the browser which character encoding the file uses. UTF-8 can represent letters, symbols and emoji from every language, and it's the standard for the web. Without it, characters such as é or € can turn into strange symbols. Always include it, near the top of the
head. <title>-
The page title appears in the browser tab, in bookmarks and as the clickable headline in search results. Try changing it to "My First Web Page", save the file and refresh your browser. The text in the tab changes.
<body>-
The
bodyholds everything that appears in the browser window: text, images, links and so on. That's why the only thing you see is "This is a simple web page." It's the only content inside thebody.
Nesting
Notice how the elements sit inside one another. The title is inside the head, and both the head and the body are inside html. Placing elements inside other elements is called nesting, and you'll see it everywhere in HTML.
The indentation in the example isn't required, but it makes the nesting easy to see. Indenting your code is a good habit that makes it much easier to read and fix.
Tip
Every page you make should start with this same basic structure. In Visual Studio Code, you can create it instantly: in a new .html file, type ! and press Tab or Enter when the suggestion appears.
In the next section, you'll start changing your page to see how HTML behaves.