Return to Video

CSS descendant selectors

  • 0:01 - 0:05
    Let's move on from donuts to carrots, one of the healthiest foods in the world.
  • 0:05 - 0:08
    Plus the favorite food of rabbits.
  • 0:08 - 0:11
    Did you know that carrots aren't just orange?
  • 0:11 - 0:14
    They were actually originally purple. Some of them still are today.
  • 0:14 - 0:20
    We have this short webpage here with a list of carrot colors and a sentence about their purple origins.
  • 0:20 - 0:25
    And we've used classes to style the color names to the appropriate color.
  • 0:25 - 0:32
    I like how the styles look in the list, but I'm not sure I like how the "purple" style looks in the text.
  • 0:32 - 0:36
    I think I'd rather not use a background color, and go for more subtle coloring there.
  • 0:36 - 0:44
    How could I tell the browser to style the purple text here differently than the purple text here?
  • 0:44 - 0:49
    Well, they have the same class names, so we can't use class unless we introduce a new class name.
  • 0:49 - 0:53
    They also have the same tag name-- they're both s.
  • 0:53 - 0:58
    So we can't use the element-plus-class-name selector that we just learned.
  • 0:58 - 1:01
    Is there anything else different about them?
  • 1:01 - 1:06
    Well, one thing is that this is inside an ,
  • 1:06 - 1:11
    whereas this is inside a .
  • 1:11 - 1:16
    So the difference is their parent tags-- the tags that are above them.
  • 1:16 - 1:21
    We can use this information to make a CSS rule, using what's called a "descendant selector"--
  • 1:21 - 1:25
    a way of selecting elements based on their position in the document.
  • 1:25 - 1:31
    For example, to select only the purple inside the paragraph
  • 1:31 - 1:36
    we'll write "p", and then a space-- the space is really important--
  • 1:36 - 1:40
    and then the class name-- dot purple--
  • 1:40 - 1:46
    and now we'll add our properties that will give us the more subtle coloring--
  • 1:46 - 1:50
    background-color, transparent, override what it was before.
  • 1:50 - 1:59
    Okay, nice. So we've affected this purple text without also affecting this purple text.
  • 1:59 - 2:03
    And now any time we use the purple class inside a paragraph like this,
  • 2:03 - 2:05
    it'll get those styles applied after.
  • 2:05 - 2:10
    We can use that space to dig deep into our document.
  • 2:10 - 2:16
    Let's say we want to apply a styling just to tags that are in s.
  • 2:16 - 2:23
    We start with the parent tag-- -- and then the space, and then .
  • 2:23 - 2:28
    And maybe we'll give them a different line height to space them out more.
  • 2:28 - 2:34
    Nice. We can even add a in front of the if we wanted,
  • 2:34 - 2:39
    to make sure it didn't apply to s inside an .
  • 2:39 - 2:44
    You see, to use descendant selectors, we need to think hard about the structure of our document
  • 2:44 - 2:46
    And what tags are inside other tags.
  • 2:46 - 2:50
    If you're indenting nicely, then that should be easy to do
  • 2:50 - 2:53
    because your indentation will reflect the hierarchy of tags.
  • 2:53 - 2:57
    See, we have this , and indented inside that we have an ,
  • 2:57 - 2:59
    and inside that we have the .
  • 2:59 - 3:03
    If you're not indenting nicely, well, fix that up now
  • 3:03 - 3:07
    because it'll make it much easier for you to understand the structure of your page
  • 3:07 - 3:11
    and to come up with CSS selectors based on that structure.
  • 3:11 - 3:14
    You can use that space between any kinds of selectors.
  • 3:14 - 3:19
    Like finding a particular tag type under an element that has a certain id,
  • 3:19 - 3:22
    or finding a particular id under elements with a particular class name,
  • 3:22 - 3:25
    any combination that you need to use.
  • 3:25 - 3:32
    The thing to remember is that if the structure of your page changes, then your old CSS rules might not apply.
  • 3:32 - 3:37
    So think carefully when you use them, and how future-proof your CSS will be.
  • 3:37 - 3:41
    You could always use classes if you want to be less dependent on the structure of your page.
  • 3:41 - 3:46
    Let's look back at the rules I created and think through them.
  • 3:46 - 3:51
    This first rule styles all the purple class elements inside paragraphs a certain way.
  • 3:51 - 3:58
    If I add in new paragraphs with purple class elements in the future, would I want them to get that property?
  • 3:58 - 4:02
    Yes, because I think that those properties will always look the best in the paragraph.
  • 4:02 - 4:04
    Do I need a more specific rule?
  • 4:04 - 4:11
    Well, maybe if these paragraph were always inside some element with class name "article", I could add that to the rule.
  • 4:11 - 4:14
    But for now, this is the most specific I can be.
  • 4:14 - 4:21
    The second rule gives all the elements inside list items a certain line height.
  • 4:21 - 4:26
    Do I always want strong items inside s to have that increased line height?
  • 4:26 - 4:31
    Hm, maybe not. This rule might be too generic.
  • 4:31 - 4:37
    Since I'm not sure, I will add a class to this
  • 4:37 - 4:42
    and then change this one so it's "ul.spacey"
  • 4:42 - 4:45
    which makes it much more specific.
  • 4:45 - 4:50
    In the future, as my webpage grows, I might make the rule more or less specific.
  • 4:50 - 4:54
    Stick this tool in your ever-growing CSS toolbox
  • 4:54 - 4:58
    and practice it, so you can use it when it makes sense.
Title:
CSS descendant selectors
Description:

{'type': u'plain'}

more » « less
Video Language:
English
Duration:
04:58

English subtitles

Revisions