Most CSS tips for formatting tables are only useful if you only want one style table on every page on your website. The following shows how to set up for more than one style on a page. Setting up tables in CSS also eliminates an enormous amount of code bloat for large tables because you don't have to apply font colors and sizes in every table cell or row. Also, unless you format your tables with CSS your page will no longer validate if you are using HTML5 and CSS3.
Formatting a Table in CSS
Step 1: Pick a Name
First you need to pick a name that describes your table. You can name it according to size, or color or according to what it contains. You can also name it with a number. I will call this one table1.
Step 2: Set up a Class in CSS
Next you set up a class for the table in CSS that controls the table's position on the page, it's width, font size and border size.
Step 3: Define Table Width, Font size and Border:
The following table will be centered on the page with width set at 200 pixels. It also uses font size 14 pixels and a solid black border around the outside edge of the table. Border-collapse was added to eliminate double borders on each cell (if you are setting up tables for a mobile site use percentages for width and ems for fonts).
border:1px solid #000;
If you want table headers with different formatted text than the regular table cells then set up a class for a TH tag (table headers). This will center the header vertically and horizontally in each cell with a border around each cell:
border:1px solid #000; }
Step 4: Set up Cell Data in CSS
The following CSS applies formatting to each TD cell (table data). In this case it is a blue colored font with a border around each cell and text centered vertically and horizontally.
border:1px solid #000; }
All the above CSS for the table, the TH and TD cells can all be applied to the following table just by adding class="table1" to the table tag (if a TH cell follows, it will apply that portion of the CSS file and the same for the TD cells).
<tr><th> Item Name </th><th> Price</th></tr>
Here is the finished table:
How to color separate cells in different colors
If you want to color the text in one table cell a different color just set up a class for that color in your CSS file like this:
Then add that class to any table TD or TH tag in the HTML like this:
If you have a lot of different colors or formatting on different cells then you should set up classes for different columns and rows.
How to Align Different TD or TH Cells Left or Right
If you want some cells left aligned and some right and others centered set up classes for left, right and center in your CSS file like this:
Pick out the alignment that is used most and add that to the CSS TD or TH tag. Then anytime you want to change the alignment for just one TD or TH cell add that class to that specific cell like this:
If you have the same formatting on several cells is will save coding if you add those classes to the column or rows instead of the individual cells.
Add a Background Color Behind the TableYou can apply a background color to the table tag or different background colors to the TD tags vs the TH cells by adding the following to the appropriate class mentioned above:
This table has a light blue background:
Adding more than One Table Per Page
If you want a 2nd table on the same page with different settings then you need to set up another class in CSS with another name like "table2". Add that new class to the TD and TH settings also.
Reduce Code Bloat by applying one "buy" button for all cells
If you want to include a "buy" button with a link to a shopping cart in your table you can reduce a lot of code bloat by loading the same button in each cell as a background image (this eliminates the need to apply width, height, border and an alt tag to every button) but be sure to include the url to the relevant cart page if your items are on different pages. You'll need to increase the width of the table by one cell. Set up the class for the button like this:
Now just add a div in the appropriate cell with class="buybtn" in between each link and it's closing link tag like this:
I have set up another class for this table called table2 as it requires different formatting than the table above (view source code at top of page for table1 and table2). The table should display like this:
Return to CSS TIPS
Copyright © 11-07-08, updated 11-11-17
Lori's Web Design
All Rights Reserved
Return to CSS TIPS