Home ยป HTML Elements Tutorial

HTML Elements Tutorial

by shedders

HTML elements are the building blocks of any webpage. They consist of opening and closing tags with content in between, defining the structure and content of a web page. In this tutorial, you’ll learn about basic, inline, block, semantic, and nested HTML elements with examples.

1. What is an HTML Element?

An HTML element consists of:

  1. An opening tag (e.g., <p>).
  2. Content (e.g., Hello World!).
  3. A closing tag (e.g., </p>).

Example: A Simple Element

<p>Hello World!</p>

2. Types of HTML Elements

a. Block Elements

Block elements take up the full width of the parent container and begin on a new line.

Example: Block Elements

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Block Elements</title>
</head>
<body>
    <h1>This is a Heading (Block Element)</h1>
    <p>This is a paragraph (Block Element).</p>
    <div>
        This is a division (Block Element) that can group other elements.
    </div>
</body>
</html>

Common block elements:

  • <div>: A generic container for grouping elements.
  • <p>: Defines a paragraph.
  • <h1> to <h6>: Define headings.

b. Inline Elements

Inline elements occupy only as much width as necessary and do not start on a new line.

Example: Inline Elements

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Inline Elements</title>
</head>
<body>
    <p>This is <strong>bold text</strong> and this is <em>italic text</em>.</p>
    <p>Visit <a href="https://www.example.com">this link</a> for more information.</p>
</body>
</html>

Common inline elements:

  • <a>: Hyperlinks.
  • <strong>: Bold text (semantic).
  • <em>: Italic text (semantic).
  • <span>: Generic inline container for styling.

3. Nesting HTML Elements

HTML allows you to nest elements inside one another, creating a structure.

Example: Nested Elements

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Nesting Elements</title>
</head>
<body>
    <h1>My Blog</h1>
    <p>This is an example of a paragraph containing <strong>bold</strong> and <em>italic</em> text.</p>
    <div>
        <h2>Subheading</h2>
        <p>A paragraph inside a <code>div</code> element.</p>
    </div>
</body>
</html>

Explanation:
Nesting allows you to combine different elements, like bold text inside a paragraph or headings inside a div.

4. Semantic HTML Elements

Semantic elements have meaningful names, improving accessibility and SEO.

Example: Semantic Elements

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Semantic Elements</title>
</head>
<body>
    <header>
        <h1>Welcome to My Website</h1>
    </header>
    <nav>
        <ul>
            <li><a href="#home">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#contact">Contact</a></li>
        </ul>
    </nav>
    <main>
        <article>
            <h2>Article Title</h2>
            <p>This is an article about HTML elements.</p>
        </article>
        <aside>
            <h3>Related Links</h3>
            <p>Check out related articles in this section.</p>
        </aside>
    </main>
    <footer>
        <p>&copy; 2024 My Website</p>
    </footer>
</body>
</html>

Explanation:

  • <header>: Represents the header section of a webpage.
  • <nav>: Contains navigation links.
  • <main>: Represents the main content.
  • <article>: Defines an independent piece of content.
  • <aside>: Defines side content or related information.
  • <footer>: Represents the footer section.

5. Void Elements

Void elements do not have closing tags and are self-closing. Examples include:

  • <img>: Embeds images.
  • <br>: Inserts a line break.
  • <hr>: Inserts a horizontal rule.
  • <meta>: Defines metadata.

Example: Void Elements

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Void Elements</title>
</head>
<body>
    <h1>Void Elements Example</h1>
    <p>This is an example of a paragraph with a <br> line break.</p>
    <img src="https://via.placeholder.com/150" alt="Placeholder Image">
    <hr>
    <p>Horizontal rule above this text.</p>
</body>
</html>

6. Interactive HTML Elements

Interactive elements allow user interaction, such as forms, buttons, and links.

Example: Form and Button

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Interactive Elements</title>
</head>
<body>
    <h1>Form Example</h1>
    <form>
        <label for="name">Name:</label>
        <input type="text" id="name" name="name">
        <br>
        <label for="email">Email:</label>
        <input type="email" id="email" name="email">
        <br>
        <button type="submit">Submit</button>
    </form>
</body>
</html>

7. Global Attributes

Some attributes can be applied to all HTML elements, including:

  • id: Unique identifier for an element.
  • class: Class name for styling or grouping.
  • style: Inline styling.
  • title: Tooltip text.

Example: Using Global Attributes

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Global Attributes</title>
</head>
<body>
    <h1 id="main-title" class="header">Welcome</h1>
    <p class="content" style="color: blue;" title="This is a tooltip.">
        This paragraph has a tooltip and is styled with inline CSS.
    </p>
</body>
</html>

Explanation:

  • id: Can target specific elements with JavaScript or CSS.
  • class: Groups multiple elements for styling.
  • style: Applies inline styles directly to an element.
  • title: Displays tooltip text on hover.

8. Combining Different HTML Elements

Combining multiple types of elements creates rich, interactive web pages.

Example: Combined Elements

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Combined Elements</title>
</head>
<body>
    <header>
        <h1>HTML Elements Tutorial</h1>
        <nav>
            <ul>
                <li><a href="#intro">Introduction</a></li>
                <li><a href="#examples">Examples</a></li>
                <li><a href="#contact">Contact</a></li>
            </ul>
        </nav>
    </header>
    <main>
        <article>
            <h2 id="intro">Introduction</h2>
            <p>This tutorial explains different types of HTML elements.</p>
            <img src="https://via.placeholder.com/150" alt="Example Image">
        </article>
        <aside>
            <h3>Tip</h3>
            <p>Use semantic elements for better accessibility.</p>
        </aside>
    </main>
    <footer>
        <p>Contact us at <a href="mailto:info@example.com">info@example.com</a></p>
    </footer>
</body>
</html>

Conclusion

In this tutorial, you learned about:

  1. Basic HTML elements: Block, inline, and void elements.
  2. Nesting and combining elements to create structured content.
  3. Semantic elements for accessibility and SEO.
  4. Interactive elements for user engagement.

Experiment with these examples to build dynamic and well-structured webpages.

You may also like