Skip to the content

Lesson 7 of 296

Page structure and tags

What you will learn

  • What a tag, an element and an attribute are
  • The basic structure every HTML page must have
  • What nesting means
  • Elements with no closing tag
  • Block and inline elements
  • How to write special characters
On this page

Tags and elements

HTML describes content using tags. A tag is a word inside angle brackets.

<p>This is a paragraph.</p>
  • <p> is the opening tag.
  • </p> is the closing tag. It has a /.
  • The text between them is the content.
  • The whole thing, opening tag + content + closing tag, is called an element.

The browser does not show the tags. It uses them to understand what each piece of content is.

Attributes

An attribute gives extra information about an element. It goes inside the opening tag, as name="value".

<a href="https://example.com">Visit Example</a>
<img src="cat.jpg" alt="A grey cat sleeping">
  • href is the attribute that says where a link goes.
  • src says which image file to show.
  • alt gives a text description of the image.

An element can have many attributes, separated by spaces. Two attributes work on almost every element:

AttributePurpose
idA unique name for one element. Only one element on the page may use each id.
classA group name. Many elements can share the same class. Used a lot by CSS and JavaScript.
<p id="welcome" class="intro large">Hello</p>

This element has one id and two classes (intro and large).

The basic structure of every page

Every HTML page should start like this:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Page</title>
  </head>
  <body>
    <h1>Welcome</h1>
    <p>This is my first page.</p>
  </body>
</html>

Line by line:

LineWhat it does
<!DOCTYPE html>Tells the browser this is a modern HTML page. Always the first line.
<html lang="en">The root element. Everything goes inside it. lang says the language of the page.
<head>Information about the page. Most of it is not shown on screen.
<meta charset="UTF-8">Lets the page show every language and symbol correctly.
<meta name="viewport" ...>Makes the page fit phone screens. Without it, phones show a tiny desktop page.
<title>The text shown in the browser tab and in Google results.
<body>Everything the user sees goes here.

Nesting

Elements can go inside other elements. This is called nesting. The inside element is the child. The outside element is the parent.

<p>This is <strong>very</strong> important.</p>

<strong> is a child of <p>.

Rule: close the elements in the reverse order you opened them. The last one opened is the first one closed.

Correct:  <p><strong>Hi</strong></p>
Wrong:    <p><strong>Hi</p></strong>

Indent child elements with spaces so the structure is easy to read. The browser ignores the spaces, but people reading your code do not.

Elements with no closing tag

Some elements have no content, so they have no closing tag. They are called void elements.

ElementPurpose
<img>An image
<br>A line break
<hr>A horizontal line (a change of topic)
<input>A form field
<meta>Information about the page
<link>Connects another file, such as CSS

You may see them written as <br /> with a slash. In HTML both forms are accepted. In React (JSX) the slash is required.

Block and inline elements

Elements behave in one of two main ways by default.

  • Block elements start on a new line and take the full width. Examples: <p>, <h1>, <div>, <ul>, <form>.
  • Inline elements stay inside the line of text and take only the width they need. Examples: <a>, <strong>, <em>, <span>, <img>.
<p>Call us on <strong>0113 496 0000</strong> today.</p>

The <strong> stays in the same line because it is inline.

div and span

Two elements have no meaning at all. They are plain containers, used for styling and layout.

  • <div>: a block container.
  • <span>: an inline container.
<div class="card">
  <p>Price: <span class="price">£12</span></p>
</div>

Use them only when no element with a real meaning fits. Lesson 12 explains why.

Comments

<!-- This is a comment. The browser ignores it. -->

Special characters

Some characters have a special job in HTML, such as < and >. To show them as normal text, use an entity code.

You wantWrite
<&lt;
>&gt;
&&amp;
"&quot;
©&copy;
A space that will not break the line&nbsp;

Common mistakes

  • Forgetting a closing tag. The browser tries to guess, and the page looks wrong in strange places.
  • Using the same id twice. It breaks JavaScript and accessibility. Use a class for groups.
  • Missing the viewport meta tag. The site looks tiny on phones.
  • Putting visible content inside <head>. It belongs in <body>.

How to say it in an interview

"An HTML element is an opening tag, content and a closing tag, and attributes add extra information. Every page starts with a doctype, then the html root with a head for metadata and a body for visible content. I always include the charset and viewport meta tags and a title. id must be unique, class can be shared. Block elements start on a new line and inline elements sit inside a line of text."

Key words

WordMeaning
TagA keyword in angle brackets, like <p>
ElementOpening tag + content + closing tag
AttributeExtra information inside an opening tag
NestingPutting elements inside other elements
Void elementAn element with no closing tag, like <img>
Block elementStarts on a new line and takes the full width
Inline elementSits inside a line of text
EntityA code for a special character, like &lt;

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.