Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the past few exercises, we've added considerable structure to our table. Before we move forward for styling our new structural elements, we need to take a step back and talk about styling the basic elements of our table. As you're creating your tables, you need to have a clear idea as to the parts of your table you plan on controlling through visual styling, versus what you'll be controlling with the table properties. In the case of our table, we are going to control padding, table width, and borders through our styles, eliminating the need to stay these properties in our code. Although you can control cell padding through styles, it's still a good idea to pass a value of zero for cell padding.
Some browsers will add default padding to the table if no value is passed. Another thing to note when planning styles is that cell spacing remains one property that we can control through table attributes but not through styles. We'll set that value manually as well and then write the styles to control our basic table formatting. So again here we have the bigsur_trails file open, so pretty much our structured table is sitting there on the page just waiting for something to happen to it so that it doesn't have to look quite so boring.
So the first thing we're going to do is take a look at its attributes. So I am going to click inside the table, find my table tag in the Tag Selector and click on that, and then go down to the Properties Inspector and verify that cell padding, cell spacing and the border or all set to zero. They are, and that is a good thing. Now we are free to go in and style those ourselves. So what I am going to do now is open up my CSS Styles panel. I am going to collapse the Files panel by double-clicking that tab and that's going to give me a little bit more room to come in here and adjust my styles.
So what I am going to do now is scroll down through my styles and I am just looking for these mainContent styles. I am just going to scroll down and find the last of my mainContent styles, right there above spotlight. Now the reason I am highlighting it there is because we have to create some new rules and this is going to help me control placement of where those rules are going to be in our external style sheet. So the next rule that we create will be created directly underneath the one that we have highlighted here. So again, down to my CSS Styles panel. I am going to go down and create a brand new CSS rule.
Now at this point we want to do a compound selector and there is a lot of stuff going on here in my selector name, so I think it probably easier if I just typed this in myself. So let's do #mainContent. Motice that that is all one word, capital C, lower case m. Space, table.data. So let's talk about what that selector is saying. That selector is saying. go find a table that has the class of data applied to it, that's inside of an element with an ID of mainContent.
So it's a very specific rule but it's targeting that table based upon the fact that it is a data table. We are going to define that in main.css and go ahead and click OK. All right, let's go down to our Box category and for Box we are going to inner in a width for our table. Let's make our table 95% wide. So it's going to take up the majority of the mainContent region. There is not going to be anything to the left of it. There is not going to be anything to the right of it. So we really don't need to leave a lot of space on either side.
For a margin deselect Same for all. Let's do 0 margins for the top, 30 margins for the bottom and for the right and for the left we'll do auto. So the remaining 5% of the available width will be split on the right-side and the left-side and that's going to center the table within that region. The 30 pixels worth of margin on the bottom are just going to help keep our table away from any content that might be below it. Now just like when you're writing your CSS for your page, you can place styles on the top parent tag and they'll inherit down to any nested elements inside of this.
We are going to take advantage of that by applying some typography to our table tag knowing that all of our table headers and table cells will inherit these values. So for font family choose Arial, Helvetica and sans serif. For font size choose .9ems. So we are going to make it just a tidbit smaller than the text around this and we're going to make the font- family Arial, Helvetica and sans serif. When I click OK my table re-styles.
So it shrunk in a little bit. It's centered within the mainContent region, the font for the text changed, and the text size changed, and those changes were exactly what we wanted. Okay now in addition to styling just the main table tag, we're also going to style the table data tags and the table header tags. So we'll start by doing our table data tags. Once again I am going to come over to the CSS Styles panel, I am going to choose New CSS Rule, again we are going to do a compound selector, and this time we're going to do #mainContent table.data. So that's exactly the selector we had last time, but this time also type in a space td.
So now we are saying a table cell found within this data table, found within the mainContent. Go ahead and click OK and here w're just going to do a slight variation on the information that we had on our table. For font-size type in .8ems. Now that means that the text inside the table cells will be smaller than the text within the table headers, and the table headers will be a little bit larger. Also go to your Box category and here's what we are going to style our cell padding. So for padding deselect Same for all. We're going to do zero for top, zero for bottom and then for right and left, let's do 10 pixels worth of padding.
So, this going to give us an equal amount of space on the right and left and not give us any padding at all on the top and the bottom. I am going to go ahead and click OK, and I could see our table cells have a little bit of extra padding in them. Now you may have wondered, why didn't we do any padding top to bottom? Now the text is still butting right up against the top edge of the table cell. Why not? Sometimes you only have to write a style once and later on we are going to control the height of the table cells by writing a line height for our table headers. That will drive the entire row, so we don't necessarily need to worry about a top-level padding for our table cells. In fact that can be problematic because it can add to the spacing or overwrite some of the other values we want.
So if we don't explicitly need it, we're simply not going to add it. All right, one more rule here. So let's go down and create a new CSS rule and this time we're going to type in #mainContent table.data th. So in this time instead of the table data cells we're affecting the table header cells. Go ahead and click OK. Here for font-size we're going to type in 1em, so they are going to be larger than individual table data cells themselves.
Font-weight is going to be normal. Sometimes we just want to overwrite a browser's default styling. Most browsers will render table header cells as center aligned, bold, and some even go so far as to make them italic. So we are going to do font-weight of normal. We are going to go to our Block category and choose text-align left. We are going to go to our Box category and for padding we are going to do the same as the table cells themselves. Zero for top and bottom, 10 pixels for a right and left.
So that will give us the same amount of spacing on other side of our table header cells as our table cells themselves. One last thing. Let's get back to our Type category and remember a minute ago when I talked about a table header's line-height controlling the height of the actual table cell itself? Well let's make the line-height 2.6 ems. Now when we click OK, notice that these table cells have gotten a lot taller. Our headers don't look quite as different as it did before, so they are not bold and centered anymore, and we've got that 10 pixels worth of space on either side through each one of our table cells.
So our table does look a lot more presentable, if not fully designed quite yet. We've laid down the foundation for our more decorative styling. By placing the global fundamental styles on the basic structural tags like we've done here, it allows us to use additional structural elements such as the thead, the tbody, for more decorative or unique effects. Note also that we have achieved our height, width and table alignment through styles and not the HTML table properties.
Get unlimited access to all courses for just $25/month.Become a member
58 Video lessons · 65549 Viewers
61 Video lessons · 96332 Viewers
56 Video lessons · 110088 Viewers
82 Video lessons · 105062 Viewers
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.