Skip to main content

How to Write a Basic HTML Page

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Every web page is a plain text file with the same few parts: a DOCTYPE line, then an html element that holds a head, with information about the page, and a body, with what people see. Save the file with a name that ends in .html, and any browser opens it.

Syntax​

Syntax
<!DOCTYPE html>
<html lang="<language>">
<head>
<meta charset="utf-8">
<title><page title></title>
</head>
<body>
<content>
</body>
</html>
PartWhat it does
<!DOCTYPE html>Tells the browser that the file is a modern HTML page. It's always the first line.
<html lang="<language>">Holds the whole page. <language> is the page's language code, such as en for English.
<head>Holds information about the page. Nothing in it appears on the page itself.
<meta charset="utf-8">Says the file is saved as UTF-8, the encoding that covers letters such as é and symbols such as €.
<title><page title></title>The page's title, which the browser shows on its tab.
<body>Holds everything people see on the page.
<content>What the page shows: headings, paragraphs and other elements.

Most elements have an opening tag, such as <body>, and a closing tag with a slash, such as </body>. Everything between the two is inside the element. A few elements, such as <meta>, have no closing tag. MDN's Basic HTML syntax page explains tags and elements in more detail.

The Example​

The example is the smallest useful page: a heading that says Hello! and one paragraph under it.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, a complete HTML page: the DOCTYPE line, an html element with lang en, a head with a UTF-8 charset and the title My first page, and a body with the heading Hello! and the paragraph This is my first web page.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My first page</title>
</head>
<body>
<h1>Hello!</h1>
<p>This is my first web page.</p>
</body>
</html>

In body, <h1> is the page's main heading and <p> is a paragraph.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: a large bold heading Hello! and under it the paragraph This is my first web page.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The browser showed what's in body: the heading large and bold, the paragraph in normal text. That look is the browser's default, because the page has no CSS.
  • Nothing in head appeared on the page. The title, My first page, went to the browser's tab.
  • The DOCTYPE line put the page in standards mode, the mode that modern pages are written for. Chromium reported the page's mode as CSS1Compat, its name for standards mode.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out <!DOCTYPE html>The browser switches to quirks mode, which copies how browsers laid out pages before web standards: for a copy of the page without that line, Chromium reported BackCompat instead of CSS1CompatKeep <!DOCTYPE html> as the very first line
Forgetting a closing tag, such as </h1>What follows ends up inside that element: with </h1> missing, the paragraph became part of the heading and came out large and bold, at 32 pixelsClose every element you open

Troubleshooting​

SymptomLikely causeFix
A large part of the page is in big bold textA heading's closing tag is missingAdd it, such as </h1>
The title doesn't appear on the pageThat's expected: title only sets the text on the tabAdd a heading in body for a title people see on the page
Your changes don't showThe browser still shows the page as it was when you opened itSave the file and reload the page

FAQ​

Do I have to indent the code?​

No. Indents are for people reading the file. The browser ignores the spaces between elements: the two spaces in front of <h1> didn't move the heading at all.

Does the file have to be called index.html?​

No. Any name that ends in .html works. index.html is the name web servers usually send when someone opens a folder's address, which is why a site's main page usually has that name.

What does lang="en" do?​

It tells browsers and assistive technology which language the page is in. MDN's html element page explains that it helps screen readers announce the page in the right language.

Do I really need the meta charset line?​

Keep it. Chromium showed é and € correctly in a test page without it, because it worked out the encoding by itself, but the line means no browser has to guess. MDN's meta page explains that utf-8 is the only encoding allowed there.

Conclusion​

Every HTML page starts from the same lines: the DOCTYPE, html, a head with a charset and a title, and a body with the content. Close every element you open, and the browser shows the page the way you wrote it.

Additional Resources​