Return to Video

Grouping CSS selectors

  • 0:01 - 0:05
    I love art museums. But I don't have time to go to art museums every day.
  • 0:05 - 0:08
    So thankfully, I can find beautiful paintings on the Internet.
  • 0:08 - 0:11
    Like even here on Khan Academy, in our art history section.
  • 0:11 - 0:14
    So I made this webpage with lists of famous paintings,
  • 0:14 - 0:21
    with one heading and lists for each art style, and links to articles about each of the paintings.
  • 0:21 - 0:25
    To make this webpage look a bit fancier, you know, artsy
  • 0:25 - 0:31
    I've applied CSS rules to change the font family of both headings-- the s and the s.
  • 0:31 - 0:39
    I like the cursive, but I think that 'fantasy' might look better, so let me change that.
  • 0:39 - 0:44
    Oh, and I have to change it here, because we have two rules.
  • 0:44 - 0:49
    But really, I always want all my headings on this page to have the same font family
  • 0:49 - 0:54
    and I don't want to have to keep changing the font family in two places every time I change my mind.
  • 0:54 - 1:00
    What is the solution here? To turn our two CSS rules into one CSS rule.
  • 1:00 - 1:04
    Hm, just think about that for a bit.
  • 1:04 - 1:08
    You might suggest adding the same class to our s and s.
  • 1:08 - 1:12
    And that would definitely work, and we could just have one CSS rule for that class.
  • 1:12 - 1:15
    But it ultimately would be more work on our end
  • 1:15 - 1:19
    because we'd have to remember to add that class every time we made an or .
  • 1:19 - 1:25
    Fortunately, there is a better way. We can group our selectors together using commas.
  • 1:25 - 1:32
    We'll just add a comma after this "h1" here, then write "h2".
  • 1:32 - 1:37
    And now we can delete this other rule because we merged it into the first one.
  • 1:37 - 1:40
    And tada! Our web page looks the same.
  • 1:40 - 1:44
    Now when I want to experiment with changing the font family, I can do it in one place.
  • 1:44 - 1:49
    Here, I will change it back to cursive, and now they're all cursive.
  • 1:49 - 1:56
    Take careful note of this comma. The selectors have to be comma-separated, not space-separated.
  • 1:56 - 2:03
    As we've seen, a space is used for descendant selectors, and means something entirely different to CSS.
  • 2:03 - 2:09
    Grouping selectors can be a great tool to reduce the number of redundant selectors you have.
  • 2:09 - 2:14
    But don't overuse it. You shouldn't group two selectors just because they happen to have the same properties now.
  • 2:14 - 2:18
    You should group two selectors because you always want them to have the same properties.
  • 2:18 - 2:22
    Usually because they're semantically very similar to each other.
  • 2:22 - 2:28
    In this case, my selector is for all heading types, which I often want to share the same styles.
  • 2:28 - 2:33
    Stay tuned for one more common use case for group selectors.
Title:
Grouping CSS selectors
Description:

{'type': u'plain'}

more » « less
Video Language:
English
Duration:
02:34
Retired user edited English subtitles for Grouping CSS selectors

English subtitles

Revisions