Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Although, our table isn't that large, the final version of the page will actually have multiple tables stacked one on top of the other. Anytime users are faced with a long vertical region of tabular data, it's a good idea to give them some type of visual aid to assist them with reading it. For our semester requirement tables we are first going to finish the basic styling of the table and then make that data a little easier to read by creating alternating background colors within the rows. So I am still working on the graphic-design.htm file. Now I have opened this one up from 09_ 05 folder, but to be honest with you, if you just wanted to pick it up and continue to work from the previous exercise, you can certainly do that.
I have my screen setup the same way. I have a Split screen view between Code and Design. The Design View has Live View enabled, so that I can see the changes as they are going to be rendered into my table and I'm focusing on the table styles right around line 483 or so. Now one of the reasons why we were at such a good stopping point in the previous excise is because I wanted to introduce you guys in this movie to some of the new selectors in the CSS Level 3 selector specification. Now they are not really new, people have been using them for a while now and their support among modern browsers is really, really good, but they're still newish enough to where a lot of web designers aren't familiar with them.
So the first thing that I'd like to do is I'd love to control if I could the width of these columns. Now there's a couple of different ways that you can do that. You could create a class and go ahead and tell the table cells, such as the table header cell to be a certain width. If one cell within a column is a certain width, then all the rest of them will follow. So I don't necessarily need to apply that type of class to every single table cell in a column. However, even that is pretty inefficient, because you have to go to the trouble of applying class to the table cell, and you have to deal with the fact that you're applying some non-semantic class attributes.
So what I love about some of these new selectors is they allow us to target elements based upon their relationship to their siblings and their parents and that's a really powerful way of targeting things especially within structured elements like tables. So I am going to go over to my Code and I am going to create a new selector and this time I am going to target article th. Now that's nothing new, we know that that will target any table header cell within the article that we are looking at. But I'm going to add a pseudo-class selector to this and to do that, I type in a colon that adds the pseudo-class selector and then I am going to type in first-child.
So what that's saying is go inside your article and find any table header that is the first child of its parent. In this case it's going to target this table header right here because it's the first table header inside the table itself. So now I'm able to go ahead and set a width on that, I am just going to set the width to 75% and I'll save my file and when I click over here, you can see, well, it didn't really need to resize that much, did it? But it did resize and now because I don't know how long all these different course names are going to be and I know that the credit hours are pretty small, I can just go ahead and expand that to fit 75% of the Width and now I have got more room for these guys and less of the room over here and I really again don't need that much of it. Okay.
Now another thing I'm going to do is I am going to tackle the alignment of the text inside the table. Now I am not just talking about left align, right align that sort of thing, but what's really nice about table cells is that you can also tackle the vertical alignment of content. So I am going to go down to the next line and I am going to type in article th, article td. So this is a group selector and this allows us to basically group two elements together at the same time and style both of them.
So we are styling any table data cell found within an article and any table header cells found within an article. So this should apply to all the cells in our table. And what I am going to do here is I am going to type in vertical-align and I want that to be middle. Now I am going to save this and then when I click back on my table, I am not really going to see anything right now, because we don't have any padding top or bottom, but do notice what it did in the headers. You can tell the text is sort of centered vertically if you will, right in the middle of the table cell and that's exactly what I wanted.
Now speaking of padding, we do need to add some to our individual table cells themselves, so I am going to go down to the next line and type in, article td and then inside that I am just going to go ahead and do a padding of .4ems top and bottom, space, 1em right and left. So when you're using two values in that shorthand notation, what that does for you is the first value is top and bottom, the second value is right and left, and now when I click on my table to refresh that, you can see what's happening with the table cell information. I have some space above and below it, I also have some space to the right and to the left of it, and because we just did a vertical line, it's sort of floating there in the middle, which is very nice.
Now another thing I would like to do is I would like to take the text in the credit hours and I would like to center that underneath the actual text, so that it looks a little bit nicer in there. I also like to bold that text. Now, in our table we already have some text that's bold, we took the course IDs here and we wrapped those in a strong tag, so you might think, well, why don't we just go ahead and wrap these in the strong tag as well. Well, actually wrapping these in a strong tag is somebody structural that we did. We wanted to call attention to those. Essentially, for the credit hours, I just want to do that as a visual thing, I don't necessarily need to draw the reader's attention to it.
A lot of times that is a judgment call and in this case, I'm thinking that for the credit hours, we could probably do a little bit better using CSS, than we can through these with strong tag. Now the next thing is in terms of the formatting requirements for that, we just need to target only these table cells. That can become a little bit difficult, but then again this is where pseudo-class selectors can come in and help us. So just below the article td, I am going to write the following selector, type in, article td:nth-child, and I am going to pass into that (even).
Now this is a pretty complex selector. If you've ever worked with nth-child selectors before, you know how much you can do with them, but if you haven't, let me talk to you a little bit about nth-child. So nth-child selectors allow you to choose an element based on its pattern within its parent. So essentially what we are passing into this is we are saying, nth-child whenever a table data is the even number within a parent, select that, and if you think about actual td tags themselves, this is our first one, this is the second, third, fourth, fifth, sixth, I think you get the idea.
So this column, they are all going to be even numbers. So that allows me to write a selector that targets just this column, not this column, and I don't have to do any class applications or write any class attributes, which is really nice. So inside this, I am just going to go ahead and type in font-weight: bold and text-align: center. So I am going to save that and click and you can see it centers the text within that column and it bolds it as well.
Now we are almost finished, but the last thing we want to do is we want to create these alternating colors, and again, nth-child selectors can assist us with that. So I am going to go down to the next line and actually you know what, I want to organize my style, so I want to move this up, because we are going to be targeting table rows. So I am going to go just below the existing table row selector and once again I am going to type in article tr, and then again, we are going to do nth-child(even). article tr:nth-child(even). Okay, so what's this going to target for me? Well, remember, it's doing every even number row, the first row would be your header, so it's going to start with this row, which is the second one, and then it's just going to jump each time ignore, target, ignore, target, ignore, target.
What's really nice about this in terms of an nth-child selector, I no longer have to worry about how many rows I am going to have in my table or assigning a class-attribute to every other row. That's impossible almost to do in dynamic tables, so this gives us a really nice ability to just target any table on the page and always know that I am going to have these alternating rows. So I am going to click inside of this and I am just going to type in background and then rgb and the color I am going to type in is 249, 228, 130.
Now it's going to be sort of a pale yellow color. If I save this and click inside of my table, you can see starting at the second row, that now becomes that sort of pale yellow color and then it alternates each timeout. So nth-child selectors are incredibly useful, especially for elements with repeating structures such as tables or lists. Now in our own table, we have used them to create alternating rows of colors and set the width of our table columns. Now if you have never worked with them before, be sure to explore the W3C Specification on CSS selectors, as they have a ton of more options for targeting page elements, than the ones that I've shown here.
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.