Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
In this hands-on course, James Williamson demonstrates the concepts that form the foundation of Cascading Style Sheets (CSS), including styling text, adding margins and padding, and controlling how images display. The course also explores the tools needed to work with CSS, the differences between embedded and external styles, how to use selectors to target elements, and what to do when styles conflict.
I hope you enjoyed working on our first lab. Now even though it may have seemed like it, my goal wasn't to try to frustrate you. Rather, I wanted to test how well you'd learned the various selector types we covered in the previous chapter and when it's appropriate to use them. So in this movie I want to show you the solutions that I came up with for the lab. Now if you got stuck doing the lab, feel free to use this movie as a way of jogging your memory or reinforcing what we covered. If you're finished with the lab, use this movie to compare your answers with mine.
Now keep in mind that there are several different variations for each selector that would work within the style sheet. Just because your solution might be different from mine doesn't mean it's wrong. As I go over to the selectors I wrote the lab, I'll discuss why I made the choices I did, and you can compare those reasons with your own to determine which selector you feel is the most efficient. Just to kind of preview the finished look of the site, I am going to go out to the browser, and here I have the finished file opened. So you can see it looks a lot better. The links aren't underlined.
I'm getting the hover color that I was wanting. If I scroll down, I can see that I've got the alternating colors that I wanted here in my menu. I've got the links styling I wanted throughout the site. My dates are bold the way that I wanted them. If I scroll down, I can see that the copyright paragraph is where I want it to be. The search field is the size that I want it. And if I go to my Philadelphia page, if I scroll below the gallery, I can see that the credit text has been added to the page exactly the way that I wanted it to.
So let's take a look at the selectors that allowed me to do all of that. So I'm going to go over to the main.css, and I'm going to scroll down to about line 622 or so where we were supposed to add our lab selector. So let's just go through them one by one. Now the first selector, which was this one, where we supposed to write a selector that targets every link element, that one is pretty simple. To target every single link on the page, we just used the correct element selector-- in this case an a. To be honest, there really is not a better way to do that than with just a very simple element selector.
Now our next goal was to write a selector that targets the copyright paragraph in the footer. If we look at the structure of this page and we go down into the footer, we can see that that copyright notice is actually in a paragraph with the class of notice, and that's perfect. So if I go back in the CSS, I can see that I used the class selector .notice to target that. I could've used a descendant selector there, but that class attribute is just too good of a styling hook to ignore. So I just used a simple class selector, and now I don't have to worry about the styling affecting any other elements.
Now for the selector that targets the dates inside the upcoming dates list, if I look at this list on the index page, again remember we have an unordered list that these are appearing in and then the dates are surrounded by span tags. So the way that I did this was I used the child combinator to say when any span is a direct child of list item. If I had to guess, I'm guessing that most of you probably just used a descendent selector, am I right? And if you did it targeted it just fine.
So there's nothing wrong with using that, so why did I use this child selector? Well, really the only reason I used it was to deal with the possibility that maybe other lists somewhere within the site later on might be more complex, or I might use nested lists inside of it that also have spans. So this is going to limit the styling to only spans that are the direct children of the list item, but I also might be being a little too specific here. So if you used the descendent selector, that's fine. There's nothing wrong with it. Now, the next one was kind of tricky.
There is no class attribute applied to this alternating links within our list. So really the only solution that we had available to us was the nth-child selector. Now you may have written this a slightly different way than me. You may have come in and instead of even, used 2n. That would work just fine. I like the even keyword, but if you wanted to use one of the groupings, that's perfectly fine as well. Notice, however, how I filtered it so that it applied to the right place. I said li:nth children inside of a nav, which is also inside of the aside.
Now there's another way that I could have done that. If I go back to the index and I look at the section, you can see that this nav has an id of archives. So I also you could have used that. I could come in and just said # archives, and that would've worked just as well. And maybe some of you might have done that. To be honest with you, I do try to avoid using IDs in my selector when possible just because they add so much weight in terms of specificity, which we're going to talk about in the next chapter. But if you used that, it worked just fine.
There was nothing wrong with it, and that would have been a perfectly acceptable solution. Now the next thing I needed to do is write a selector that changed the color of the main navigation links. Now here when I look at the styling hooks I had available to me there, if I go up to this particular nav, I can see that again I has an id of main nav. Then we have an unordered list and the links are all inside list items. There really isn't a lot there other than this id to filter that particular list. So when I go back to my main.css, I can see that exactly what I used.
I used the a:hover pseudo-class, but I used the descendent selector to target that only when those links are found within the main nav region. You may have done something very similar to that. Otherwise, it would have targeted every single link on the page. Now if I scroll down a little bit further, I was tasked to write a selector that targets the search input text box. Now there are a lot of different ways that you could have done this one, and chances are you may not have ended up with the same solution that I did. I used an attribute selector because one of the things I noticed about that particular search field-- let me scroll down and show you what I mean-- is that the input type is equal to search.
Now if I go to the contact page and I were to look through all these form elements, there is not a single one of those form elements who has a type of anything other than URL, email, text, that sort of thing. No searches. So I'm very safe here to use an attribute selector that basically says hey, anytime you find an input that has a type of search, I want you to make this wide, because it's the only search field in the entire site. Again, you could have done that a different way using a certain type of descendent selector.
If you did that, that's fine. That was just my solution for it. Then finally I had to write a selector that will add the usage text to the end of the photos gallery. Now I am guessing because of the fact that we have this content property, it's a dead giveaway for you guys that we're probably going to be using either the before and after, and since we wanted it at the end of the photos, the after is the obvious choice. What I'm curious about is how many of you guys tried something different than photos? Now if I go back to this page and I look at this section where my galleries are, I can see that they're in a div with the class of photos.
You could have also targeted the article. That would've been fine, because it would have placed it so directly after the article, which would still have been right before the archives, which are floating on the inside the page. So it'd have shown up probably in exactly the same place. But since this div is the more immediate semantic element that is the photos gallery, that's the one I chose to target. So I did .photos::after and then the selector itself. So those are my solutions. Now, how are yours compare to this? Now if your styles are working properly, then you succeeded, even if our selectors don't match exactly.
Don't get too caught up in that. Just make sure that you take some time to think about why you chose the selectors that you did and to think about whether or not they were the best option available to you, because as always, as an author of CSS, that's your goal.
There are currently no FAQs about CSS: Core Concepts.
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.