Lesson 3 of 36
HTML Tutorial

Elements, Attributes and Comments

How tags form elements, how attributes configure them, and how to leave notes in your code.

3 min readBeginnerWeb FundamentalsIntroduction

You now know the skeleton of a page. Next we look at the three ideas that appear in every line of HTML: elements, attributes and comments.

Elements#

An element is a complete unit of content. Most elements have an opening tag, content and a closing tag:

html
<!-- opening tag, content, closing tag -->
<p>Our espresso is roasted in the hills.</p>

Together, <p>, the sentence and </p> form one paragraph element. The closing tag is the same name with a slash.

Some elements have no content and therefore no closing tag. They are called void elements. You will meet several, including <br>, <hr>, <img>, <input> and <meta>:

html
<!-- void elements stand alone -->
<img src="latte.jpg" alt="A latte with leaf-shaped milk foam">
<br>

Elements can contain other elements. This is called nesting, and it must be done in a strictly correct order:

html
<!-- correct: the inner element closes before the outer one -->
<p>Try our <strong>cardamom</strong> latte.</p>

Writing <p><strong>text</p></strong> is invalid, because the tags overlap.

Attributes#

An attribute gives an element extra information. It lives inside the opening tag as a name and a value:

html
<a href="menu.html" title="See the full menu">Menu</a>

Here href and title are attributes of the <a> element. Follow these habits:

  • Write the value in double quotes: name="value".
  • Separate multiple attributes with spaces.
  • Use lowercase names. HTML is not case sensitive, but lowercase is the universal convention.

Some attributes work everywhere (id, class, lang, title) and are called global attributes. Others belong to particular elements, such as src for images or href for links. A few are boolean attributes: their mere presence means "on".

html
<!-- "disabled" has no value; its presence is enough -->
<button disabled>Order closed</button>

Comments#

A comment is a note for humans. The browser ignores it completely:

html
<!-- TODO: replace this placeholder with the real opening hours -->
<p>Open daily.</p>

<!--
  Comments can span
  several lines too.
-->

Use comments to explain why something exists, to mark sections of a long page, or to temporarily disable a piece of markup while testing. Remember that comments are still visible to anyone who views the page source, so never put passwords or private notes in them.

Common mistakes#

  • Forgetting a closing tag, which makes the rest of the page nest inside the unclosed element.
  • Using single or unmatched quotes around values, such as href="menu.html'.
  • Adding a slash to a void element and expecting it to do something. <br/> works, but the slash is optional in HTML.
  • Placing a secret inside a comment because "nobody will see it".

Try it yourself#

Write three elements for a profile page: a heading with your name, a paragraph about yourself with one word wrapped in <strong>, and a link to any website with both href and title. Add a comment above each one saying what it is. Then wrap one element in a comment to "switch it off" and refresh the page to confirm it disappears.

Recap#

Elements are made of tags and content, and some are void with no closing tag. Attributes add configuration in name="value" form inside the opening tag. Comments leave notes for humans and are never displayed.

Check your understanding

Lesson quiz

Optional

A short quiz to check what you just learned.

Questions
4
Time limit
5 min
Pass mark
70%
Best score
-