Skip to content

Start: the web and your first page

Elements, tags and attributes

Tags versus elements, nesting them correctly, elements with no closing tag, and attributes such as id and class.

  • 14 min
  • 1 live exercise
  • Quiz
  • Free
In this lesson you will
  • Break an element into opening tag, content and closing tag
  • Nest elements correctly
  • Write attributes and tell id from class

Everything in HTML is an element. Most elements have three parts: an opening tag, content and a closing tag. The tag’s name says what the content is — a paragraph, a heading, a link.

HTML
<p>This is a paragraph.</p>
  • <p> is the opening tag; </p> the closing tag (the same name with a slash).
  • The text between is the element’s content.
  • Write tag names in lower case; browsers accept both, but this is the convention.

Nesting

Elements sit inside each other; the one opened last is closed first — like a box inside a box.

HTML
<!-- right -->
<p>This is <strong>important</strong>.</p>

<!-- wrong: closed in the wrong order -->
<p>This is <strong>important</p></strong>
Right and wrong

Elements with no closing tag

A few elements have no content and no closing tag: <br> (line break), <hr> (a dividing rule), <img> (image), <meta> and <input>. These are called void elements.

Attributes

Attributes carry extra information about an element and always go in the opening tag: name="value". A few you will use constantly:

  • id — a unique name for one element on the page (once only).
  • class — a group name; many elements can share a class, and CSS uses it to style them.
  • href is a link’s destination, src an image’s address, alt an image’s description, lang a language.
HTML
<p id="about" class="intro">Hi, I am a <strong>programmer</strong>.</p>
<hr>
Exercise

Give the paragraph id="about", put one important word inside <strong>, and add a dividing <hr> below it.

Aim for:
  • A paragraph with id="about"
  • A <strong> inside the paragraph
  • A dividing <hr>
Live preview

Lesson quiz

3 questions — marked by Mashaal’s server, every answer explained.

  1. 1Which element has no closing tag?

  2. 2Where are attributes written?

  3. 3Which nesting is correct?