Return to Video

Using multiple CSS classes

  • 0:01 - 0:04
    We've managed to do a lot so far
    with the selectors that we've seen in CSS:
  • 0:04 - 0:08
    selecting elements by tag name,
    by ID, and by class name.
  • 0:08 - 0:11
    Let's review those for a second
    in this webpage.
  • 0:11 - 0:14
    This webpage is all about donuts.
  • 0:14 - 0:16
    And it has a heading, paragraphs--
  • 0:16 - 0:19
    and some of the paragraphs are
    short one-liner facts.
  • 0:19 - 0:23
    The CSS starts with a rule about selecting
    all of the `` tags on the page,
  • 0:23 - 0:26
    and giving them
    a font family of sans-serif.
  • 0:26 - 0:30
    I'll change this to monospace,
    so you can see everything it selects.
  • 0:31 - 0:32
    See it?
  • 0:34 - 0:38
    The next rule selects whatever element
    has an ID of `donut-header`.
  • 0:38 - 0:42
    And we know it's selecting for an ID,
    because it starts with this hash symbol.
  • 0:42 - 0:44
    Since the ID is pretty descriptive,
  • 0:44 - 0:47
    it sounds to me like it's selecting
    the big heading at the top
  • 0:47 - 0:48
    and changing its font.
  • 0:48 - 0:51
    But I'll just scroll down and confirm
    that the `` has the ID.
  • 0:51 - 0:53
    Yep, there it is.
  • 0:53 - 0:57
    The final rule selects all the elements
    that have the `fact` class name.
  • 0:57 - 1:00
    And we know it's looking for a class name,
    because it starts with a dot.
  • 1:00 - 1:03
    To figure out which elements
    have that class name,
  • 1:03 - 1:05
    I can either look at
    what the rule is doing--
  • 1:05 - 1:08
    adding a top and bottom border
    and some padding--
  • 1:08 - 1:10
    or I can look through my HTML
    for the class name.
  • 1:11 - 1:13
    I can see the class name
    is on this paragraph
  • 1:13 - 1:14
    and this paragraph--
  • 1:14 - 1:16
    the two paragraphs
    with the one-liner facts--
  • 1:16 - 1:18
    and that's why they have the border.
  • 1:18 - 1:20
    Class names are great,
  • 1:20 - 1:23
    because they let us use the same styles
    across multiple elements.
  • 1:23 - 1:25
    But there's even more
    we can do with class names,
  • 1:25 - 1:27
    and that's what I'll show you now.
  • 1:27 - 1:29
    So we have a webpage about donuts,
  • 1:29 - 1:31
    but donuts are really
    not that healthy for you.
  • 1:31 - 1:34
    They might be one of the
    least healthy things for you.
  • 1:34 - 1:37
    And they're also kind of addictive,
    because of all that sugar.
  • 1:37 - 1:40
    So if we're going to have a page
    talking about them,
  • 1:40 - 1:42
    we should probably warn people
    about their unhealthiness.
  • 1:43 - 1:47
    How about we make this top fact red,
    to really get across that it's a warning?
  • 1:48 - 1:49
    How would we do that?
  • 1:49 - 1:53
    Well, we could start with adding
    a `color` property to the `fact` CSS rule,
  • 1:53 - 1:55
    and see what that does.
  • 1:56 - 2:00
    But that made both of the facts red,
    and that second fact isn't a warning,
  • 2:00 - 2:02
    it's just a spelling thing.
  • 2:02 - 2:04
    So we don't want it to be red.
  • 2:04 - 2:06
    We could add an ID,
  • 2:06 - 2:10
    but then if we wanted to color
    other things that were warnings later
  • 2:10 - 2:11
    and add more warnings,
  • 2:11 - 2:14
    then we'd have to keep adding IDs,
    and rules for those IDs.
  • 2:14 - 2:20
    In a case like this, the best thing to do
    is to add another class to the `` tag.
  • 2:20 - 2:24
    Browsers actually let us add as many
    classes as we want to a single tag.
  • 2:25 - 2:29
    To do that, we just put our cursor
    after the last class name,
  • 2:29 - 2:33
    put a space, and then write
    a new class name, like `warning`.
  • 2:34 - 2:36
    Now we can make a rule for warning,
  • 2:37 - 2:40
    and move this color property
    into the rule.
  • 2:41 - 2:44
    And now the the top fact is red,
    and the second one isn't.
  • 2:44 - 2:45
    Beautiful.
  • 2:46 - 2:49
    We can put the class name
    on more elements, like before.
  • 2:50 - 2:54
    Maybe we want to color the
    strong text, "deep fried"--
  • 2:54 - 2:57
    we want to color that red
    because deep-fried stuff
  • 2:57 - 3:00
    are often associated with being unhealthy.
  • 3:00 - 3:04
    So we can just add
    `class ="warning"`--
  • 3:04 - 3:05
    also red.
  • 3:05 - 3:11
    Now notice that this warning here
    has that red color,
  • 3:11 - 3:15
    and it also still has the `border: top`
    and `border: bottom`.
  • 3:15 - 3:18
    And that's what happens
    when using multiple classes--
  • 3:18 - 3:21
    the browsers will look at
    all the rules that apply to them
  • 3:21 - 3:22
    and apply all the relevant styles.
  • 3:23 - 3:27
    We should be careful about using
    just colors for conveying meaning,
  • 3:27 - 3:29
    because some people are colorblind.
  • 3:29 - 3:31
    There are some people who can
    barely tell the difference
  • 3:31 - 3:34
    between red and black--
    and maybe you're one of them.
  • 3:34 - 3:37
    So let's let's change our class
    to make it more noticable for everyone.
  • 3:38 - 3:41
    We'll change this color
    to a background color,
  • 3:41 - 3:45
    because anybody can tell
    that something has a background color.
  • 3:45 - 3:48
    That's pretty low contrast,
    that red and black.
  • 3:48 - 3:51
    And contrast is also important
    to make our page readable to everyone.
  • 3:51 - 3:54
    So let's just make the color white.
  • 3:55 - 3:57
    Okay, now we have high contrast,
  • 3:57 - 4:00
    and a red background
    for people who can see red.
  • 4:00 - 4:04
    And since we used a class, both our
    warning tags have those same styles.
  • 4:05 - 4:08
    Now, thanks to the flexibility
    of CSS classes,
  • 4:08 - 4:11
    we can save the whole world from donuts.
Title:
Using multiple CSS classes
Description:

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

English subtitles

Revisions