The HTML <table>
element allows web authors to display tabular data (such as text, images, links, other tables,etc.) in a two-dimensional table with rows and columns of cells.
<table> <tr> <th>Heading 1/Column 1</th> <th>Heading 2/Column 2</th> </tr> <tr> <td>Row 1 Data Column 1</td> <td>Row 1 Data Column 2</td> </tr> <tr> <td>Row 2 Data Column 1</td> <td>Row 2 Data Column 2</td> </tr> </table>
This will render a <table>
consisting of three total rows (<tr>
): one row of header cells (<th>
) and two rows of content cells (<td>
). <th>
elements are tabular headers and <td>
elements are tabular data. You can put whatever you want inside a <td>
or <th>
.
Heading 1/Column 1 | Heading 2/Column 2 |
Row 1 Data Column 1 | Row 1 Data Column 2 |
Row 2 Data Column 1 | Row 2 Data Column 2 |
Table cells can span multiple columns or rows using the colspan
and rowspan
attributes. These attributes can be applied to <th>
and <td>
elements.
<table> <tr> <td>row 1 col 1</td> <td>row 1 col 2</td> <td>row 1 col 3</td> </tr> <tr> <td colspan="3">This second row spans all three columns</td> </tr> <tr> <td rowspan="2">This cell spans two rows</td> <td>row 3 col 2</td> <td>row 3 col 3</td> </tr> <tr> <td>row 4 col 2</td> <td>row 4 col 3</td> </tr> </table>
Will result in
row 1 col 1 | row 1 col 2 | row 1 col 3 |
This second row spans all three columns | ||
This cell spans two rows | row 3 col 2 | row 3 col 3 |
row 4 col 2 | row 4 col 3 |
Note that you should not design a table where both rows and columns overlap as this is invalid HTML and the result is handled differently by different web browsers.
rowspan = A non-negative integer that specifies the number of rows spanned by a cell. The default value of this attribute is one (1). A value of zero (0) means that the cell will extend from the current row until the last row of the table (<thead>
, <tbody>
, or <tfoot>
).
colspan = A non-negative integer that specifies the number of columns spanned by the current cell. The default value of this attribute is one (1). A value of zero (0) means that the cell will extend from the current to the last column of the column group <colgroup>
in which the cell is defined.
Sometimes you may want to apply styling to a column or group of columns. Or for semantic purposes, you may want to group columns together. To do this, use <colgroup>
and <col>
elements.
The optional <colgroup>
tag allows you to group columns together. <colgroup>
elements must be child elements of a <table>
and must come after any <caption>
elements and before any table content (e.g., <tr>, <thead>, <tbody>
, etc.).
<table> <colgroup span="2"></colgroup> <colgroup span="2"></colgroup> ... </table>
The optional <col>
tag allows you to reference individual columns or a range of columns without applying a logical grouping. <col>
elements are optional, but if present, they must be inside a <colgroup>
element.
<table> <colgroup> <col id="MySpecialColumn" /> <col /> </colgroup> <colgroup> <col class="CoolColumn" /> <col class="NeatColumn" span="2" /> </colgroup> ... </table>
The following CSS styles can be applied to <colgroup>
and <col>
elements:
border
background
width
visibility
display
(as in display: none)display: none;
will actually remove the columns from the display, causing the table to render as if those cells don’t existFor more information, see HTML5 Tabular data.
HTML also provides the tables with the <thead>, <tbody>, <tfoot>,
and <caption>
elements. These additional elements are useful for adding semantic value to your tables and for providing a place for separate CSS styling.
When printing out a table that doesn’t fit onto one (paper) page, most browsers repeat the contents of <thead>
on every page.
There’s a specific order that must be adhered to, and we should be aware that not every element falls into place as one would expect. The following example demonstrates how our 4 elements should be placed.
<table> <caption>Table Title</caption> <!--| caption is the first child of table |--> <thead> <!--======================| thead is after caption |--> <tr> <th>Header content 1</th> <th>Header content 2</th> </tr> </thead> <tbody> <!--======================| tbody is after thead |--> <tr> <td>Body content 1</td> <td>Body content 2</td> </tr> </tbody> <tfoot><!--| tfoot can be placed before or after tbody, but not in a group of tbody. |--> <!--| Regardless where tfoot is in markup, it is rendered at the bottom. |--> <tr> <td>Footer content 1</td> <td>Footer content 2</td> </tr> </tfoot> </table>
The following example’s results are demonstrated twice–the first table lacks any styles, the second table has a few CSS properties applied: background-color, color, and border*
. The styles are provided as a visual guide and is not an essential aspect of the topic at hand.
Element | Styles Applies |
<caption> | Yellow text on black background. |
<thead> | Bold text on purple background. |
<tbody> | Text on blue background. |
<tfoot> | Text on green background. |
<th> | Orange borders. |
<td> | Red borders. |
th elements are very commonly used to indicate headings for table rows and columns, like so:
<table> <thead> <tr> <td></td> <th>Column Heading 1</th> <th>Column Heading 2</th> </tr> </thead> <tbody> <tr> <th>Row Heading 1</th> <td></td> <td></td> </tr> <tr> <th>Row Heading 2</th> <td></td> <td></td> </tr> </tbody> </table>
This can be improved for accessibility by the use of the scope attribute. The above example would be amended as follows:
<table> <thead> <tr> <td></td> <th scope="col">Column Heading 1</th> <th scope="col">Column Heading 2</th> </tr> </thead> <tbody> <tr> <th scope="row">Row Heading 1</th> <td></td> <td></td> </tr> <tr> <th scope="row">Row Heading 1</th> <td></td> <td></td> </tr> </tbody> </table>
scope
is known as an enumerated attribute, meaning that it can have a value from a specific set of possible values. This set includes:
col
row
colgroup
rowgroup
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/th#attr-scope
https://www.w3.org/TR/WCAG20-TECHS/H63.html
Similar to other programming, markup, and markdown languages, comments in HTML provide other developers with development specific information without affecting the user interface. Unlike other languages however, HTML comments can be used to specify HTML elements for Internet Explorer only. This topic explains how to write HTML comments, and their functional applications.
HTML comments can be used to leave notes to yourself or other developers about a specific point in code. They can be initiated with <!– and concluded with –>, like so:
<!-- I'm an HTML comment! -->
They can be incorporated inline within other content:
<h1>This part will be displayed <!-- while this will not be displayed -->.</h1>
They can also span multiple lines to provide more information:
<!-- This is a multiline HTML comment.
Whatever is in here will not be rendered by the browser.
You can "comment out" entire sections of HTML code.
-->
However, they cannot
appear within another HTML tag, like this:
<h1 <!-- testAttribute="something" -->>This will not work</h1>
This produces invalid HTML as the entire <h1 <!-- testAttribute="something" -->
block would be considered a single start tag h1 with some other invalid information contained within it, followed by a single >
closing bracket that does nothing.
For compatibility with tools that try to parse HTML as XML or SGML, the body of your comment should not contain two dashes --
.
Inline display elements, usually such as span or a, will include up to one white-space character before and after them in the document. In order to avoid very long lines in the markup (that are hard to read) and unintentional white-space (which affects formatting), the white-space can be commented out.
<!-- Use an HTML comment to nullify the newline character below: --> <a href="#">I hope there will be no extra whitespace after this!</a><!-- --><button>Foo</button>
Try it without a comment between the inline elements, and there will be one space between them. Sometimes picking up the space character is desired.
Example code:
<!-- Use an HTML comment to nullify the newline character below: --> <a href="#">I hope there will be no extra whitespace after this!</a><!-- --><button>Foo</button> <hr> <!-- Without it, you can notice a small formatting difference: --> <a href="#">I hope there will be no extra whitespace after this!</a> <button>Foo</button>
Output:
JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used in web development. At…
AJAX (Asynchronous JavaScript and XML) is a powerful technique used in modern web development that…
Introduction After successfully optimizing your website for speed, it's essential to maintain and build upon…
Securing your WordPress folders is crucial to safeguarding your website from unauthorized access and potential…
Creating a file upload feature with a circular progress bar involves multiple steps. You'll need…
Integrating WP Rocket with AWS CloudFront CDN helps to optimize and deliver your website content…