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.
So now it's time to put what you've learned about selectors into practice by tackling our first lab. Now I've prepped a few pages of my fictitious desolve.org site that I want you to work on. Unlike the other exercises that we've completed so far, this is a true lab, so while I'm going to tell you what your goals are, I'm not going to tell you how to do it. In this lab, we're going to focus on properly targeting elements and writing efficient selectors. So the first thing I want you to do is go to the 02_18 folder and open up the contact.htm, galleries.htm, index.htm, philadelphia.htm, and inside the _css directory, the main.css.
Now we've been working with these really simple files throughout all of our exercises as we concentrate on the concepts. This is more of an applied lab, so this is more of a finished site. I just want to preview this for you so you can see kind of what you're going to be working with. So I'm just going to the index page and open this up in one of my browsers. So here is our site, and it's only partially styled. Some of the styles are finished; some of the styles are not. Your goals are going to be targeting specific elements within this site.
This is the index page, but we also have a page for contact. That's the contact page that we saw earlier. We have our galleries page where we have all these different galleries, and then we have one individual gallery page, and that would be Philadelphia. So the styles that you write are actually going to be targeting things on all four of these pages. If I go back into the structure of the code, each one of these pages has a link to the main.css file. So whereas in most of our exercises we've been adding the styles locally to the pages, in this one you're going to be adding your styles to the main.css file.
All the pages are structured in a very similar manner. Inside the body tag we have a header that has our pageHeader. Following that, we have a nav, which has the main navigation for the page. Notice the IDs of these sections as well. We have a section of our content. On the index page we have a banner that we don't have on other pages. Inside this content section, we typically have an article. The article's ID reflects the page that's on in the content that is inside of it.
And inside that we also have some individual sections, depending upon the pages that you're looking at. In this case, I have two sections with the class of news. Below our article, we have an aside, which serves as our sidebar. The aside is made up of a couple of elements. One of those is another nav. This is the nav with the ID of archives. Underneath that we have a couple of sections. These are our contest sections. The first one is this month's contest and the second one is the previous contest, but notice that they both have the class of contest applied to them.
And then finally we have our page footer that has that ID pageFooter. So that's how our HTML is organized. It's different depending upon which page you're on, but the overall structure is pretty much the same. If I go over to the main.css, this is a really large style sheet. It's actually an average-sized style sheet to be quite honest with you. At the very top of it I have a color guide to help us when we are choosing colors a little bit later on, and then the styles are organized by regions and by style type.
So global styles, the type of formatting that goes everywhere, comes first. After that, I have a section on basic layout styles. After that, I have a section on content region styles, and even those content region styles are organized by regions, so headers get its own region, the nav styles have their own region, that sort of thing. The most of the time you would go in and you would work within the appropriate section. Just to make this initial lab a little bit easier on you, I have actually grouped the selectors that we need down towards the bottom.
If you're in the CSS file with me, go ahead and scroll all the way down towards the bottom of it. We're going to go down to about line 618. On line 618 I have the Add lab selectors here. I'm not going to ask you to do any of the formatting. We haven't gone over enough properties yet for you to really tackle the formatting options. What I'm interested in, and what our focus was for this chapter was on writing selectors. Your goal is going to be targeting the appropriate elements on the page using the appropriate selector.
So I want to go over each one of these needs one by one and show you what they are going to style. The first one I want you to do is write a selector that targets every link element throughout the entire site. On our pages we have different links. We have them in different places. Some of them would be within the body copy, some of them are within the navigation. I want you to write a very generic selector that targets every single link all the way throughout the site. The second selector that I want you to write is write a selector that targets the copyright paragraph in the footer.
Now when you start getting into things like this, you're going to have to be really familiar with the structure of the page, and I know that one of the hardest things to do is to go into somebody else's page that they created and sort of sift through the way that they structured it, because it's almost always different than the way that you structured it. But what this does is it reinforces the fact that if you don't know the structure of the page you cannot write a selector for it. So you need to go back into the HTML, find those elements, such as the footer, and as this selector is asking us to do, write a selector that targets the copyright paragraph in the footer.
That would cause you to go back to that footer, look in the footer and find the copyright paragraph, and then figure out how to target this independently of anything else on the page. You'll need to do the same thing for the next one, which is asking you to write a selector that targets the dates inside the upcoming dates list on the index page. You can find that in the index page. And if I scroll up into my main section, I have these news, and here are the upcoming dates right there.
Sometimes it's helpful to preview that in the browser because it becomes very easy to see at that point, and it would be these dates. So that's what you're targeting there. Next, you want to write a selector that changes the background of every second list item in the unordered list that forms the archived gallery menu in the sidebar. So again, on each page you'll find the same structure where we have this aside and we have a navigation in there that has this list in it.
You're going to target it so that every second list item uses this background. Next, you'll need to write a selector that changes the color of the main navigation links. The main navigation links are these links right up here. So what you're going to need to do is write a selector that changes the color as users hover over those links. And again, it's going to target only the main navigation links, none of the other links on the page. We also need to write a selector that targets the search input text box in the footer, and make sure that it will only target the search input form element.
So again, you can go back down to the footer, you can take a look at the search input form element, and then try to figure out how to target only that particular element on the page. And then we're going to write a selector that will add the usage text you see here at the end of the photos gallery. So the photos gallery is in the Philadelphia page. If you scroll down, you'll find that we have a div with a class of photos. Inside that we have all of our images. You'll need to write a selector here that will add this content to the end of that photos gallery, so after it.
Now that you know what your goals are, go back, carefully study the structure of HTML, and try to write the most efficient selector possible. You should be able to accomplish every single one of these without modifying the HTML at all, so you shouldn't have to apply any extra classes, any IDs, or modify the code at all; you should be able to target them just the way the code is right now. One of the most challenging things about writing CSS is that there is almost always more than one way to accomplish what you're trying to do.
So don't worry too much about matching the exact selectors that I used to complete the lab; I want you to concentrate instead on writing a selector that correctly targets the elements on the page without affecting the styling of other elements. So as you test it, make sure the other elements aren't being affected. I also want you to focus on targeting these elements with the most simple selector possible. Now once you're finished, go ahead and check out the solutions movie that follows this one to compare your work to mine.
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.