Basic HTML Elements

HTML elements are the building blocks of every web page. They define how content is structured, grouped, and presented in a browser. Before you can style a page with CSS or add interactivity with JavaScript, you must understand how HTML elements work.

In this lesson, you’ll explore the most essential HTML elements used to structure web pages. You’ll learn how headings, paragraphs, lists, tables, and containers like div and span are used, and how block-level and inline elements behave.

By the end of this lesson, you’ll be comfortable using these elements together to create clear, well-structured HTML documents.

Desired Outcomes:

By the end of this lecture, you should be able to:

  • Understand the purpose of various HTML elements
  • Use headings, paragraphs, lists, tables, and divs to structure content
  • Apply basic styles to HTML elements

Let's begin by exploring the purpose and usage of some fundamental HTML elements.

Headings

HTML provides six levels of headings, ranging from <h1> (most important) to <h6> (least important). Headings help create a clear content hierarchy, making pages easier to read for users and easier to understand for search engines.

Example

<h1>This is a h1 heading</h1>
<h2>This is a h2 heading</h2>
<h3>This is a h3 heading</h3>
<h4>This is a h4 heading</h4>
<h5>This is a h5 heading</h5>
<h6>This is a h6 heading</h6>

Key points to understand

  • <h1> is usually the main title of the page
  • Headings should be used in order, not for styling
  • Headings define the structure of your content

Paragraphs

The <p> tag is used to define paragraphs of text. It separates blocks of text and helps maintain readability and organization.

<p>This is a paragraph of text.</p>

Browsers automatically add spacing before and after paragraphs, making text easier to read. Paragraphs should be used for normal text content, not headings or layout.

Headings and paragraphs together

Headings introduce topics, while paragraphs explain them. These two elements work together to form the core of readable HTML documents.

Headings and paragraphs
Headings and paragraphs

Lists

HTML offers two types of lists: ordered lists (<ol>) and unordered lists (<ul>). Ordered lists are used for items with a specific order or sequence, while unordered lists are used for items without a particular order. Each list item is defined using the <li> tag.

Lists are commonly used for:

  • Navigation menus
  • Feature lists
  • Steps or instructions

Example:

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

HTML Lists will be explained in detail in the following lessons.

Tables

Tables (<table>) are used to organize tabular data. They consist of rows (<tr>) and cells (<td>). Tables help present data in a structured format.

How tables work

  • <table> defines the table
  • <tr> defines a row
  • <td> defines a cell

Example:

<table>
   <tr>
      <td>Cell 1</td>
      <td>Cell 2</td>
   </tr>
   <tr>
      <td>Cell 3</td>
      <td>Cell 4</td>
   </tr>
</table>
Simple table
Simple table

HTML tables will be explained in detail in the following lessons.

Divs

The <div> element is a versatile block-level container that allows you to group and style sections of your webpage. It provides flexibility in organizing and styling content. It is primarily used for creating sections and grouping elements.

<div>
   <h2>Section Title</h2>
   <p>This is another paragraph inside a div.</p>
</div>

Why divs are useful

  • Group related content
  • Apply styles to sections
  • Create layouts (with CSS)

Divs are one of the most commonly used HTML elements in real-world projects.

Exercise:

Let's incorporate these elements into an example:

<!DOCTYPE html>
<html>
<head>
   <title>Basic HTML Elements</title>
   <style>
      h1 {
         color: blue;
      }
      p {
         font-style: italic;
      }
   </style>
</head>
<body>
   <h1>Welcome to Basic HTML Elements!</h1>
   <p>This is a paragraph of text.</p>
   <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
   </ul>
   <table>
      <tr>
         <td>Cell 1</td>
         <td>Cell 2</td>
      </tr>
      <tr>
         <td>Cell 3</td>
         <td>Cell 4</td>
      </tr>
   </table>
   <div>
      <h2>Section Title</h2>
      <p>This is another paragraph inside a div.</p>
   </div>
</body>
</html>

This code will render the following when opened in a browser.

Divs
Divs

In this example, we've used various HTML elements to structure and present content. We've applied basic styles using the <style> tag within the <head> section to color the <h1> element blue and make the <p> element italic.

Hands-On Exercise: Putting It All Together

Let’s combine multiple HTML elements into one complete example.

<!DOCTYPE html>
<html>
<head>
   <title>Basic HTML Elements</title>
   <style>
      h1 {
         color: blue;
      }
      p {
         font-style: italic;
      }
   </style>
