CSS Table
You can make your table by using the various properties available in CSS. You can select the layout that you want along with the different colors and other properties. We will discuss all these properties in this section.
Select the layout of the table
The
table-layout property allows you to select a layout for your table. This property can have two possible values -
automatic or
fixed. The automatic value sets the column width by the largest unbreakable content of that column. This layout can be slow since, the content of all the cells will be accessed and then the width will be decided. The fixed value is fast since, the column width is independent of the column's content.
Example:
| This is column1 |
This is column2 |
This is column3 |
| This is column1 |
This is column2 |
This is column3 |
Output:
| This is column1 |
This is column2 |
This is column3 |
| This is column1 |
This is column2 |
This is column3 |
Collapse table border
You can choose to collapse the table border or detach it by using the
border-collapse property. It can have either of the two properties,
collapse or
separate. Collapse is the default value. It causes the table borders to collapse into a single border. Separate value detaches the borders.
Example:
| Serial No. |
Name |
| 1 |
Griffin |
| 2 |
Arthur |
| 3 |
Peter |
| Serial No. |
Name |
| 1 |
Griffin |
| 2 |
Arthur |
| 3 |
Peter |
Output:
| Serial No. |
Name |
| 1 |
Griffin |
| 2 |
Arthur |
| 3 |
Peter |
| Serial No. |
Name |
| 1 |
Griffin |
| 2 |
Arthur |
| 3 |
Peter |
Handle empty cells
You can choose to display empty cells or hide them.
empty-cells property allows you to do so. It can have two possible values -
hide, which hides the empty columns which is also the default value and
show, which displays the empty columns.
Example:
| Serial No. |
Name |
| 1 |
Griffin |
| 2 |
Arthur |
| 3 |
Peter |
| Serial No. |
Name |
| 1 |
Griffin |
| 2 |
Arthur |
| 3 |
Peter |
Output:
| Serial No. |
Name |
| 1 |
Griffin |
| 2 |
Arthur |
| 3 |
Peter |
| Serial No. |
Name |
| 1 |
Griffin |
| 2 |
Arthur |
| 3 |
Peter |
Set position of the table caption
The
caption-side property allows you to set the position of the caption.
Example:
Table caption
| Serial No. |
Name |
| 1 |
Griffin |
| 2 |
Arthur |
| 3 |
Peter |
Output:
Table caption
| Serial No. |
Name |
| 1 |
Griffin |
| 2 |
Arthur |
| 3 |
Peter |