{"id":1350,"date":"2008-02-20T00:00:14","date_gmt":"2008-02-20T05:00:14","guid":{"rendered":"http:\/\/teamtutorials.com\/web-development-tutorials\/styling-tables-using-css"},"modified":"2008-02-20T09:19:22","modified_gmt":"2008-02-20T14:19:22","slug":"styling-tables-using-css","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/web-development-tutorials\/styling-tables-using-css","title":{"rendered":"Styling Tables Using CSS"},"content":{"rendered":"
This is part two of our table tutorial. In part one I explain why you should still use tables and when to use them. No we will apply some style to those ugly tables using CSS. If you haven\u2019t created the table yet please follow the \u201cHow and Why You Should Still Use Tables\u201d tutorial. <\/p>\n
This is the table we will be creating in this tutorial
\n<\/p>\n
Now we are going to need to finish that table.htm file by making it into a proper HTML file. First we will add the open and close HTML tags. Also make sure that you remove the border=\u201d1\u201d setting from the table tag. We will now be using CSS to set the border style.<\/p>\n
\r\n<html>\r\n\r\n<table>\r\n\r\n <tr>\r\n\r\n <th>First Name<\/th>\r\n <th>Last name<\/th>\r\n <th>Age<\/th>\r\n\r\n <\/tr>\r\n\r\n <tr>\r\n\r\n <td>John<\/td>\r\n <td>Ward<\/td>\r\n <td>21<\/td>\r\n\r\n <\/tr>\r\n\r\n <tr>\r\n\r\n <td>Mike<\/td>\r\n <td>Maguire<\/td>\r\n <td>21<\/td>\r\n\r\n <\/tr>\r\n\r\n <tr>\r\n\r\n <td>Sara<\/td>\r\n <td>Lee<\/td>\r\n <td>61<\/td>\r\n\r\n <\/tr>\r\n\r\n <tr>\r\n\r\n <td>Walter<\/td>\r\n <td>Reed<\/td>\r\n <td>1<\/td>\r\n\r\n <\/tr>\r\n\r\n<\/table>\r\n<\/html>\r\n <\/pre>\nThen we will define the body section. The body section is where all of the page content go.<\/p>\n
\r\n<html>\r\n\r\n<body>\r\n\r\n<table>\r\n\r\n <tr>\r\n\r\n <th>First Name<\/th>\r\n <th>Last name<\/th>\r\n <th>Age<\/th>\r\n\r\n <\/tr>\r\n\r\n <tr>\r\n\r\n <td>John<\/td>\r\n <td>Ward<\/td>\r\n <td>21<\/td>\r\n\r\n <\/tr>\r\n\r\n <tr>\r\n\r\n <td>Mike<\/td>\r\n <td>Maguire<\/td>\r\n <td>21<\/td>\r\n\r\n <\/tr>\r\n\r\n <tr>\r\n\r\n <td>Sara<\/td>\r\n <td>Lee<\/td>\r\n <td>61<\/td>\r\n\r\n <\/tr>\r\n\r\n <tr>\r\n\r\n <td>Walter<\/td>\r\n <td>Reed<\/td>\r\n <td>1<\/td>\r\n\r\n <\/tr>\r\n\r\n<\/table>\r\n<\/body>\r\n<\/html>\r\n <\/pre>\nFinally we will add the head section. The head section contains the page title that is displayed in the browser.<\/p>\n
\r\n<html>\r\n\r\n<head>\r\n\r\n<title>Styling Tables with CSS<\/title>\r\n\r\n<\/head>\r\n\r\n<table>\r\n\r\n <tr>\r\n\r\n <th>First Name<\/th>\r\n <th>Last name<\/th>\r\n <th>Age<\/th>\r\n\r\n <\/tr>\r\n\r\n <tr>\r\n\r\n <td>John<\/td>\r\n <td>Ward<\/td>\r\n <td>21<\/td>\r\n\r\n <\/tr>\r\n\r\n <tr>\r\n\r\n <td>Mike<\/td>\r\n <td>Maguire<\/td>\r\n <td>21<\/td>\r\n\r\n <\/tr>\r\n\r\n <tr>\r\n\r\n <td>Sara<\/td>\r\n <td>Lee<\/td>\r\n <td>61<\/td>\r\n\r\n <\/tr>\r\n\r\n <tr>\r\n\r\n <td>Walter<\/td>\r\n <td>Reed<\/td>\r\n <td>1<\/td>\r\n\r\n <\/tr>\r\n\r\n<\/table>\r\n<\/html>\r\n <\/pre>\nFor the rest of this tutorial we are only going to show the head section of the file, since this is where we will add the CSS code. CSS code goes in style tags when you are including it in the header of an HTML. You will also enclose it in the comment tags like below.<\/p>\n
\r\n<html>\r\n\r\n<head>\r\n\r\n<title>Styling Tables with CSS<\/title>\r\n\r\n<style type="text\/css">\r\n<!--\r\n\r\n\r\n-->\r\n<\/style>\r\n\r\n<\/head>\r\n<\/pre>\nNow we can start to apply some styling to the table. First we will define the table tag. CSS always using the tag name follow by { }. The style for the table element will be inside the { } tags.<\/p>\n
\r\n<html>\r\n\r\n<head>\r\n\r\n<title>Styling Tables with CSS<\/title>\r\n\r\n<style type="text\/css">\r\n<!--\r\ntable {\r\n\t\r\n}\r\n-->\r\n<\/style>\r\n\r\n<\/head>\r\n<\/pre>\nWe are going to give our table a border spacing of 0. Then we set the border-collapse to collapse. This causes the table to have a single lime for a border, rather than the double lines that appear in part 1. Last I am setting a static width of 250px to the table.<\/p>\n
\r\n<style type="text\/css">\r\n<!--\r\ntable{\r\n border-spacing: 0px;\r\n border-collapse: collapse;\r\n width: 250px;\r\n}\t\r\n-->\r\n<\/style>\r\n<\/pre>\nNext we will style the table headers. They were defined with the TH tag. As you can see we align the text in the cells to be centered and we make the font bold by adjusting the font weight. <\/p>\n
\r\n<style type="text\/css">\r\n<!--\r\ntable{\r\n border-spacing: 0px;\r\n border-collapse: collapse;\r\n width: 250px;\r\n}\r\nth {\r\n text-align: center;\r\n font-weight: bold;\r\n}\r\n-->\r\n<\/style>\r\n<\/pre>\nWe will add a 2px padding to the cell. That means that there will be an extra two pixels added outside of the cell. This will be the same color as the background. <\/p>\n
\r\n<style type="text\/css">\r\n<!--\r\ntable{\r\n border-spacing: 0px;\r\n border-collapse: collapse;\r\n width: 250px;\r\n}\r\nth {\r\n text-align: center;\r\n font-weight: bold;\r\n padding: 2px;\r\n}\r\n-->\r\n<\/style>\r\n<\/pre>\nNext we set the cell border properties. Our border is going to be two pixels thick, solid, and white in color.<\/p>\n
\r\n<style type="text\/css">\r\n<!--\r\ntable{\r\n border-spacing: 0px;\r\n border-collapse: collapse;\r\n width: 250px;\r\n}\r\nth {\r\n text-align: center;\r\n font-weight: bold;\r\n padding: 2px;\r\n border: 2px solid #FFFFFF;\r\n background: #4a70aa;\r\n color: #FFFFFF;\r\n}\r\n-->\r\n<\/style>\r\n<\/pre>\nFinally we will change the background color to a blue, and we change the font color to white.<\/p>\n
\r\n<style type="text\/css">\r\n<!--\r\ntable{\r\n border-spacing: 0px;\r\n border-collapse: collapse;\r\n width: 250px;\r\n}\r\nth {\r\n text-align: center;\r\n font-weight: bold;\r\n padding: 2px;\r\n border: 2px solid #FFFFFF;\r\n background: #4a70aa;\r\n color: #FFFFFF;\r\n}\r\n-->\r\n<\/style>\r\n<\/pre>\nOur header cells are done now. Your table should look like this.
\n<\/p>\nLooks good but I would like to add some style to the cells. The rest of our table cells are defined with the TD tag, so we will add that to our css. As you can see we apply the same text-align, padding, and border as the header, but for this cell we will give it a light blue\/grey background color. The easiest way to do this is to copy the code from our TH style.<\/p>\n
\r\n<style type="text\/css">\r\n<!--\r\ntable{\r\n border-spacing: 0px;\r\n border-collapse: collapse;\r\n width: 250px;\r\n}\r\nth {\r\n text-align: center;\r\n font-weight: bold;\r\n padding: 2px;\r\n border: 2px solid #FFFFFF;\r\n background: #4a70aa;\r\n color: #FFFFFF;\r\n}\r\ntd {\r\n text-align: center;\r\n padding: 2px;\r\n border: 2px solid #FFFFFF;\r\n background: #e3f0f7;\r\n}\r\n-->\r\n<\/style>\r\n<\/pre>\nThat is it. Save your file with a .htm extension and open it in Firefox (or you favorite browser) and it should look like this.<\/p>\n
<\/p>\n
That is it for the first table styling tutorial. Look for the next tutorial to go over alternating row styles.<\/p>\n","protected":false},"excerpt":{"rendered":"
This is part two of our table tutorial. In part one I explain why you should still use tables and when to use them. No we will apply some style to those ugly tables using CSS. If you haven\u2019t created the table yet please follow the \u201cHow and Why You Should Still Use Tables\u201d tutorial. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[44,30,17],"tags":[],"yoast_head":"\n
Styling Tables Using CSS<\/title>\n\n\n\n\n\t\n\t\n\t\n