</head>
<body>
   <h1>Welcome to Basic HTML Elements!</h1>
   <p>This is a paragraph of text.</p>
   <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
   </ul>
   <table>
      <tr>
         <td>Cell 1</td>
         <td>Cell 2</td>
      </tr>
      <tr>
         <td>Cell 3</td>
         <td>Cell 4</td>
      </tr>
   </table>
   <div>
      <h2>Section Title</h2>
      <p>This is another paragraph inside a div.</p>
   </div>
</body>
</html>

This example shows how different HTML elements work together to structure a page. Basic styling is applied using the <style> tag, which you’ll explore more deeply when learning CSS.

Span

The <span> element is a versatile inline container that allows you to group and style specific sections of text or inline elements. It is primarily used for targeting and styling specific sections of text or inline elements within your HTML content.

Important behavior

  • <span> does not create a new line
  • It does not break text flow
  • It is commonly used for inline styling or scripting
<p>
    HTML is <span style="color:red">awesome</span>!
    <span style="color:red">Span elements</span> 
    can be used for inline styling.
</p>
Spans
Spans

In this example, we used <span> elements to  color some of the paragraphs text into the color red. Notice that since the span element is an inline level element it does not disrupt the text flow.

Block and inline elements

Block-Level Elements

  • Block-level elements start on a new line and occupy the full width available by default.
  • Examples of block-level elements include <div>, <p>, <h1> to <h6>, <ul>, <li>, and <section>.
  • Block-level elements create a visual block or box-like structure.
  • Block-level elements can contain other block-level and inline-level elements.
  • They are commonly used for structuring and grouping content, creating sections, and adding vertical spacing.

Inline-Level Elements

  • Inline-level elements do not start on a new line and only occupy the necessary width to render their content.
  • Examples of inline-level elements include <span>, <a>, <strong>, <em>, <img>, and <input>.
  • Inline-level elements flow within the surrounding text and do not create line breaks.
  • Inline-level elements cannot contain block-level elements but can contain other inline-level elements.
  • They are commonly used for styling or adding emphasis within a line of text or to create inline links or images.

It's worth noting that the display behavior of elements can be modified using CSS properties such as display. For example, a block-level element can be changed to behave like an inline-level element by setting display: inline, and vice versa.

Understanding the differences between block-level and inline-level elements helps in structuring and styling web content effectively.

HTML Comments

Comments in HTML allow you to leave notes in your code that are ignored by the browser. They can be useful for leaving notes to yourself or other developers, explaining your code, or temporarily disabling a piece of code.

<!-- This comment explains what the following code does -->
<p>This paragraph contains some text.</p>

Congratulations on mastering the basics of HTML elements! Keep practicing and exploring the possibilities of these foundational building blocks.

By incorporating these HTML elements into your webpages, you can create well-organized and visually appealing content. In the next lecture, we'll delve into HTML attributes and learn how to enhance the functionality and styling of HTML elements.

Conclusion

Congratulations on mastering the basics of HTML elements. These elements form the foundation of every webpage, and you’ll use them constantly as you build more complex projects.

In the next lesson, you’ll learn about HTML attributes, which allow you to add extra information and behavior to these elements.

Part of our complete Learn HTML guide

Updated on:

-33%

Time remaining:
days 00: 00: 00

Learn to code HTML & CSS

From Zero to Hero

Check out this course on Udemy, now at 75% off! Inside this interactive course, I will teach you how to develop websites from scratch moving from beginner to advanced concepts. I take time to explain every detail and finish off by building some real-world websites.

HTML & CSS: From Zero to Hero
Learn to code HTML & CSS

-33%

Time remaining:
days 00: 00: 00

Learn to code JavaScript & jQuery

From Zero to Hero

Check out this course on Udemy, now at 75% off! Inside this interactive course, I will teach you how to develop components from scratch moving from beginner to advanced concepts. I take time to explain every detail and finish off by building some real-world reusable components.

JavaScript & jQuery: From Zero to Hero
Learn to code JavaScript & jQuery

-25%

Time remaining:
days 00: 00: 00

Learn Frontend Web Development

From Zero to Hero

Check out this course on Udemy, now at 75% off! A bundle of the previous two courses. Inside this interactive course, I will teach you how to develop websites from scratch moving from beginner to advanced concepts. I take time to explain every detail and finish off by building some real-world websites.

Frontend Web Development: From Zero to Hero
Learn Frontend Web Development