Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Even before I started using CSS I have absolutely loathed the default table borders the browsers apply. Through CSS we can leave those clunky, semi-3D, beveled borders behind, and create streamlined and customized borders. Now like anything else, however, table borders created with CSS have rules that we have to follow in order to get the desired results. Let's dive in and finish off our table by adding some custom borders. I first want to show you where we are. I have opened up the graphic-design.htm file from the 09_06 directory and if I preview this in Live View; I am just going to scroll down, you can see that one of the things I have done is added a few more tables to the page, so you can really see what kind of impact our styling has when these tables are combined together.
Now right now currently it looks like our tables have borders, doesn't it? I mean you can see they have got white border right here between all of the table cells. Well, it's not really a border. One of the things about cell spacing; it's been made obsolete, meaning it's been removed from HTML5, but the big issue with cell spacing is that older browsers, and even current browsers to be quite honest with you. If explicit value of cell spacing isn't set to 0 for example, then it defaults to a value and in most browsers case that is one pixel spacing.
Let me show you what I mean. If I go in the Source Code here and I scroll down to the first table that I have, if I take that table and I set cellspacing to an explicit value of 0; I will save this, and now watch what happens when I click back on Live View. So the spacing that was masquerading as a border goes away and the other tables down below are still sort of facing those spacing issues, if you will. So what we are going to do is we are going to get rid of that cell spacing, but we are going to do it by applying borders to our table.
That's another way to sort of get rid of the default cell spacing that we are encountering here. So I am going to get and get rid of that cell spacing attribute, safe the file, and I am going to set up the workspace similar to what we had before. I am going to collapse my icons down, I am going to make sure that I am in Live View on the Design View and then over here I am going to switch to my main.css and I want to make sure I have enough room to see my tables. There we go! So I am going to scroll down to my table styles which are right around line 465 or so and we are going to add our border to couple of different locations.
Now the first thing I want to do is just show you how borders work on tables and then we will refine them. If I go to the table selector and just come in and enter a border in. So I will do border, let's just do 1 pixel, solid border, and we will just do black so we can see what that would look like. So I am going to go ahead and Save this and now when I click back on my Tables, notice what happens. It might not be what you were expecting. You will notice for example that what we have here is we have this sort of black line that goes all the way around. Well, actually it's doing exactly what we told it to do.
We told it to apply a black border all the way around the ends of the table which is exactly what it's doing. So if we wanted to have a border inside the table, then we need to address that with the th and tds in our table. So with these two guys right here, I am going to come in and apply another border to them and we will do the same border; 1 pixel, solid, black. Save that and now I want to click back on my file, well, that doesn't look at all like I was expecting it to look.
What we are getting here is we are still getting that cell spacing gone on and then you can see that the border is going around each of the individual table cells. Now if the cell spacing wasn't there, if we had set cell spacing to 0, these tables cell spacing would collapse, but the borders wouldn't. So we would end up with a 2 pixel wide border instead. Well, there is very easy fix for this entire mess. All I have to do is go up to my table selector and I use a property that's not accessible to us in the CSS rule definition dialog box, which is one reason why we're coding this by hand actually and I am just going to click on the next line and I am going to enter the border-collapse property and I am going to tell it to collapse.
Now what that means is as if two borders are touching, instead of honoring both their values and having a totally cumulative border of two pixels it's going to collapse down and take the larger of the two values. So now if I save this, click back inside, you can see what that does. It collapses those table cells, the cell spacing is eliminated and now I am left with a 1 pixel black border all the way around it. That doesn't actually look that bad. I kind of like that. However, I want to try to do something a little bit more custom. What if we did a border where we had a white divider between the headers in the table cells and then just another white divider that divides the two columns together? Well, doing something like that, a custom border like that, becomes a little bit more tricky.
So let's take a look at how we would do that. The first thing I want to do is I want to go back to my table selector and I am going to change the border that I have applied here. I am going to change this to 3 pixels because I am going to make it little bit larger and I am going to change it to white. Now you might say to yourself, but wait a second, why would you have a white border all the way around the outside of the table, the background is white? Exactly! So what that's going to do for me is it is going to make this tables look a little tiny bit smaller, but when I have borders here, that sort of come together, basically this line in the outside is going to blend in with them and you are not going to see this white line jutting out, if you would.
If would look really jagged if I didn't do that. I am going to scroll down and I need to take the border off of the th and the tds, because we need to customize this so that the table headers are getting one border and then the table cells are going to be getting a different flavor of the border. So I am going to go up to the th selector which is just above that and for that I am going to do a border-bottom. I am only going to apply a border to the bottom of the table header cells and I am going to use the same border 3 pixels solid and white.
I will save that and now if I click over on my tables you can see what this does for us, it gives us a nice little separator between our table headers and the cells themselves. Now we've could also used margins to do that for us since we have a white background. But that means that if these table appear anywhere else or if I change the background color of the article, I'll still have that white space regardless of what the background color of the article is. Then finally I am going to go to my table data cells, but what I am going to do is I am going to come down to the td, th selector. So this one right here, the one we just took the border off of.
What I am going to do is I am going to come in here and create a new line. I am just going to do border-left and I am going to do a left border of 3 pixels solid and white. Now you know why we have a border all the way around the outside of the table, because each table cell is going to have a 3 pixel left border on it, but not one on the right-hand side. So I would have an offset over here if I didn't have this white border all the way around that. I am going to go ahead and save this and now when I click back into my tables you can see that now we have this custom border where it looks like there is little bit of margins or spacing going on here, but in reality we are just using custom borders to achieve the same effect and you can see it because we have multiple tables on there, each one of them are getting the styling.
Now even though our table is extremely simple we've spent a great deal of care in properly structuring and styling in it. As your tables get more complex, you'll want to pay even closer attention to a structure and if you've never worked with them before you should learn about additional table structural elements like thead and tbody that can help structure complex table data.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.