Reading history

HTML Elements and Attributes Explained (With Examples)

5 min read 1 views

Learn HTML elements and attributes: tags and nesting, text elements, id, class, title and lang, block vs inline elements, comments and entities.

Every web page is built from elements: a start tag, some content and an end tag, such as <p>Hello</p>. Attributes go inside the start tag and add extra information, such as an id, a link address or a language. This lesson covers the everyday text elements, the difference between block and inline elements, the most useful attributes, comments and special characters.

In this lesson you will learn

  • The parts of an HTML element
  • Text elements such as <strong>, <em>, <sub> and <br>
  • Global attributes: id, class, title, lang, style
  • Block vs inline elements
  • Comments and HTML entities like &lt;

Anatomy of an Element

In <a href="/learn/">Courses</a>, <a> is the start tag, href="/learn/" is an attribute (a name and a value in quotes), “Courses” is the content, and </a> is the end tag. Elements can be nested inside each other, but they must close in the reverse order they opened.

Text Elements and Attributes

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Elements and attributes</title>
</head>
<body>
  <h2 id="intro" title="Hover over me">Elements and Attributes</h2>
  <p>This paragraph has <strong>strong</strong>, <em>emphasised</em> and
     <mark>highlighted</mark> words.</p>
  <p lang="bn">ঢাকা</p>
  <p>Water is H<sub>2</sub>O and 10<sup>3</sup> is 1000.</p>
  <p>Line one<br>Line two after a line break.</p>
  <hr>
  <p style="color: teal;">An inline style attribute makes this text teal.</p>
</body>
</html>

Result

  • <strong> marks important text (bold) and <em> marks emphasis (italic). Screen readers can stress them, which plain bold styling does not do.
  • <mark> highlights, <sub> and <sup> write subscripts and superscripts.
  • <br> and <hr> are void elements: they have no content and no end tag.
  • id names one unique element; title shows a tooltip on hover; lang tells browsers and screen readers the language of the text.
  • The style attribute applies CSS to one element. It is handy for quick tests, but a stylesheet is better for real sites.

Block vs Inline, Comments and Entities

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Block and inline</title>
  <style>
    div  { background: #e0f2f1; margin: 4px 0; padding: 4px; }
    span { background: #fff3cd; }
  </style>
</head>
<body>
  <div>A div is a block element: it starts on a new line.</div>
  <div>Another div, stacked underneath.</div>
  <p>A span is <span>inline</span>, so it sits <span>inside</span> the line.</p>
  <!-- This comment is not shown in the browser -->
  <p>5 &lt; 7 &amp; 7 &gt; 5 &copy; 2026</p>
</body>
</html>

Result

  • Block elements (div, p, h1–h6, ul) start on a new line and take the full width.
  • Inline elements (span, a, strong, img) sit inside a line of text.
  • <!-- ... --> is a comment; browsers ignore it.
  • Characters with special meaning are written as entities: &lt; for <, &gt; for >, &amp; for &, &copy; for ©.

Common Global Attributes

AttributePurpose
idA unique name, used for links (#id), CSS and JavaScript
classOne or more reusable names for styling groups of elements
titleExtra information shown as a tooltip
langLanguage of the content, such as en or bn
styleInline CSS for one element
hiddenHides the element
data-*Custom data for scripts, such as data-price="250"

Common Mistakes

  1. Overlapping tags like <strong><em>text</strong></em>. Close the inner one first.
  2. Reusing an id on several elements. Use a class instead.
  3. Using <br> for spacing. Use CSS margins; keep <br> for real line breaks such as addresses.
  4. Typing < directly in text, which the browser may read as a tag.

Practice Exercises

Exercise 1. Write the chemical formula for carbon dioxide (CO₂) in HTML.

Show answer
<p>CO<sub>2</sub></p>

Exercise 2. Display the text <p> on a page without the browser treating it as a tag.

Show answer
<p>Use the &lt;p&gt; tag for paragraphs.</p>

Frequently Asked Questions

Are HTML tags case-sensitive?

No, <P> and <p> work the same, but lowercase is the standard style.

What is the difference between <b> and <strong>?

Both look bold. <strong> also says the text is important, which matters for accessibility; <b> is purely visual.

Summary

  • An element is a start tag, content and an end tag; attributes live in the start tag.
  • Block elements start new lines; inline elements flow within text.
  • id is unique, class is reusable.
  • Write special characters as entities.

Next: HTML Links and Images.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *