Return to Video

CSS Selecting by id

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

German subtitles

Incomplete

Revisions