Lesson 2: Your First Page: HTML Elements and Structure
HTML, HyperText Markup Language, is the language every web page is written in, and it is simpler than it first looks. An HTML document is plain text with extra marks called tags, and those tags tell the browser what each piece of text is: a heading, a paragraph, a link, an image. You do not need to memorise every tag before you start, because a small set covers most pages, and the rest you can look up as you need them. In this lesson you will write your first page, save it, and open it in your browser, which is the moment web development stops being theory and starts being something you can see.
Elements, tags and attributes
An element is a piece of content wrapped in tags. A paragraph looks like this in code: <p>Hello, world!</p>. The opening tag <p> says a paragraph starts here, the closing tag </p> says it ends, and the text in between is the content. Most elements work this way, though a few, such as images and line breaks, have no closing tag because they carry no text content. Tags can also carry extra information called attributes, written inside the opening tag: <a href="https://alpha.unb.college">Alpha School</a> makes a link, and the href attribute says where the link goes. Attributes always have a name, an equals sign and a value in quotes, and getting that pattern right is half of writing correct HTML.
The skeleton every page shares
Every HTML page has the same skeleton, and it is worth learning by heart. It starts with <!DOCTYPE html>, which tells the browser this is a modern HTML document. Then comes the <html> element, which wraps everything, and inside it two parts: the <head>, which holds information about the page that is not displayed, such as its title and its character encoding, and the <body>, which holds everything the visitor sees. The head contains a <title>, the text shown on the browser tab, and a <meta charset="utf-8"> line, which tells the browser how to read characters such as accented letters. The body is where your headings, paragraphs and links live. Copy the example below into a file called index.html, open it in your browser, and you have published your first page, at least on your own computer.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My First Page</title>
</head>
<body>
<h1>Hello, world!</h1>
<p>This is my first web page.</p>
</body>
</html>
Headings and paragraphs
Headings run from <h1> to <h6>, and they describe the structure of your page, not just its appearance. There should be exactly one <h1> per page, naming its main topic, and then <h2> headings for the major sections, <h3> for subsections, and so on, in order, without skipping levels. That matters because screen readers and search engines use headings to understand a page, and a page with three <h1> elements or a jump from <h1> to <h4> is confusing to both. Paragraphs use <p>, and you should let the browser handle line breaks: write one paragraph per idea, and do not use empty paragraphs or repeated <br> tags to create space, because that is the job of CSS, which you will meet in Unit 2. Keep your text natural, write it for a human reader first, and the structure will take care of itself.
Saving, opening and checking your work
Save your file with the extension .html, in a folder you can find again, and open it by double-clicking it or by dragging it into a browser window. Every time you change the file, save it and refresh the browser to see the result, and if something looks wrong, check the most common mistakes first: a missing closing tag, a missing closing quote on an attribute, or a tag name spelled differently in the opening and closing tag. Browsers are forgiving and will usually still show something, but forgiving does not mean correct, and a page that looks right in one browser may break in another if the HTML is sloppy. From this lesson on, get into the habit of validating your pages: the free W3C Markup Validation Service will read your file and list every error, and fixing those errors as you go is the fastest way to build good habits.
Key Terms
- HTML
- HyperText Markup Language, the language that describes the content and structure of a web page.
- Element
- A piece of content wrapped in tags, such as a paragraph or a heading.
- Tag
- A mark in angle brackets, such as <p>, that tells the browser what something is.
- Attribute
- Extra information inside an opening tag, such as href on a link, written as name="value".
- Head
- The part of a page that holds information about it, such as the title, and is not displayed.
- Body
- The part of a page that holds everything the visitor sees.
- DOCTYPE
- The first line of a page, , which tells the browser to use modern HTML rules.
Discussion Questions
- Why does HTML separate the head (information about the page) from the body (what the visitor sees)?
- Browsers try to display broken HTML anyway instead of showing an error. Is that a good design choice? What are the downsides?
- Why should there be only one h1 on a page, and why does the heading order matter for people using screen readers?
Practice Questions
- Write the HTML for a paragraph that says “Welcome to my site.”
- What does the href attribute do in a link?
- Which two parts sit inside the <html> element, and what does each hold?
- What is the purpose of the line <!DOCTYPE html>?
- Why is it a mistake to use an <h1> just to make text big?
Show answers
- <p>Welcome to my site.</p> The opening tag starts the paragraph, the text is the content, and the closing tag with the slash ends it.
- It says where the link goes. In <a href="https://alpha.unb.college">Alpha School</a>, href holds the destination address.
- The head and the body. The head holds information about the page, such as its title, and the body holds everything the visitor sees.
- It tells the browser this is a modern HTML document. Without it, browsers may fall back to older, inconsistent ways of displaying the page.
- Headings describe structure, not appearance. An h1 says “this is the main topic”, and using one for styling confuses screen readers and search engines; size is a CSS job.