HTML & CSS Interview Question #20

What are the main elements used to create an HTML table?

HTML Fundamentals, Structure, Semantics, Links, Images, Tables & Forms Junior Beginner

Quick Interview Answer

A table uses table as the container, tr for rows, th for header cells, and td for data cells. thead, tbody, and tfoot can group logical table sections, while caption provides an accessible table title or description.

Detailed Explanation

A table uses table as the container, tr for rows, th for header cells, and td for data cells. thead, tbody, and tfoot can group logical table sections, while caption provides an accessible table title or description.

Tables should be used for genuinely tabular data rather than general page layout. Header relationships should be clear so users of assistive technologies can understand the association between headings and data cells.

Code Example

<table>
    <caption>Interview Subjects</caption>
    <thead>
        <tr>
            <th scope="col">Subject</th>
            <th scope="col">Questions</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>JavaScript</td>
            <td>200</td>
        </tr>
    </tbody>
</table>