HTML Tables
In HTML, the original purpose of tables was to present tabular data. Although they are still used for this purpose today, many web designers tended to use tables for advanced layouts. This is probably due to the restrictions that HTML has on layout capabilities - it wasn't really designed as a layout language.
Anyway, whether you use tables for presenting tabular data, or for page layouts, you will use the same HTML tags.
Basic table tags
In HTML, you create tables using the <table> tag, in conjunction with the <tr> and <td> tags. Although there are other tags for creating tables, these are the basics for creating a table in HTML.
| Code | Result |
|
|
You'll notice that we added a border attribute to the table tag. This particular attribute allows us to specify how thick the border will be. If we don't want a border we can specify 0 (zero).
You can also add attributes to other table elements, such as the td tag, as you will see below.
Table Width
Widths can be specified using the CSS width property, in either pixels or percentages. Specifying the width in pixels allows you to specify an exact width. Percentages allows the table to "grow" or "shrink" depending on what else is on the page and how wide the browser is.
| Code | Result |
|
|
Table Headers
Many tables, particularly those with tabular data, have a header row or column. In HTML, you can use the th tag.
Most browsers display table headers in bold and center-aligned.
| Code | Result |
|
| Table header | Table header |
| Table cell 1 | Table cell 2 |
|
Colspan
You can use the colspan attribute to make a cell span multiple columns.
| Code | Result |
|
| Table header |
| Table cell 1 | Table cell 2 |
|
Rowspan
Rowspan is for rows just what colspan is for columns (rowspan allows a cell to span multiple rows).
| Code | Result |
|
| Table header |
Table cell 1 |
| Table cell 2 |
|
Color
You can apply color to your table using CSS. Actually, you can apply any applicable CSS property to your table - not just colors. For example, you can use CSS to apply width, padding, margin, etc
| Code | Result |
|
| Table header |
| Table cell 1 | Table cell 2 |
|
Enjoy this page?
-
- Link to this page (copy/paste into your own website or blog):
- Link to Quackit using one of these banner ads.
Thanks for supporting Quackit!