Skip to content

Lesson 2 of 52HTML

HTML Basics

Learn the grammar of HTML: elements, tags, attributes, nesting, the page skeleton, and why the browser ignores your extra spaces.

16 min read0/5 questions

HTML has a small, consistent grammar. Once you can read one element, you can read them all, because every heading, link, image and button follows the same pattern. This lesson teaches that pattern: how tags open and close, how attributes add extra information, how elements fit inside each other, and the skeleton every page needs. You will also meet two behaviours that surprise every beginner: the browser squashes your spaces together, and it quietly repairs broken HTML instead of showing an error.

Anatomy of an element

An element is one piece of a page: a heading, a paragraph, a link. You write most elements as an opening tag, some content, and a closing tag:

<p>Preheat the oven to 200 degrees.</p>
  • <p> is the opening tag. The p is the element's name, short for paragraph.
  • Preheat the oven to 200 degrees. is the content.
  • </p> is the closing tag. The forward slash means "this element ends here".

Tag names are not case sensitive, so <P> works too, but everyone writes them in lowercase.

Attributes

Attributes add extra information to an element. They go inside the opening tag, as a name, an equals sign and a value in quotes:

<a href="https://example.com" title="Visit the example site">Example</a>

Here a is a link element (the "a" stands for anchor). The href attribute says where the link goes, and title gives a tooltip when you hover. An element can have many attributes, separated by spaces, in any order.

A few attributes work on almost every element. id gives an element a unique name on the page, class puts it into one or more groups for styling, and lang says what language its text is in. Some attributes need no value at all: these are boolean attributes, which are on if present and off if missing, like disabled on a button.

HTML & CSS
<h1 class="title" title="I am a tooltip">Pancake Night</h1><p>Hover over the heading to see its title attribute.</p><button>Order now</button><button disabled>Sold out</button>

Preview

Try changing the tooltip text, then remove the word disabled from the second button and watch it come back to life. Add disabled to the first one.

Match the pairs0/5 matched

Match each part of <a href="/menu">See the menu</a> to its name.

Choose an item, then its match on the other side.

Nesting: elements inside elements

Elements can contain other elements. This is called nesting, and it is how a page gets its structure. The element on the outside is the parent and the ones inside are its children.

<p>Serve with <strong>fresh</strong> berries.</p>

The strong element (important text, shown in bold) sits inside the p. The rule is simple: close elements in the reverse order you opened them, like closing boxes inside boxes. The inner box must be closed before the outer one.

<!-- Wrong: the tags cross over -->
<p>Serve with <strong>fresh berries.</p></strong>
 
<!-- Right: inner closes first -->
<p>Serve with <strong>fresh berries.</strong></p>

Because everything sits inside something else, a page forms a tree. The browser builds exactly this tree when it reads your HTML, and it is the DOM you met in the last lesson.

A small page as a tree
htmltitleheadh1strongpliliulbodyroot

Indenting your code so each child sits two spaces further in than its parent makes the tree visible in your editor. The browser does not need the indentation, but people reading your code do.

Quick check
Which line is correctly nested?
Need a nudge? A hint rules out a wrong answer.

Void elements

A few elements cannot have content, so they have no closing tag. These are called void elements. The common ones are img (an image), br (a line break), hr (a thematic break, usually drawn as a line), input (a form field), meta and link.

<img src="pancakes.jpg" alt="A stack of pancakes with berries">
<br>
<input type="email">

You will sometimes see them written with a slash at the end, like <br />. That style comes from an older, stricter version called XHTML and from JSX in React. In HTML the slash is allowed but does nothing, so both forms are fine. Pick one and be consistent.

Quick check
Which of these is a void element that must not have a closing tag?
Need a nudge? A hint rules out a wrong answer.

The page skeleton

Every full HTML document follows the same outline. You saw it in the last lesson; here is what each line does.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Pancake Night</title>
  </head>
  <body>
    <h1>Pancake Night</h1>
    <p>Every Friday, 6pm.</p>
  </body>
