Loading...

HTML essentialsFoundations

Elements & structure

5 min lesson

Next
  • HTML describes the structure and meaning of the content in a web page, such as headings, paragraphs and lists.
  • Almost every page you visit is built from HTML, so it is the first skill every web developer learns.
  • Think of labeling boxes: you put a label on the box (opening tag), put stuff inside (content), then close the box (closing tag).
  • A tag is the word inside angle brackets, like <p>. An element is the whole thing: opening tag, content and closing tag.
  • Elements can be nested inside other elements to show which content belongs together.
  • The example page has one heading and one short paragraph inside a main area; you will see both on screen.
Keep in mind
  • Use nesting to show which content belongs together.
  • Close your tags in the reverse order you opened them, like nested boxes.
  • Keep indentation consistent so the page is easy to scan.
  • Mistakes are normal. Fix one at a time and you will improve quickly.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Elements & structure</title>
</head>
<body>
  <!-- The main area of the page -->
  <main>
    <!-- The biggest heading on the page -->
    <h1>My first page</h1>
    <!-- A paragraph of normal text -->
    <p>A little bit of page content.</p>
  </main>
</body>
</html>

What happens: A page with the heading "My first page" and a short paragraph "A little bit of page content."

Breaking it down

Opening tag

<p>

Tells the browser a paragraph starts here. Everything after it becomes part of the paragraph. Without it, the browser would not know the text is a paragraph.

Content

A little bit of page content.

The text or elements between the tags. This is what the user actually reads or sees.

Closing tag

</p>

Tells the browser the paragraph ends here. Without it, the browser may treat the rest of the page as part of the paragraph.

Common HTML elements

ElementWhat it makes
<h1>The biggest heading
<p>A paragraph of text
<ul>A bulleted list
<main>The main content area

A helpful analogy

An HTML element is like a sandwich. The opening tag is the top slice of bread, the content is the filling, and the closing tag is the bottom slice. You can put small sandwiches inside bigger ones, and that is called nesting.

Nesting rules

  • 1.Always close the most recently opened tag first. This is called 'last in, first out.'
  • 2.Keep child elements indented inside their parent so the structure is easy to read.

Common mistakes

Forgetting the closing tag

Avoid

<p>Hello world

Use instead

<p>Hello world</p>

The browser may swallow the rest of your page into that paragraph. Always check that every opening tag has a closing tag.

Crossing tags instead of nesting them

Avoid

<p><strong>Hi</p></strong>

Use instead

<p><strong>Hi</strong></p>

The inner tag must close before the outer tag. Crossing breaks the structure and confuses the browser.

Quick tips

  • •Indent child elements by two spaces so the structure is visually obvious.
  • •Look at the Elements panel in DevTools to see how the browser actually nested your tags.
Previous
Next