- CSS Tables
- Table Borders
- Example
- Full-Width Table
- Example
- Double Borders
- Collapse Table Borders
- Example
- How to style Tables using CSS
- What are tables in HTML
- Style Tables using CSS border property
- Style Table border using CSS width property
- Style Tables using CSS border-collapse property
- Style Table using CSS width and height properties
- Style Table cells alignment using CSS vertical-align property
- Style Table cells alignment using CSS horizontal-align property
- Style Tables using CSS padding property
- Style Tables using CSS background-color property
- Conclusion
- About the author
- Sharqa Hameed
- HTML Table Styling
- Example
- HTML Table — Vertical Zebra Stripes
- Example
- Combine Vertical and Horizontal Zebra Stripes
- Example
- Horizontal Dividers
- Example
- Hoverable Table
CSS Tables
The look of an HTML table can be greatly improved with CSS:
Company | Contact | Country |
---|---|---|
Alfreds Futterkiste | Maria Anders | Germany |
Berglunds snabbköp | Christina Berglund | Sweden |
Centro comercial Moctezuma | Francisco Chang | Mexico |
Ernst Handel | Roland Mendel | Austria |
Island Trading | Helen Bennett | UK |
Königlich Essen | Philip Cramer | Germany |
Laughing Bacchus Winecellars | Yoshi Tannamuri | Canada |
Magazzini Alimentari Riuniti | Giovanni Rovelli | Italy |
Try it Yourself »
Table Borders
To specify table borders in CSS, use the border property.
Firstname | Lastname |
---|---|
Peter | Griffin |
Lois | Griffin |
Example
Full-Width Table
Firstname | Lastname |
---|---|
Peter | Griffin |
Lois | Griffin |
Example
Double Borders
To remove double borders, take a look at the example below.
Collapse Table Borders
The border-collapse property sets whether the table borders should be collapsed into a single border:
Firstname | Lastname |
---|---|
Peter | Griffin |
Lois | Griffin |
Example
Firstname | Lastname |
---|---|
Peter | Griffin |
Lois | Griffin |
How to style Tables using CSS
Cascading Style Sheets (CSS) are majorly utilized in combination with HTML to style elements such as tables, text, buttons, and images. More specifically, a typical HTML table is not user-friendly and easily understandable; therefore, you should create the required table using CSS in order to enhance its appearance.
This blog will demonstrate the procedure of styling tables using CSS. So, let’s get started!
What are tables in HTML
Tables are considered an essential component of an HTML document. It can be created using the “ ” tag and the sub-table tags “ ” for rows, “ ” for columns, and “ ” for table headers. All of the mentioned tags are the basic tables tag; however, CSS style properties can be used for styling tables.
Some important and basic CSS properties are given below that assist in changing table style:
- Add borders
- Adjust borders
- Collapse borders
- Adjust width and height of the table
- Align vertical text
- Align horizontal text
- Add padding
- Define cell colors
We will now discuss all of the above-given CSS properties to style HTML tables with suitable examples.
Style Tables using CSS border property
CSS offers a “border” property that is used to add borders in a table. To add a border, you can adopt the following example.
Example
The below-given example will display a solid “black” border with “1px” width for whole table “table”, headers “th”, and columns “td”:
Style Table border using CSS width property
In the previous example, you have seen the method to insert a border around a table in CSS. The given “border” property does not specify the width, as you might want to adjust it according to your requirements. To do so, check out the provided example.
Example
In this example, we will create an HTML Table and style it in a way that it will span the entire scan with full width “100%”:
Style Tables using CSS border-collapse property
As you can see, the table created in the above-given example has a separate border for each row and column that might not give a decent look on a website. However, CSS permits you to collapse the borders into a single border using the “border-collapse” property.
Example
Now, we will utilize the “border-collapse” property to style the created table with one single border for both rows and columns:
Style Table using CSS width and height properties
In CSS, the “width” and “height” properties are primarily utilized for change the table matrices. For instance, in the following example, we will set the table width as “90%” and the height of the table header cell as “90px”:
table {
width : 90 %;
border-collapse: collapse;
}
Style Table cells alignment using CSS vertical-align property
CSS “vertical-align” property is utilized for aligning the text in the table cell from the top, bottom, or middle, but you have to select whether to align the row or the columns vertically. Let’s check the provided example:
width : 90 %;
border-collapse: collapse;
}
The above example will vertically align the table cell text to the bottom:
Style Table cells alignment using CSS horizontal-align property
CSS offers “horizontal-align” property for the purpose of aligning the text horizontally. This property can help to align the text to the left, right, or center:
width : 90 %;
border-collapse: collapse;
}
The above-given example will align the text added in the table cell with respect to bottom:
Style Tables using CSS padding property
width : 90 %;
border-collapse: collapse;
}
The below-given images signifies that we have successfully adding “20px” padding in between the border and the cell text:
Style Tables using CSS background-color property
CSS permits you to define colors separately for each table each column, row or cell. For instance, the following table will contain the background of the table header “th” as “orange” color and all “nth-child(even)” even number of rows “tr” will have “grey” background color:
th, td {
text- align : left;
padding: 8px;
}
tr:nth-child ( even ) { background- color :grey }
Output
Conclusion
To Style HTML Tables, you can utilize different CSS properties for adding and adjusting borders, creating collapse borders, adjusting width and height of the table, aligning text horizontally or vertically, adding padding or customizing rows, columns or cells with the specified colors. This blog demonstrated the usage of different CSS properties for style tables for a webpage. Give them a try and enjoy styling HTML tables in a new way!
About the author
Sharqa Hameed
I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.
HTML Table Styling
If you add a background color on every other table row, you will get a nice zebra stripes effect.
1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 |
To style every other table row element, use the :nth-child(even) selector like this:
Example
Note: If you use (odd) instead of (even) , the styling will occur on row 1,3,5 etc. instead of 2,4,6 etc.
HTML Table — Vertical Zebra Stripes
To make vertical zebra stripes, style every other column, instead of every other row.
1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 |
Set the :nth-child(even) for table data elements like this:
Example
Note: Put the :nth-child() selector on both th and td elements if you want to have the styling on both headers and regular table cells.
Combine Vertical and Horizontal Zebra Stripes
You can combine the styling from the two examples above and you will have stripes on every other row and every other column.
If you use a transparent color you will get an overlapping effect.
Use an rgba() color to specify the transparency of the color:
Example
tr:nth-child(even) <
background-color: rgba(150, 212, 212, 0.4);
>
th:nth-child(even),td:nth-child(even) background-color: rgba(150, 212, 212, 0.4);
>
Horizontal Dividers
If you specify borders only at the bottom of each table row, you will have a table with horizontal dividers.
Add the border-bottom property to all tr elements to get horizontal dividers:
Example
Hoverable Table
Use the :hover selector on tr to highlight table rows on mouse over:
First Name | Last Name | Savings |
---|---|---|
Peter | Griffin | $100 |
Lois | Griffin | $150 |
Joe | Swanson | $300 |