</html>
  • <!DOCTYPE html> tells the browser to use modern standards mode. Without it, browsers fall back to "quirks mode", which copies the odd behaviour of 1990s browsers. It is not a tag, just a declaration, and it always comes first.
  • <html lang="en"> wraps the whole page. The lang attribute tells screen readers which pronunciation to use and helps translation tools.
  • <head> holds information about the page that is not shown in the page itself: the character set, the title in the browser tab, links to stylesheets, and descriptions for search engines.
  • <meta charset="utf-8"> makes sure every character displays correctly, from accented letters to Japanese text.
  • The viewport meta tag makes the page fit phone screens properly. You will learn more about it in the responsive design lesson.
  • <title> is the text in the browser tab and in search results.
  • <body> holds everything visible: all the content people actually read.
Put it in order8 steps

Put the skeleton lines in the order they appear in a document.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.

Comments

A comment is a note for humans that the browser ignores. It starts with <!-- and ends with -->:

<!-- Opening hours: update these every season -->
<p>Open Friday to Sunday.</p>

Comments are handy for explaining something unusual or temporarily hiding a line while you experiment. Remember that anyone can read them with devtools, so never put passwords or private notes in a comment.

Whitespace collapses

Here is the surprise. In HTML, any run of spaces, tabs and line breaks inside text collapses into a single space. The browser decides where lines wrap, not you.

HTML & CSS
<p>This has lots of spaces.</p> <p>This is split over several lines.</p> <p>To force a new line<br>use a br element.</p> <p>Paragraph one.</p><p>Paragraph two.</p>

Preview

Try adding more spaces and Enter presses inside the first two paragraphs: nothing changes. Then add another <br> somewhere and watch the line break appear. Notice that each <p> gets space above and below it automatically.

Use <br> only where a line break is part of the content itself, such as a postal address or a poem. For space between blocks of text, use separate paragraphs, and for visual spacing use CSS. Stacking <br><br><br> to push things down is a classic beginner habit to avoid.

Block and inline elements

Look at the preview above again. Each paragraph starts on a new line and stretches across the full width, while strong or a sits inside a line of text. This is the difference between block and inline display, which every element has by default:

BlockInline
Starts on a new lineYesNo, flows inside text
WidthAs wide as its containerAs wide as its content
Examplesh1, p, ul, li, diva, strong, em, span, img

The div and span elements are special: they mean nothing at all. A div is a plain block box and a span a plain inline box, used when you need a wrapper to style and no meaningful element fits.

HTML & CSS
<style> div, p, h2 { outline: 2px dashed tomato; } span, strong, a { outline: 2px solid royalblue; }</style> <h2>Block boxes</h2><p>A paragraph with <strong>strong text</strong> and <a href="#">a link</a> inside it.</p><div>A div is a block too.</div><span>A span</span> <span>sits next to</span> <span>another span.</span>

Preview

The red dashed outlines are block boxes, each taking a full row. The blue outlines are inline boxes flowing like words. Try changing the first div to a span and watch it jump up next to the paragraph. CSS can change any element's display, which you will do a lot in the layout lessons, but the defaults tell you a lot about what an element is for.

Quick check
You write <p>Flour</p><p>Eggs</p> on one line with no space between them. How does the browser show them?
Need a nudge? A hint rules out a wrong answer.

Validating your HTML

Browsers are extremely forgiving. If you forget a closing tag or nest things wrongly, the browser guesses what you meant and carries on without any error. That sounds kind, but it means mistakes hide until something looks strange in one browser and not another.

A validator checks your HTML against the official rules. Paste your code into the W3C Nu HTML Checker at validator.w3.org/nu and it lists every problem with a line number. Run it on each page you build in this course. Errors such as "stray end tag" or "element not allowed as child" usually point straight at a nesting mistake.

Key terms
element
One piece of a page, usually an opening tag, content and a closing tag, such as a paragraph or link.
tag
The markup that starts or ends an element, like <p> and </p>.
attribute
Extra information in an opening tag, written as name="value", such as href or alt.
nesting
Putting elements inside other elements. Inner elements must close before outer ones.
void element
An element with no content and no closing tag, such as img, br, input and meta.
block vs inline
Block elements start on a new line and fill the width; inline elements flow inside a line of text.
validator
A tool that checks HTML against the official rules and reports mistakes the browser would silently repair.