benjaminolsen.dev

Give the document a structure

What you will learn

The short example from the previous lesson works because browsers fill in some missing structure. An explicit document is easier to understand and maintain. Start it with <!doctype html>, which requests modern HTML rendering. The html element contains the document; lang="en" identifies its language for tools such as screen readers.

Inside head, put information about the page. meta charset="utf-8" tells the browser how text is encoded. title names the browser tab. The viewport setting lets mobile browsers use the device width rather than a simulated desktop width. You will investigate responsive layouts later.

Put visible content inside body. A top-level header can introduce the site, nav groups major navigation links, main contains the page's primary content, and footer contains closing information. These names describe purpose; they do not automatically position things on screen.

Use one visible main for this page. A section groups a distinct topic and normally needs a heading. Do not wrap every sentence in a section. A plain paragraph is enough when no new topic starts. Close nested elements in the reverse order you opened them: close the paragraph before closing its section.

The title and h1 serve different places. A useful tab title might be “Mina Patel | Learning journal”, while the visible heading simply says “Mina Patel”. Neither replaces the other. The complete downloaded example includes both the document shell and the visible fragment below.

Working example

The HTML shown here is the body fragment. The download includes the complete document.

<header><p>Learning journal</p></header>
<main>
  <h1>Mina Patel</h1>
  <section><h2>About me</h2><p>I enjoy making useful things.</p></section>
</main>
<footer><p>Written by Mina</p></footer>

Open in browser editor

Download working example

Try a change

Open the downloaded index.html. Change its title, main heading and footer author. Add a second section with an h2 and a paragraph inside main.

What you should observe

The browser tab and page heading both identify the author. Both sections remain inside the one main element. The footer follows main.

Check your understanding

Does adding a header element make it stay at the top when you scroll?

Read the explanation

No. It gives the content a meaning. Sticky positioning is a separate CSS decision and is not needed here.