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

Targeting page content: Solution

From: CSS: Core Concepts

Video: Targeting page content: Solution

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.

Targeting page content: Solution

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.

Show transcript

This video is part of

Image for CSS: Core Concepts
CSS: Core Concepts

81 video lessons · 40859 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
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Core Concepts.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.