Return to Video

CSS dynamic pseudo classes

  • 0:01 - 0:05
    We're back with the webpage that
    links to lists of famous paintings.
  • 0:05 - 0:12
    The CSS starts with a rule that sets a font family
    for everything in the body to sans-serif.
  • 0:12 - 0:17
    Then a rule for group selectors that
    changes the font family of the s and s.
  • 0:17 - 0:25
    And then we have a bunch of rules that you probably
    haven't seen before and might look a bit funny.
  • 0:25 - 0:30
    They all start with "a", and then a colon,
    and then a word.
  • 0:30 - 0:36
    What does that colon mean?
    What could those rules be selecting for?
  • 0:36 - 0:41
    Well, all of those things that start with
    a colon are called pseudo-classes.
  • 0:41 - 0:48
    A pseudo-class is used to select elements based on information
    that's not really part of the webpage structure
  • 0:48 - 0:52
    or just can't be expressed using
    other selectors.
  • 0:52 - 0:57
    In this page, I'm using pseudo-classes to
    style the links based on their state.
  • 0:57 - 1:03
    The 'link' pseudo-class will select all the links
    on the page that the user has not visited yet.
  • 1:03 - 1:07
    Which most browsers default to blue.
  • 1:07 - 1:10
    The 'visited' pseudo-class will select
    all the links that the user has visited.
  • 1:10 - 1:13
    Which most browsers default to purple.
  • 1:13 - 1:16
    If we really want, we can
    change the colors.
  • 1:16 - 1:18
    But you should have a good reason
    for doing that.
  • 1:18 - 1:23
    People get used to associating blue and purple
    with things they have and haven't seen.
  • 1:23 - 1:26
    And people like knowing
    which pages they've visited.
  • 1:26 - 1:28
    So you should not take that away
    without a good reason.
  • 1:28 - 1:31
    So, I'm just going to delete these two rules.
  • 1:31 - 1:34
    Because I don't think it's personally
    a good idea to be messing with them.
  • 1:34 - 1:39
    The next rule is a fun one though: 'a:hover'.
  • 1:39 - 1:46
    The 'hover' pseudo-class will select an element
    as long as the user is currently mousing over it.
  • 1:46 - 1:48
    So the properties will only get applied then.
  • 1:48 - 1:53
    Try pausing this talk-through now
    and hover over the links to see what happens.
  • 1:53 - 1:57
    Go ahead, I'll wait. Hover hover
    hover hover...
  • 1:57 - 2:01
    Did you see that background change color?
    It's a pretty cool effect.
  • 2:01 - 2:05
    And you can actually use that 'hover' pseudo-class
    on any element, not just links.
  • 2:05 - 2:08
    Just remember that it won't work
    for all devices.
  • 2:08 - 2:11
    Like if you're on a phone, you don't have a way of hovering.
    You just have touch or no touch.
  • 2:11 - 2:15
    It's great to have a hover effect as a bonus, but don't rely on it.
  • 2:15 - 2:21
    What about these last two here?
    They're similar to 'hover'.
  • 2:21 - 2:23
    They depend on what the user is currently doing.
  • 2:23 - 2:27
    The 'active' pseudo-class selects elements
    that are currently being activated.
  • 2:27 - 2:34
    Like for a link, if the user is currently pressing down on the link,
    right before they actually change pages.
  • 2:34 - 2:39
    The 'focus' pseudo-class selects elements
    that currently have the focus
  • 2:39 - 2:42
    which usually happens if you use your tab key
    to tab around an interface.
  • 2:42 - 2:48
    Pause the talk-through now and try pressing down on the links
    and tabbing around them, and see what happens.
  • 2:48 - 2:51
    Did you see the background
    change color when you did that?
  • 2:51 - 2:56
    I chose the same property for 'hover', 'active', and 'focus',
    because they're all sort of the same thing.
  • 2:56 - 2:59
    The user is targeting the link somehow.
  • 2:59 - 3:04
    Many web designers use the same properties
    across all three states for that reason.
  • 3:04 - 3:07
    What if I decided I want to change
    the color of that background?
  • 3:07 - 3:14
    Well, I can either go into each of them
    and change each one of them
  • 3:14 - 3:16
    Or I could do what we just learned.
  • 3:16 - 3:20
    I can group the selectors by putting them
    all into one rule, separated by commas.
  • 3:20 - 3:27
    So we'll just put our comma after here,
    say "a:active", then "a:focus".
  • 3:27 - 3:37
    Now I can delete these two and change
    all three of those at once. Nice.
  • 3:37 - 3:42
    These are the first pseudo-classes we've
    shown you here, but they're not the only ones.
  • 3:42 - 3:45
    You can search for CSS pseudo-classes
    on the Internet to find out about others
  • 3:45 - 3:49
    or wait for us to teach more here.
Title:
CSS dynamic pseudo classes
Description:

more » « less
Video Language:
English
Duration:
03:49

English subtitles

Revisions