Table
HTML tables allow web developers to arrange data into rows and columns.
Learning Objectives
- Differentiate the data table from the layout table.
- Use the
<table>
and</table>
elements in a web page. - Create a borderless table for a horizontal navigation bar with text links.
- Design tables using partition elements like
<thead></thead>
and<tbody></tbody>
. - Create tables in a web page that span cells using rowspan and colspan attributes.
- Improve the appearance of table using additional CSS properties.
Data Tables vs Layout Tables
Data tables
Very often holds numbers, but they can hold text and other types of content as well.
Layout tables
Are not limited to holding data - they are allowed to hold any type of content. Their purpose is to position that content with a row-column layout scheme.
Example of Data Table
Student Name | Student Number | Age |
---|---|---|
Juan Dela Cruz | 2021-21067 | 18 |
Pedro Cruise | 2021-21114 | 19 |
Simon Ibarra | 2021-21584 | 18 |
Example of Layout Tables
Table Elements
- An HTML table is defined with the
<table>
and</table>
tags. - Each table row is defined with the
<tr>
and</tr>
tags. - A table header is defined with the
<th>
and</th>
tags. - By default, table headers are bold and centered.
- A table data or cell is defined with the
<td>
and</td>
tags.
Students List Web page
Student Name | Student Number | Age |
---|---|---|
Juan Dela Cruz | 2021-21067 | 18 |
Pedro Cruise | 2021-21114 | 19 |
Simon Ibarra | 2021-21584 | 18 |
Formatting a Data Table
Properties | Definition |
---|---|
border-style | use to specify a table border. |
border-width | use to specify the border’s width. |
border | a shorthand property that handles a set of border-related properties. |
padding | use to specify the space between the cell content and its border. |
border-collapse | use to collapse borders into just one border. |
Students List Web Page With border
Property
Students List Web Page With border-collapse
Property
Table head and Table body
thead
thead
is used to group header content in an HTML table
tbody
tbody
is used to group the body content in an HTML table.
Students List Web Page With thead
and tbody
Elements
Cell Spanning
Attributes | Description |
---|---|
colspan | Defines the number of columns a table cell should span. |
rowspan | Specifies the number of rows a cell should span. |
Students List Web Page With border-collapse
Property
Table Size
The size of the table is define using width
and height
properties.
Table Alignment
The text-align
property is used to set the horizontal alignment of the content in <th>
and <td>
elements.
The vertical-align
property sets the vertical alignment of the content in <th>
and <td>
.
The value for vertical-align
property can be top, bottom, or middle.
Horizontal Dividers
Use the border-bottom
property to <th>
and <td>
for horizontal dividers.
Hoverable Table
Use the :hover
selector on <tr>
to highlight table rows on mouse over.
Example
Last updated on