Skip to main content

Tags

What is a Tag? πŸ·οΈβ€‹

Tags are the building blocks of HTMLβ€”they tell the browser what type of content you're creating. Think of tags as labels that mark different parts of your web page.

Real-World Analogy: Labeling Boxes πŸ“¦β€‹

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Label: "Books" β”‚ ← This is like an HTML tag
β”‚ ───────────────── β”‚
β”‚ πŸ“š Book 1 β”‚ ← This is the content
β”‚ πŸ“š Book 2 β”‚
β”‚ πŸ“š Book 3 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

In HTML:

<h1>This is a Heading</h1>
↑ ↑ ↑
Opening Content Closing
tag tag

Tag Structure​

Most tags come in pairs:

<tagname>Content goes here</tagname>

Breaking it down:

  1. Opening tag: <tagname> - Starts the element
  2. Content: The text or media inside
  3. Closing tag: </tagname> - Ends the element (note the /)

Common Tag Examples​

<!-- Heading -->
<h1>Main Title</h1>

<!-- Paragraph -->
<p>This is a paragraph of text.</p>

<!-- Link -->
<a href="https://google.com">Click here</a>

<!-- Image (self-closing!) -->
<img src="photo.jpg" alt="Description">

<!-- Bold text -->
<strong>Important text</strong>

<!-- Italic text -->
<em>Emphasized text</em>

Visual Output:

Main Title

This is a paragraph of text.

Click here

A small sample image

Important text

Emphasized text

What is an Attribute?​

Tags can also include attributes, which provide additional information about the element. Attributes are added to the opening tag and are usually in the form of a name/value pair. For example, the img tag is used to add an image to a web page, and it includes an src attribute that specifies the URL of the image file. Here's an example:

<img src="image.webp" alt="A description of the image" />

In this example, the src attribute specifies the URL of the image file, while the alt attribute provides a description of the image, which is displayed if the image can't be loaded for some reason. Tags and attributes are the building blocks of HTML, and they allow web developers to create complex and dynamic web pages.

This could show up as:

A description of the image

See image is not showing up, because it is not available, so it is showing the alt text. Now, I put a src that points to a real image, so it will show up:

<img src="/img/logo.svg" alt="A description of the image" width="120" />
A description of the image

Self-Closing Tags (Void Elements) πŸ”“β€‹

Some tags don't need a closing tag because they don't contain contentβ€”they just perform an action or insert something.

Why Self-Closing?​

Regular tags contain content:

<p>Content goes here</p>

Self-closing tags don't have content to contain:

<img src="photo.jpg"> <!-- Just inserts an image -->
<br> <!-- Just creates a line break -->
<hr> <!-- Just draws a horizontal line -->

Common Self-Closing Tags​

<!-- Line break (new line) -->
<br>

<!-- Horizontal rule (divider line) -->
<hr>

<!-- Image -->
<img src="photo.jpg" alt="Description">

<!-- Input field -->
<input type="text" placeholder="Enter name">

<!-- Link to stylesheet -->
<link rel="stylesheet" href="styles.css">

<!-- Metadata -->
<meta charset="UTF-8">

Example Usage​

<p>First line<br>Second line</p>

<!-- Output: -->
<!-- First line -->
<!-- Second line -->
<p>Section 1</p>
<hr>
<p>Section 2</p>

<!-- Output: -->
<!-- Section 1 -->
<!-- ____________ (horizontal line) -->
<!-- Section 2 -->
Self-Closing vs Void Elements

These terms mean the same thing:

  • Self-closing tag βœ…
  • Void element βœ…
  • Empty element βœ…

All refer to tags that don't have closing tags!

Optional Slash

In HTML5, the trailing slash / is optional:

<!-- Both are valid -->
<img src="photo.jpg"> βœ…
<img src="photo.jpg" /> βœ…

<br> βœ…
<br /> βœ…

Recommendation: Use no slash for cleaner code, but be consistent!

Essential HTML Tags by Category πŸ“šβ€‹

HTML has 100+ tags, but you'll use about 20-30 regularly. Let's organize them by purpose!

Quick Reference: Most Used Tags​

TagPurposeExample
<h1> to <h6>Headings<h1>Main Title</h1>
<p>Paragraph<p>Text here</p>
<a>Link<a href="url">Link</a>
<img>Image<img src="pic.jpg">
<div>Container<div>Content</div>
<span>Inline container<span>Text</span>
<ul>, <ol>, <li>Lists<ul><li>Item</li></ul>
<button>Button<button>Click</button>
<input>Form input<input type="text">

Now let's explore tags by category with examples!

Document Structure​

<!DOCTYPE>:​

Declares the document type and version. It should be the first line of an HTML document.

<!DOCTYPE html>

<html>:​

Defines the root of an HTML document. All other elements must be a child of this element.

<html>
...
</html>

Contains meta information about the document. This element is a child of the <html> element.

<head>
<title>Page Title</title>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>

<title>:​

Defines the title of the document, which is shown in a browser's title bar or on the page's tab. This element is a child of the <head> element.

<title>Page Title</title>

<body>:​

Defines the document's body, and is a container for all the visible contents, such as headings, paragraphs, images, hyperlinks, tables, lists, etc. This element is a child of the <html> element.

Live Editor
<body>
  <h1>My First Heading</h1>
  <p>My first paragraph.</p>
</body>
Result
Loading...

Text Formatting​

<h1> to <h6>:​

