HTML table tags
HTML table tags and examples of table tags
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.
Simple Table Example
|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|
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>.
HTML Table Spanning columns or rows
Table cells can span multiple columns or rows using the colspan and rowspan attributes. These attributes can be applied to <th> and <td> elements.
|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.
HTML Table Column Groups
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.).
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.
The following CSS styles can be applied to <colgroup> and <col> elements:
- 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 exist
Table with thead, tbody, tfoot, and caption
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.
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.
HTML Table Heading scope
th elements are very commonly used to indicate headings for table rows and columns, like so:
This can be improved for accessibility by the use of the scope attribute. The above example would be amended as follows:
the scope is known as an enumerated attribute, meaning that it can have a value from a specific set of possible values. This set includes
ATutorialHub Related Guide
AWS Amazon EC2-Deploy a MERN Stack application to AWS Amazon EC2
Sea battle game only using HTML and CSS
HTML Tutorials Comments (9)
panduranga gupta2021-07-05 07:03:13
good website for learning and help me a lot
The awsome website i am looking like for a long time, good work atutorialhub team keep doing
Learning a lot from the courses present on atutorialhub. The courses are very well explained. Great experience
It is very helpful to students and easy to learn the concepts
Great job Tutorials are easy to understand Please make use of it
Zain Khan2021-09-18 15:07:23
Great content and customized courses.
Rudrakshi Bhatt2021-09-09 15:08:10
Well structured coursed and explained really well!
Pavana Somashekar2021-09-11 15:09:08
Good platform for beginners and learn a lot on this website