Return to Video

CSS Selecting by id

  • 0:00 - 0:02
    In this webpage, we're using CSS
  • 0:02 - 0:05
    to style our ``s and paragraphs
  • 0:05 - 0:09
    so that all the ``s are green
    and all the paragraphs are purple.
  • 0:10 - 0:13
    But what if we wanted to select
    just the first ``
  • 0:13 - 0:16
    or style just the second paragraph?
  • 0:17 - 0:20
    We'd need to come up with a way
    to tell the browser exactly which
  • 0:20 - 0:22
    of the elements we're selecting
  • 0:22 - 0:25
    so that it didn't apply the styles
    to all of them like it is now.
  • 0:25 - 0:28
    One way to do that is to select by ID.
  • 0:28 - 0:31
    We can give a tag in our page
    a unique ID
  • 0:31 - 0:32
    and then we can tell CSS,
  • 0:32 - 0:36
    "Listen here: I only want to apply
    these styles to the element with this ID,
  • 0:36 - 0:38
    not to any of the other elements."
  • 0:38 - 0:41
    To do that, the first step is
    actually modifying the HTML
  • 0:41 - 0:45
    to add id attributes to the tags
    we want to specifically select.
  • 0:46 - 0:48
    Let's start with
    the second paragraph here.
  • 0:49 - 0:51
    To add an id attribute,
    we put our cursor
  • 0:51 - 0:53
    in the start `` tag
    right after the p,
  • 0:53 - 0:58
    then add a space, and then type
    `id = "`
  • 0:59 - 1:03
    Now we need to fill in this
    id attribute with a value.
  • 1:03 - 1:04
    What ID should I give it?
  • 1:04 - 1:06
    Well, it should be
    descripitive and unique.
  • 1:07 - 1:09
    Nothing else on this page should
    ever have the same ID.
  • 1:09 - 1:15
    Well since this is a song about rabbits,
    I'll call it rabbits-song.
  • 1:15 - 1:20
    We can't have spaces in IDs, so if
    they have multiple words like this one
  • 1:20 - 1:22
    you should always use
    hyphens or underscores.
  • 1:23 - 1:25
    I really like hypens, myself.
  • 1:26 - 1:29
    Now we have a way of identifying
    this paragraph uniquely.
  • 1:29 - 1:32
    So we can add a CSS rule targeting it.
  • 1:32 - 1:35
    Let's go back up to our `` tag
    for the second step.
  • 1:36 - 1:38
    We'll add a new line,
    after the last rule there.
  • 1:39 - 1:43
    Now remember, the first part
    of a CSS rule is the selector:
  • 1:43 - 1:46
    the part that tells the browser
    what to select.
  • 1:46 - 1:51
    In our previous rules up here,
    we used selectors like `` and ``
  • 1:51 - 1:54
    to select all the ``s and ``s
    on the page.
  • 1:55 - 1:57
    Now to make a selector
    that only selects elements
  • 1:57 - 1:59
    with a particular ID,
  • 1:59 - 2:01
    we must start the selector
    with a hash sign.
  • 2:01 - 2:05
    That tells the browser that
    whatever is coming next is an ID.
  • 2:05 - 2:09
    Now we write our ID:
    rabbits-song.
  • 2:10 - 2:12
    The rest of the rule
    is the same as before.
  • 2:12 - 2:14
    We open and close our curly braces,
  • 2:14 - 2:18
    we add a property,
    like background-color...
  • 2:18 - 2:21
    ...and ta-da! It worked!
  • 2:21 - 2:24
    Only the song paragraph has
    the yellow background color,
  • 2:24 - 2:26
    and the first paragraph
    stayed the same.
  • 2:27 - 2:31
    Let's do this again,
    for selecting that first ``.
  • 2:31 - 2:33
    Can you remember the first step?
  • 2:34 - 2:37
    That's right. We need to
    actually modify this HTML
  • 2:37 - 2:38
    to add the `id` attribute.
  • 2:38 - 2:41
    So we stick our cursor
    in the start tag,
  • 2:41 - 2:44
    add a space, type
    `id =`
  • 2:44 - 2:47
    and then type a very unique
    and descriptive ID.
  • 2:47 - 2:50
    So, "rabbits-info-heading".
  • 2:51 - 2:54
    Okay, the second step.
    Back up in our style tag
  • 2:54 - 2:57
    we add a new line,
    write the hash sign,
  • 2:57 - 3:02
    then our ID,
    rabbits-info-heading
  • 3:02 - 3:05
    and then put our properties
    inside curly braces {
  • 3:05 - 3:09
    background-color: purple;
    }
  • 3:09 - 3:13
    Uh-oh! Okay, it didn't work.
    Umm... do you see what went wrong?
  • 3:13 - 3:15
    Did I... spell it the same way?
  • 3:15 - 3:18
    rabbits-info-Heading,
    rabbits-info-heading...
  • 3:18 - 3:21
    Hmm... so they look pretty much the same.
  • 3:21 - 3:23
    Now I could compare them
    letter-by-letter
  • 3:23 - 3:25
    to figure out what's wrong,
  • 3:25 - 3:30
    but what I like to do is just go down
    and select the ID in the HTML,
  • 3:30 - 3:34
    and copy it, and then paste it in here
    to make sure it's exactly the same.
  • 3:34 - 3:35
    And... it worked!
  • 3:36 - 3:40
    My `` has a background.
    And did you noticed what changed?
  • 3:40 - 3:44
    Maybe you did. It was the h here.
    The h used to be a capital H,
  • 3:44 - 3:47
    which the browser
    does not consider the same.
  • 3:47 - 3:50
    It has to be that lower h
    to match the HTML.
  • 3:50 - 3:53
    That's because id's are case-sensitive.
  • 3:53 - 3:57
    So you have to both spell them
    and case them the same way everywhere.
  • 3:58 - 4:02
    I find it's best to just always use
    lowercase for every letter in my IDs
  • 4:02 - 4:05
    so I don't have to think about
    what casing I used when.
  • 4:05 - 4:08
    Okay, now let me leave you with
    one last warning:
  • 4:08 - 4:11
    IDs must be unique!
  • 4:11 - 4:13
    If you have two tags on your page
    with the same exact ID,
  • 4:13 - 4:18
    the browser might style both of them,
    but it also might style only one of them.
  • 4:18 - 4:20
    And you don't want to leave that
    up to chance.
  • 4:20 - 4:23
    Nice, unique, descriptive IDs.
Title:
CSS Selecting by id
Video Language:
English
Duration:
04:25

English subtitles

Revisions