<table>
is an HTML element that displays data in tabular format. HTML tables use rows and columns to make data and relationships easy to interpret.
You might use a <table>
element to display a timetable, or a product’s pricing structure. Tables display related content in a way that is accessible and easy to understand at a quick glance.
Example
The HTML <table>
element works with some required and some optional nested elements.
Syntax
1 |
<table>
|
2 |
<thead>
|
3 |
<th>First name</th> |
4 |
<th>Last name</th> |
5 |
<th>Occupation</th> |
6 |
</thead>
|
7 |
<tbody>
|
8 |
<tr>
|
9 |
<td>John</td> |
10 |
<td>Doe</td> |
11 |
<td>Scientist</td> |
12 |
</tr>
|
13 |
<tr>
|
14 |
<td>Jane</td> |
15 |
<td>Doe</td> |
16 |
<td>Web Developer</td> |
17 |
</tr>
|
18 |
</tbody>
|
19 |
</table>
|
Result
Browser support
The <table>
tag is supported in all modern browsers as well as many email clients. Read more on caniuse.com.
Attributes
The <table>
tag supports Global Attributes in HTML. Global Attributes are common to all HTML elements and can be used on all of them (though they may not have much of an effect on some of them).
Content
Within tables you can use a variety of additional HTML elements.
Add <caption>
for Better Accessibility
To increase accessibility, try adding a <caption>
element immediately after the opening <table>
tag, giving a clear description of the table’s purpose.
The <caption>
will help someone understand what content is being presented and if they should spend time digesting it. Additionally, the <caption>
element provides cues to assistive technologies like screen readers.
Styling HTML Tables
Tables are rigid but can be styled to meet most needs. CSS properties that might be useful in styling a <table>
element include width
, border
, border-style
, border-color
, border-width
, border-collapse
, border-spacing
, margin
, padding
, text-align
, and vertical-align
.
Mobile Device Considerations
Learn More
After their introduction to the HTML spec, tables were soon being used to create layouts for web pages. This was a logical development as they allow for columns and nested content.
Graphics packages such as Adobe Photoshop jumped onboard, developing the slice tool, which allowed designs to be “sliced” up and exported in pieces, ready to be reassembled in table cells.
Whilst all this was incorrect use of the <table>
element, there were no layout alternatives at the time. Nowadays, table-based layouts on the web aren’t needed since CSS provides better solutions.
No comments:
Post a Comment