New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Targeting page content: Lab

From: CSS: Core Concepts

Video: Targeting page content: Lab

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.

Targeting page content: Lab

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.

Show transcript

This video is part of

Image for CSS: Core Concepts
CSS: Core Concepts

81 video lessons · 39381 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 4m 57s
    1. Welcome
      55s
    2. Using the exercise files
      4m 2s
  2. 1h 7m
    1. Exploring default styling
      4m 56s
    2. CSS authoring tools
      2m 29s
    3. CSS syntax
      4m 45s
    4. Writing a selector
      4m 10s
    5. Setting properties
      8m 40s
    6. Common units of measurement
      7m 47s
    7. Inline styles
      5m 1s
    8. Embedded styles
      5m 19s
    9. Using external style sheets
      10m 34s
    10. Checking for browser support
      8m 48s
    11. Dealing with browser inconsistencies
      5m 30s
  3. 2h 15m
    1. Structuring HTML correctly
      2m 51s
    2. Element selectors
      4m 52s
    3. Class selectors
      6m 4s
    4. ID selectors
      3m 27s
    5. Using classes and IDs
      10m 7s
    6. Element-specific selectors
      4m 35s
    7. The universal selector
      5m 42s
    8. Grouping selectors
      4m 49s
    9. Descendent selectors
      7m 32s
    10. Child selectors
      5m 7s
    11. Adjacent selectors
      5m 30s
    12. Attribute selectors
      12m 43s
    13. Pseudo-class selectors
      3m 54s
    14. Dynamic pseudo-class selectors
      8m 29s
    15. Structural pseudo-class selectors
      6m 45s
    16. Nth-child selectors
      13m 10s
    17. Pseudo-element selectors
      12m 40s
    18. Targeting page content: Lab
      8m 56s
    19. Targeting page content: Solution
      7m 59s
  4. 42m 39s
    1. What happens when styles conflict?
      4m 0s
    2. Understanding the cascade
      5m 47s
    3. Using inheritance
      6m 11s
    4. Selector specificity
      6m 55s
    5. The !important declaration
      4m 5s
    6. Reducing conflicts through planning
      3m 33s
    7. Resolving conflicts: Lab
      6m 45s
    8. Resolving conflicts: Solution
      5m 23s
  5. 1h 47m
    1. Setting a font family
      7m 10s
    2. Using @font-face
      9m 18s
    3. Setting font size
      7m 35s
    4. Font style and font weight
      6m 52s
    5. Transforming text
      3m 58s
    6. Using text variants
      2m 49s
    7. Text decoration options
      4m 26s
    8. Setting text color
      3m 2s
    9. Writing font shorthand notation
      8m 49s
    10. Controlling text alignment
      6m 33s
    11. Letter and word spacing
      9m 11s
    12. Indenting text
      4m 30s
    13. Adjusting paragraph line height
      10m 30s
    14. Controlling the space between elements
      6m 41s
    15. Basic text formatting: Lab
      8m 45s
    16. Basic text formatting: Solution
      7m 14s
  6. 2h 1m
    1. Understanding the box model
      16m 53s
    2. Controlling element spacing
      14m 29s
    3. Controlling interior spacing
      10m 49s
    4. Margin and padding shorthand notation
      6m 27s
    5. Adding borders
      8m 57s
    6. Defining element size
      10m 7s
    7. Creating rounded corners
      6m 58s
    8. Background properties
      2m 51s
    9. Using background images
      5m 10s
    10. Controlling image positioning
      10m 25s
    11. Using multiple backgrounds
      7m 5s
    12. Background shorthand notation
      5m 25s
    13. Styling container elements: Lab
      7m 55s
    14. Styling container elements: Solution
      8m 17s
  7. 47m 51s
    1. Color keyword definitions
      5m 4s
    2. Understanding hexadecimal notation
      6m 5s
    3. Using RGB values
      4m 58s
    4. Using HSL values
      5m 17s
    5. Working with opacity
      2m 23s
    6. Using RGBa and HSLa
      3m 8s
    7. Styling drop shadows
      5m 38s
    8. CSS gradients
      6m 32s
    9. Working with color: Lab
      4m 26s
    10. Working with color: Solution
      4m 20s
  8. 1m 58s
    1. Additional resources
      1m 58s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.