Course Content
Introduction to HTML
In this introductory module, you'll learn what HTML is and its role in web development. You will set up your development environment and create your first HTML page. You'll also understand the basic structure of an HTML document and get familiar with fundamental HTML elements and attributes.
0/4
Working with Text in HTML
This module covers how to format text in HTML, including creating headings, paragraphs, and lists. You'll learn how to emphasize text with bold, italics, and underline, and how to create links for navigation. You'll also explore text alignment and introduce inline CSS for basic styling.
0/4
Working with Images and Multimedia
In this module, you’ll learn how to add images and multimedia to your HTML pages. You'll master the <img> tag, and understand how to work with attributes like src and alt. Additionally, you'll discover how to embed audio and video files directly into your webpage, enhancing its interactivity.
0/3
Tables and Forms in HTML
This module introduces the use of tables for displaying structured data and forms for collecting user input. You'll learn how to create, format, and style tables, and how to build forms with input fields, checkboxes, and buttons. Basic HTML5 form validation will also be covered.
0/2
HTML5 Elements and Semantic Markup
Explore the power of HTML5 in this module, where you’ll learn about new HTML5 elements like <article>, <section>, and <nav>. You’ll gain an understanding of semantic HTML, which helps improve search engine optimization (SEO) and accessibility. You’ll also be introduced to HTML5-specific features such as video and audio embedding.
0/3
Advanced HTML Concepts
This module dives deeper into advanced HTML topics, including embedding external content using [iframe], working with HTML5 APIs like geolocation, and implementing accessibility best practices with ARIA attributes. You’ll also get a primer on responsive web design with the use of meta tags and media queries.
0/4
HTML Best Practices
Learn the best practices for writing clean, maintainable, and accessible HTML. This module covers proper code formatting, the importance of semantic HTML, and how to structure your HTML for SEO. You’ll also get practical tips on debugging HTML and using online validators to ensure your code is error-free.
0/3
Project – Building a Simple Website
Apply everything you’ve learned by creating a complete website from scratch in this hands-on project. You’ll plan, design, and build a multi-page website, incorporating text, images, forms, and navigation. This project will help you solidify your skills and showcase your work.
0/4
Conclusion and Next Steps
In the final module, you'll review the key concepts and skills you’ve learned throughout the course. You’ll also get guidance on the next steps in your web development journey, including an introduction to CSS for styling and JavaScript for interactivity. You’ll complete a final assessment to demonstrate your new HTML skills.
0/2
Complete HTML Course (Free)
About Lesson

 

Creating Tables

This lesson will teach you how to create and style tables using HTML. You will learn the basic structure of tables, how to include headers and data, and ways to enhance the table’s appearance with CSS.


1. Why Use Tables?

Tables are used to organize and display data in rows and columns, making it easy for users to interpret information. They are ideal for presenting tabular data such as schedules, comparison charts, and financial reports.


2. Basic Structure of a Table

HTML tables are created using the <table> element, with rows defined by <tr> and cells by <td>. Headers are created using the <th> element.

Example of a Basic Table:

<!DOCTYPE html>
<html>
<head>
    <title>Basic Table</title>
</head>
<body>
    <h1>Student Grades</h1>
    <table border="1">
        <tr>
            <th>Name</th>
            <th>Subject</th>
            <th>Grade</th>
        </tr>
        <tr>
            <td>John Doe</td>
            <td>Mathematics</td>
            <td>A</td>
        </tr>
        <tr>
            <td>Jane Smith</td>
            <td>Science</td>
            <td>B</td>
        </tr>
    </table>
</body>
</html>

3. Key Table Elements

  1. <table>:

    • Defines the table container.
  2. <tr> (Table Row):

    • Defines a row within the table.
  3. <th> (Table Header Cell):

    • Represents header cells, typically bold and centered by default.
  4. <td> (Table Data Cell):

    • Represents standard data cells.
  5. border Attribute:

    • Adds a border around the table for visibility (use CSS for modern styling).

4. Adding a Caption to a Table

The <caption> element provides a title or description for the table.

Example:

<table border="1">
    <caption>Monthly Sales Report</caption>
    <tr>
        <th>Product</th>
        <th>Sales</th>
        <th>Revenue</th>
    </tr>
    <tr>
        <td>Widget A</td>
        <td>500</td>
        <td>$10,000</td>
    </tr>
</table>

5. Merging Cells

You can merge cells using the colspan and rowspan attributes.

  1. colspan: Merges cells horizontally.

    <td colspan="2">Merged Cell</td>
    
  2. rowspan: Merges cells vertically.

    <td rowspan="2">Merged Cell</td>
    

Example:

<table border="1">
    <tr>
        <th>Name</th>
        <th colspan="2">Performance</th>
    </tr>
    <tr>
        <td>John</td>
        <td>Excellent</td>
        <td>A</td>
    </tr>
</table>

6. Styling Tables with CSS

CSS can significantly enhance the appearance of a table.

Example:

<!DOCTYPE html>
<html>
<head>
    <title>Styled Table</title>
    <style>
        table {
            width: 80%;
            margin: 20px auto;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: center;
        }
        th {
            background-color: #f4f4f4;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        tr:hover {
            background-color: #f1f1f1;
        }
    </style>
</head>
<body>
    <h1>Styled Table Example</h1>
    <table>
        <tr>
            <th>Item</th>
            <th>Price</th>
            <th>Quantity</th>
        </tr>
        <tr>
            <td>Apples</td>
            <td>$1.00</td>
            <td>5</td>
        </tr>
        <tr>
            <td>Bananas</td>
            <td>$0.50</td>
            <td>10</td>
        </tr>
    </table>
</body>
</html>

7. Responsive Tables

To make tables responsive, use CSS to enable horizontal scrolling on smaller screens.

Example:

<style>
    .table-container {
        overflow-x: auto;
    }
    table {
        width: 100%;
        border-collapse: collapse;
    }
    th, td {
        border: 1px solid #ddd;
        padding: 8px;
    }
</style>

<div class="table-container">
    <table>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
        </tr>
        <tr>
            <td>Data 1</td>
            <td>Data 2</td>
            <td>Data 3</td>
        </tr>
    </table>
</div>

8. Advanced Features for Tables

  1. Sorting Tables:

    • Use JavaScript or libraries like DataTables to add sorting functionality.
  2. Highlighting Rows:

    • Add hover effects or color-code rows for better readability.
  3. Accessibility:

    • Use the scope attribute in <th> for better screen reader compatibility.
      <th scope="col">Name</th>
      

9. Hands-On Activity: Create Your Own Table

  1. Create a new HTML file named table_example.html.
  2. Add a table displaying a list of items, their prices, and quantities.
  3. Apply CSS to style the table with borders, hover effects, and responsive behavior.

Example Output: A styled table displaying data like products, prices, and quantities.


10. Best Practices for Creating Tables

  1. Keep Tables Simple:

    • Avoid cluttering with excessive data or styling.
  2. Add Captions:

    • Use <caption> for clarity and accessibility.
  3. Enhance Readability:

    • Use alternating row colors and padding.
  4. Test Responsiveness:

    • Ensure the table adapts to different screen sizes.

11. Summary

  • Tables organize data into rows and columns using <table>, <tr>, <th>, and <td>.
  • Use CSS to style and enhance table presentation.
  • Make tables responsive for better usability on mobile devices.
  • Use attributes like colspan and rowspan to merge cells.