Defines HTML headings, <h1> defines the most important heading and <h6> defines the least important heading. Each heading level has its own default size, with <h1> rendered the largest. This element is a child of the <body> element.

Live Editor
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>
Result
Loading...

<p>:​

Defines a paragraph of text.

Live Editor
<p>This is a paragraph.</p>
Result
Loading...

<br>:​

Inserts a single line break.

Live Editor
<p>This is<br />a paragraph.</p>
Result
Loading...

<hr>:​

Inserts a horizontal rule (a thematic break in an essay or article).

Live Editor
<p>This is a paragraph.</p>
<hr />
<p>This is another paragraph.</p>
Result
Loading...

<pre>:​

Defines preformatted text. Text in a <pre> element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks.

<pre>
This is a paragraph.
This is another paragraph.

This is a third paragraph.
</pre>

<b>:​

Defines bold text.

Live Editor
<p>This is <b>bold</b> text.</p>
Result
Loading...

<strong>:​

Defines important text.

Live Editor
<p>This is <strong>important</strong> text.</p>
Result
Loading...

<u>:​

Defines underlined text.

Live Editor
<p>This is <u>underlined</u> text.</p>
Result
Loading...

<i>:​

Defines italic text.

Live Editor
<p>This is <i>italic</i> text.</p>
Result
Loading...

<em>:​

Defines emphasized text.

Live Editor
<p>This is <em>emphasized</em> text.</p>
Result
Loading...

Lists​

<ul>:​

Defines an unordered list.

Live Editor
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>
Result
Loading...

<ol>:​

Defines an ordered list.

Live Editor
<ol>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ol>
Result
Loading...

<li>:​

Defines a list item.

<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>

<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>

Tables​

<table>:​

Defines a table.

Live Editor
<table>
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
    <th>Header 3</th>
  </tr>
  <tr>
    <td>Row 1, Column 1</td>
    <td>Row 1, Column 2</td>
    <td>Row 1, Column 3</td>
  </tr>
  <tr>
    <td>Row 2, Column 1</td>
    <td>Row 2, Column 2</td>
    <td>Row 2, Column 3</td>
  </tr>
</table>
Result
Loading...

<tr>:​

Defines a table row.

Live Editor
<table>
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
    <th>Header 3</th>
  </tr>
</table>
Result
Loading...

<th>:​

Defines a table header.

Live Editor
<table>
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
    <th>Header 3</th>
  </tr>
</table>
Result
Loading...

<td>:​

Defines a table cell.

Live Editor
<table>
  <tr>
    <td>Row 1, Column 1</td>
    <td>Row 1, Column 2</td>
    <td>Row 1, Column 3</td>
  </tr>
</table>
Result
Loading...

Forms​

<form>:​

Defines an HTML form for user input.

Live Editor
<form>
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" />
  <input type="submit" value="Submit" />
</form>
Result
Loading...

<input>:​

Defines an input control.

Live Editor
<form>
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" />
  <input type="submit" value="Submit" />
</form>
Result
Loading...

<textarea>:​

Defines a multiline input control (text area).

Live Editor
<form>
  <label for="message">Message:</label>
  <textarea id="message" name="message" rows="5" cols="33">
    The cat was playing in the garden.
  </textarea>
</form>
Result
Loading...

<select>:​

Defines a drop-down list.

Live Editor
<form>
  <label for="cars">Choose a car:</label>
  <select id="cars" name="cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </select>
</form>
Result
Loading...

<option>:​

Defines an option in a drop-down list.

Live Editor
<form>
  <label for="cars">Choose a car:</label>
  <select id="cars" name="cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </select>
</form>
Result
Loading...

<button>:​

Defines a clickable button.

Live Editor
<button type="button">Click Me!</button>
Result
Loading...

Media​

<img>:​

Defines an image.

Live Editor
<img
  src="https://docs.rizwanashiq.com/img/rizwan-ashiq.webp"
  alt="Rizwan Ashiq"
  width="104"
  height="104"
/>
Result
Loading...

<audio>:​

Defines sound content.

Live Editor
<audio controls>
  <source src="horse.ogg" type="audio/ogg" />
  <source src="horse.mp3" type="audio/mpeg" />
  Your browser does not support the audio element.
</audio>
Result
Loading...

<video>:​

Defines a video or movie.

Live Editor
<video width="320" height="240" controls>
  <source src="movie.mp4" type="video/mp4" />
  <source src="movie.ogg" type="video/ogg" />
  Your browser does not support the video tag.
</video>
Result
Loading...

Embedded Content​

<iframe>:​

Defines an inline frame.

<iframe src="https://rizwanashiq.com"></iframe>

Containers​

<div>:​

Defines a section in a document. The <div> element is often used as a container for other HTML elements to style them with CSS or to perform certain tasks with JavaScript. The <div> element has no special meaning. It is used to group block-elements to format them with CSS.

<div
style="border:1px solid black; padding:10px; color:black; background-color:white;"
>
<h2>London</h2>
<p>London is the capital city of England.</p>
</div>

This will produce the following output:

London

London is the capital city of England.

<span>:​

Defines a section in a document. The <span> element is used to group inline-elements in a document. The <span> element does not introduce a new line or a new paragraph. The <span> element is useful for styling a part of a text, or for adding a hook to a part of a text.

<span>
<h2>London</h2>
<p>London is the capital city of England.</p>
</span>

Conclusion​

In this tutorial, we have learned about the HTML elements and their attributes. We also learned about the different types of HTML elements.