Header Ads Widget

WebLearner Pro Banner

Beginner to Advance HTML 8


 

Chapter 8: Creating Tables in HTML


HTML tables are useful for displaying data in a structured, grid-like format. They allow you to organize information into rows and columns, making it easy to present everything from simple lists to complex data. In this chapter, we’ll cover how to create tables using HTML and format them for readability.


1. Basic Table Structure


A basic HTML table is created with the <table> element, containing rows (<tr>) and cells (<td> for regular data and <th> for headers).


Basic Syntax


<table>

    <tr>

        <th>Header 1</th>

        <th>Header 2</th>

    </tr>

    <tr>

        <td>Data 1</td>

        <td>Data 2</td>

    </tr>

</table>


In this example:


<table> defines the table.


<tr> defines a table row.


<th> defines a header cell (bold and centered by default).


<td> defines a regular data cell.



2. Adding Headers


The <th> element is used to create table headers, which are typically bold and centered. Headers help identify the type of data in each column or row.


Example:


<table>

    <tr>

        <th>Name</th>

        <th>Age</th>

        <th>City</th>

    </tr>

    <tr>

        <td>John</td>

        <td>28</td>

        <td>New York</td>

    </tr>

    <tr>

        <td>Jane</td>

        <td>32</td>

        <td>Los Angeles</td>

    </tr>

</table>


3. Adding Captions


The <caption> element provides a title or description for the table, which is usually displayed above the table.


Example:


<table>

    <caption>Employee Information</caption>

    <tr>

        <th>Name</th>

        <th>Position</th>

        <th>Department</th>

    </tr>

    <tr>

        <td>Emily</td>

        <td>Manager</td>

        <td>Sales</td>

    </tr>

    <tr>

        <td>Mark</td>

        <td>Developer</td>

        <td>IT</td>

    </tr>

</table>


4. Spanning Columns and Rows


The colspan and rowspan attributes allow a cell to span multiple columns or rows.


colspan: Merges cells horizontally across columns.


rowspan: Merges cells vertically across rows.



Example:


<table>

    <tr>

        <th>Name</th>

        <th colspan="2">Contact Information</th>

    </tr>

    <tr>

        <td>John</td>

        <td>john@example.com</td>

        <td>123-456-7890</td>

    </tr>

    <tr>

        <td>Jane</td>

        <td colspan="2">jane@example.com</td>

    </tr>

</table>


In this example, the "Contact Information" header spans two columns, and Jane's contact information spans two cells.


5. Adding Table Borders and Styling


By default, HTML tables have no borders. You can add borders and apply styling using CSS.


Example of Basic Table Styling


<style>

    table {

        width: 100%;

        border-collapse: collapse;

    }

    th, td {

        border: 1px solid black;

        padding: 8px;

        text-align: left;

    }

    th {

        background-color: #f2f2f2;

    }

</style>


<table>

    <tr>

        <th>Name</th>

        <th>Age</th>

        <th>City</th>

    </tr>

    <tr>

        <td>Emily</td>

        <td>25</td>

        <td>New York</td>

    </tr>

    <tr>

        <td>John</td>

        <td>28</td>

        <td>Los Angeles</td>

    </tr>

</table>


In this example:


border-collapse: collapse removes the gaps between borders.


The th background color enhances readability.



6. Structuring Large Tables with <thead>, <tbody>, and <tfoot>


For larger tables, you can use <thead>, <tbody>, and <tfoot> to divide the table into header, body, and footer sections. This helps with readability and styling.


Example:


<table>

    <thead>

        <tr>

            <th>Product</th>

            <th>Price</th>

            <th>Quantity</th>

        </tr>

    </thead>

    <tbody>

        <tr>

            <td>Apple</td>

            <td>$1.00</td>

            <td>10</td>

        </tr>

        <tr>

            <td>Banana</td>

            <td>$0.50</td>

            <td>20</td>

        </tr>

    </tbody>

    <tfoot>

        <tr>

            <td>Total</td>

            <td>$15.00</td>

            <td>30</td>

        </tr>

    </tfoot>

</table>


7. Accessibility Considerations


For improved accessibility:


Use <caption> to describe the table’s purpose.


Apply <th> for headers and scope attributes (scope="col" or scope="row") to define row and column headers.


Provide summary data or context if the table is complex.



Summary


In this chapter, you learned how to create tables in HTML using <table>, <tr>, <th>, and <td>. You explored table headers, captions, column and row span

ning, styling with CSS, and structuring large tables with <thead>, <tbody>, and <tfoot>. In the next chapter, we’ll cover how to create forms, a crucial element for collecting user input on your website.


Post a Comment

0 Comments