HTML Tutorial
HTML XHTML
HTML Media
HTML Advanced
Here's a tutorial on HTML tables:
HTML (Hypertext Markup Language) tables are used to display data in rows and columns. Tables can be used to organize and display tabular data, such as product listings, financial data, and scientific data.
To create a table in HTML, you can use the <table>
element. Inside the <table>
element, you can use the <tr>
element to create rows, and the <td>
element to create cells. The <th>
element can be used to create header cells.
Here's an example of how to create a simple HTML table:
<table> <tr> <th>Name</th> <th>Age</th> </tr> <tr> <td>John</td> <td>30</td> </tr> <tr> <td>Jane</td> <td>25</td> </tr> </table>
In this example, a table is created with two columns, "Name" and "Age". The <tr>
element is used to create rows, and the <th>
element is used to create header cells. The <td>
element is used to create cells for the data.
By default, tables in HTML have no borders and the cells are separated by space. You can use CSS (Cascading Style Sheets) to style tables, including adding borders, changing the background color, and changing the font size and color.
Here's an example of how to style a table using CSS:
<style> table { border-collapse: collapse; width: 100%; } th, td { padding: 8px; text-align: left; border-bottom: 1px solid #ddd; } th { background-color: #f2f2f2; } </style> <table> <tr> <th>Name</th> <th>Age</th> </tr> <tr> <td>John</td> <td>30</td> </tr> <tr> <td>Jane</td> <td>25</td> </tr> </table>
In this example, CSS is used to add a border to the table, set the background color of the header cells, and add a bottom border to the cells.
By using HTML tables and CSS styling, you can create customized and visually appealing tables to display your data.
Creating tables in HTML:
<table>
element, and rows are defined with <tr>
, columns with <td>
(data cell), and <th>
(header cell).<table> <tr> <td>Row 1, Cell 1</td> <td>Row 1, Cell 2</td> </tr> <tr> <td>Row 2, Cell 1</td> <td>Row 2, Cell 2</td> </tr> </table>
HTML table structure:
<tr>
), cells (<td>
or <th>
), and may have a header (<thead>
), body (<tbody>
), and footer (<tfoot>
).<table> <thead> <tr> <th>Header 1</th> <th>Header 2</th> </tr> </thead> <tbody> <tr> <td>Row 1, Cell 1</td> <td>Row 1, Cell 2</td> </tr> <!-- Additional rows --> </tbody> <tfoot> <tr> <td>Footer 1</td> <td>Footer 2</td> </tr> </tfoot> </table>
HTML table rows and columns:
<tr>
and columns using <td>
(data cell) or <th>
(header cell).<table> <tr> <td>Row 1, Cell 1</td> <td>Row 1, Cell 2</td> </tr> <!-- Additional rows and cells --> </table>
Styling tables with CSS in HTML:
border
, padding
, background-color
, and more.table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
Customizing table borders in HTML:
border
, border-collapse
, and border-spacing
.table { border-collapse: separate; border-spacing: 10px; }
Responsive design for HTML tables:
@media (max-width: 600px) { table, th, td { display: block; width: 100%; } }
HTML table headers and captions:
<th>
, and a caption can be added with the <caption>
element.<table> <caption>Table Caption</caption> <thead> <tr> <th>Header 1</th> <th>Header 2</th> </tr> </thead> <tbody> <!-- Table body content --> </tbody> </table>
HTML table cell spacing and padding:
padding
.td { padding: 10px; }
HTML table row and column span:
rowspan
and colspan
attributes in <td>
or <th>
elements.<table> <tr> <td rowspan="2">Spanned Row</td> <td>Cell 1</td> </tr> <tr> <td>Cell 2</td> </tr> </table>
Nested tables in HTML:
<table> <tr> <td> <table> <!-- Nested table content --> </table> </td> <!-- Other cells --> </tr> </table>
HTML table accessibility:
<table> <thead> <tr> <th id="header1">Header 1</th> <th id="header2">Header 2</th> </tr> </thead> <tbody> <tr> <td headers="header1">Row 1, Cell 1</td> <td headers="header2">Row 1, Cell 2</td> </tr> <!-- Additional rows --> </tbody> </table>
HTML table attributes:
border
, align
, and bgcolor
can be used to customize table appearance.<table border="1" align="center" bgcolor="#f0f0f0"> <!-- Table content --> </table>
HTML table sorting:
<!-- JavaScript for table sorting --> <script> // Table sorting logic </script>
HTML table colspan and rowspan:
colspan
and rowspan
attributes are used to merge multiple cells in columns or rows.<table> <tr> <td colspan="2">Merged Cells</td> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> </tr> </table>
Adding background images to HTML tables:
table { background-image: url('table-background.jpg'); background-size: cover; }
HTML table alignment:
text-align
or vertical-align
.table { margin: auto; text-align: center; }
HTML table border-collapse property:
border-collapse
property controls whether table borders are collapsed into a single border or separated.table { border-collapse: separate; border-spacing: 10px; }
Table layout algorithms in HTML:
auto
, fixed
, and inherit
.table { table-layout: fixed; width: 100%; }