HTML (HyperText Markup Language) is the language that describes the content and meaning of a web page: this is a heading, this is a paragraph, this is a link. Appearance (colors, layout) comes later with CSS, and interactions with JavaScript.
Tags, elements and attributes
An HTML element is generally written with an opening tag, content and a closing tag:
<!-- opening tag, content, closing tag -->
<p>Hello everyone</p>
<!-- an attribute refines the element: name="value" -->
<a href="contact.html">Contact us</a>
Some elements are empty: they have neither content nor a closing tag, for example <meta>, <img>, <br> or <input>.
Elements nest like Russian dolls: you always close first what you opened last.
<p>A word <strong>important</strong> here.</p> <!-- correct -->
<p>A word <strong>important</p></strong> <!-- incorrect -->
The minimal skeleton
Here is the basis of every modern page:
<!DOCTYPE html> <!-- the browser's standards mode -->
<html lang="fr"> <!-- main language of the page -->
<head>
<meta charset="UTF-8"> <!-- encoding: accents display correctly -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My first site</title> <!-- text of the tab and of search engines -->
</head>
<body>
<h1>Welcome</h1>
<p>This is my first page.</p>
</body>
</html>
Line by line:
<!DOCTYPE html>is not a tag but a declaration: it tells the browser to use standards mode.<html lang="fr">is the root of the document. Thelangattribute helps screen readers pick the right pronunciation and search engines classify the page.<head>groups the metadata, invisible in the page: encoding, mobile display setting (viewport), tab title, and later the links to CSS stylesheets.<body>contains everything the user sees.
The document tree
The browser turns this text into a tree (the DOM): html has two children, head and body, which themselves have children. Keeping this picture in mind will help you a lot with CSS and JavaScript.
Good habits
- Indent your code: each level of nesting is shifted by two spaces.
- Write tags and attributes in lowercase and put quotes around values.
- Add comments with
<!-- ... -->; they are not displayed. - Save the file as UTF-8 with the
.htmlextension, then open it in your browser.
HTML is forgiving: an error does not crash the page, the browser guesses. That is handy, but it hides errors. Get into the habit of writing clean code from the start.
Exercises
Solutions and tests stay on the server. In the student space, your code is run and checked, and the tutor gives hints before the solution.
Complete the skeleton
Complete the page: add the
lang="fr"attribute to<html>, a<meta charset="UTF-8">tag and a<title>containing Ma page.<!DOCTYPE html> <html> <head> <!-- ajoutez l'encodage et le titre --> </head> <body> <p>Bonjour</p> </body> </html>A complete page
Write a complete page from scratch: doctype,
<html lang="fr">,<head>with UTF-8 charset, meta viewport and the title Portfolio, then a<body>containing an<h1>“Bonjour, je suis Alex” and an introductory paragraph.<!-- écrivez toute la page ici -->Where does the tab title go?
In which element should the
<title>tag be placed?- In <body>
- In <head>
- Right after <!DOCTYPE html>, outside <html>
End-of-lesson quiz
What is for?
- Displaying the title
- Activating the browser's standards mode
- Importing CSS
Which of these elements is an empty element (no closing tag)?
- <p>
- <title>
- <meta>
Why add lang=“fr” to ?
- To translate the page automatically
- To help screen readers and search engines know the language
- It is mandatory for the page to display
The rest of the course
- Headings, Paragraphs and Lists15 min
- Links and Images15 min
- Structuring a Page with Semantic HTML15 min
- Data Tables15 min
- Forms20 min