Return to Video

Combing CSS class and element selectors

  • 0:00 - 0:04
    We're back with our webpage about donuts and the dangers of eating them.
  • 0:04 - 0:08
    I'm really liking the warning class that we added to our and our tags.
  • 0:08 - 0:13
    But I want to make this warning fact stand out even more.
  • 0:13 - 0:17
    It's got that grey border now, from the "fact" class rule.
  • 0:17 - 0:21
    But I think it'd look better with a big, thick orange border.
  • 0:21 - 0:26
    Let's try adding that to the "warning" rule.
  • 0:26 - 0:30
    So border, 5 px, solid, orange.
  • 0:30 - 0:34
    Ah, I Iove it. But it's also on the tag
  • 0:34 - 0:37
    and it doesn't look so good in line with that text there.
  • 0:37 - 0:40
    I only want it to be on the "warning" paragraph,
  • 0:40 - 0:44
    not warning text that's in-line like that.
  • 0:44 - 0:47
    How can I make a more specific CSS rule?
  • 0:47 - 0:51
    Well, one approach is to make a whole new class-- "warning-paragraph"--
  • 0:51 - 0:55
    and move the properties there. But we don't have to do that.
  • 0:55 - 1:01
    Instead, we can tell the browser to only apply the border properties to paragraph tags that have the warning,
  • 1:01 - 1:05
    but no other tags that have the warning class.
  • 1:05 - 1:09
    To make that rule, we first write the element we're looking for-- "p"--
  • 1:09 - 1:14
    then we write a dot, and the class name-- "warning".
  • 1:14 - 1:19
    That tells the browser to find all tags that have the class name "warning"
  • 1:19 - 1:21
    and apply the properties inside.
  • 1:21 - 1:26
    So let's paste this border property in here...
  • 1:26 - 1:34
    And voila! We've got the border only on the fact and not on the .
  • 1:34 - 1:38
    We can do that with any combination of elements and class names,
  • 1:38 - 1:43
    whenever we want the browser to only style particular tags that have a class.
  • 1:43 - 1:45
    Make sure you follow exactly this sequence:
  • 1:45 - 1:51
    the tag name, the dot, and then the class name.
  • 1:51 - 1:56
    If you put a space accidentally between the tag name and the dot, it will not work anymore.
  • 1:56 - 2:00
    That's because a space means something else in CSS.
  • 2:00 - 2:03
    And the browser interprets that rule differently.
  • 2:03 - 2:07
    Stay tuned to find out about that oh-so-special space.
Title:
Combing CSS class and element selectors
Description:

{'type': u'plain'}

more » « less
Video Language:
English
Duration:
02:08

English subtitles

Revisions