The standard starting template for a web page is short enough to memorise and almost universally copied without being read. That is a shame, because every line in it is solving a real problem, and three of them cause visible breakage when missing. Here is the template, followed by what each part is for.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Page Title</title> </head> <body> </body> </html> The doctype is a switch, not a version number
The first line looks like it declares which version of HTML you are using. It does not really do that. It is a switch that tells the browser to use standards mode rather than quirks mode, an emulation of the broken behaviour of browsers from the 1990s that is still supported for old sites.
Leave it out and you get quirks mode, where the box model changes so that padding and borders are measured inside an element width instead of added to it. Your layout will be subtly and inexplicably wrong, and nothing in your CSS will look like the cause. This one line prevents that, which is why it goes first and takes no arguments.
The html element and the lang attribute
Everything else lives inside the root element. The lang attribute on it is small and genuinely matters. Screen readers use it to select pronunciation rules, so a page without it may be read out in the wrong accent or with mangled emphasis. Browsers use it for automatic translation offers and for choosing the correct quotation marks and hyphenation. Search engines use it as a regional signal. It costs nine characters.
The head holds instructions, the body holds content
Nothing in the head is rendered on the page. It is metadata: information about the document rather than the document itself. The body is what a visitor actually sees. Keeping the distinction clear in your head prevents the common beginner move of putting visible content in the head, where the parser will simply relocate it and leave you wondering why it appears in an unexpected place.
The charset declaration must come early
The character set declaration tells the browser how to interpret the raw bytes it received as text. Without it, or with the wrong value, accented characters, curly apostrophes, currency symbols and emoji become sequences of nonsense characters, the failure everyone recognises on sight.
UTF-8 is the correct answer in essentially every case, because it covers every writing system in use. The declaration also has to appear within the first 1024 bytes of the document, which is why it is the very first thing in the head. The browser has to guess an encoding to begin reading at all, and if it discovers your declaration late it must throw away the work and start again.
The viewport meta tag is what makes a page usable on phones
Mobile browsers default to pretending they have a screen around 980 pixels wide, then shrinking the result to fit. That behaviour exists so old desktop-only sites remain readable rather than showing a small slice of the top left corner.
The viewport tag opts out of it. Setting the width to device-width tells the browser to use the actual screen width, and the initial scale of one stops it zooming out on load. Without this tag your media queries never fire, because as far as the browser is concerned the screen is still 980 pixels wide. Every responsive design failure on a phone should send you to check this line first.
The title does more than name the tab
The title appears in the browser tab, becomes the default name for a bookmark, and is normally the clickable headline in search results. It is also the first thing a screen reader announces when a page loads, which makes it the primary way someone with several tabs open works out where they are.
Write it for a person reading a list of results rather than for a search engine. Put the specific part first, keep it under roughly sixty characters so it is not truncated, and make sure it is unique across your site. Two pages sharing a title is a reliable sign that at least one of them has not been thought about properly.
Type it out at least once
Copy this template from now on by all means, but type it manually once. The muscle memory is worth having, and the next time a page renders at the wrong scale on a phone or a layout is mysteriously eight pixels out, you will know exactly which line to look at.