New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

ID selector essentials

From: CSS Web Site Design

Video: ID selector essentials

In this chapter we're going to be covering selectors in some detail, this is because learning about selectors is really the key to writing really good CSS. So, what we're going to start with is ID selectors. Now, in the markup here, there are a few ids, there's a div ID of masthead, right after it there's an a element with an ID of homelink and then after that is an ol with an ID of navlinks. There is also a paragraph with an ID of today and a div with and ID of content.

ID selector essentials

In this chapter we're going to be covering selectors in some detail, this is because learning about selectors is really the key to writing really good CSS. So, what we're going to start with is ID selectors. Now, in the markup here, there are a few ids, there's a div ID of masthead, right after it there's an a element with an ID of homelink and then after that is an ol with an ID of navlinks. There is also a paragraph with an ID of today and a div with and ID of content.

IDs are assigned in the markup as you can see here. You can't just make up IDs in your CSS and expect the browser to know what to do with those unless there are corresponding IDs in the XHTML markup. Now there is a special rule about IDs, you can only have one instance of a given ID in the document. So here I have a div with an ID of content that is the only element in this entire document that can have an ID of content. Obviously we can have IDs on many different elements in fact, we could, if we were feeling particularly strange, put an ID on every single element as long as every single one of those IDs was unique, that is a requirement, that IDs have uniqueness within a given document.

The way that you select IDs is like this. You use the octothorpe character, that's the little tic-tac-toe grid or # sign or hash mark. So you have that and then immediately follow the octothorpe with the name of the ID, so, since we're trying to select the element with an ID of masthead, we just say octothorpe masthead and then we're going to give it a background of let's say green, and if we go over to our web browser and take a look, hit Reload, there's the green.

What this selector is saying is select any element whose ID attribute has a value of masthead and then make the background of it green. So let's throw in a couple of more ID selectors here, we'll do a silver background for the navlinks and for the sidebar of the document which has an ID strange enough of sidebar, put a border around it. So if we go back to Firefox, and hit Reload there you can see the borders come in, the silver background is coming on the navigation links.

Basically we're just selecting these things by the IDs that are associated with the elements. So as you might started to gather just from these ID names, IDs are commonly used to mark sections of affectively a layout or at least sections of a document. So the masthead of the document, the first part that contains things like the name of the site and navigation links and what today's date is, that's all enclosed in an element with an ID of masthead, in this case it's a div with an ID of masthead and that's often the case too.

You'll often see IDs that say things like sidebar, or navlinks, or navigation, or those sorts of pieces of the document are what tend to be IDed. Now, you're not restricted to just doing IDs you can also put in an element selector. So in this case, what we're saying is any ol element, which has an ID of navlinks, or any p element, which has an ID of sidebar. So if we save that and we go back and we hit Reload, there's no change to the navlinks, but the sidebar lost its border, and that's because what we said back here in our code was any paragraph with an ID of sidebar should get a one pixel solid border. Well, the sidebar isn't a paragraph, it's a div.

So, what we did is we created a selection that doesn't apply to the sidebar in this document. If we wanted to include the element name, we'd need to say div#sidebar, or we can do what we did before and just say sidebar, like so. There is one more thing to watch out for. You might accidentally do this at some point, put a space in between let's say an element name of like ol and an ID name like navlinks.

And if you do that, that completely changes the meaning of the selector in question. If we go back to our Web browser and hit Reload, you can see the silver goes completely away. That's because what we've really done there is, we've changed this so that we've created a descendent selector, which we will discuss in a later video in this chapter. But this doesn't mean any ol element with an ID of navlinks any more, just because of that single space. If we get rid of the space then, everything's back to normal.

And the last thing to note about ID names is that they are case-sensitive. Here I've changed the name of navlinks so that has capital N, capital L and down here in my markup navlinks is all lowercase. If we switch over and hit Reload there is still no silver background and that's because navlinks with the capital N and L, does not match navlinks all lowercase. They are as different as if we had called them night and day. So, if I want this to apply here, I either need to change the N and the L to be uppercase in the XHTML or I need to change navlinks here to be all lowercase in the CSS. As long as the capitalization is consistent then everything's fine.

It doesn't really matter what capitalization you use. If you want to use the sort of capitalization that I was just showing, where each sort of subword has its first letter capitalized, or if you want to go all lowercase, or you want to make all of your ID names all uppercase, that's really up to you as long as you are consistent. There were some older browsers, notably earlier versions of Internet Explorer for Windows that didn't pay attention to this capitalization and that led to some sloppiness in some peoples authoring styles, where they would just mix case and IDs.

Do not do that, be consistent and with the capitalization between your CSS and your XHTML for the most obvious reason, that in most browsers today and if you're not consistent in your capitalization, then your styles aren't going to get applied to the document. So, that covers basic ID selectors. In the next video we'll talk about somewhat related concept, Class selectors.

Show transcript

This video is part of

Image for CSS Web Site Design
CSS Web Site Design

73 video lessons · 60738 viewers

Eric Meyer
Author

 
Expand all | Collapse all
  1. 14m 34s
    1. Welcome
      28s
    2. What is CSS?
      5m 34s
    3. Design tour
      2m 38s
    4. Using the exercise files
      1m 20s
    5. Installing the Web Developer toolbar
      4m 34s
  2. 25m 56s
    1. XHTML essentials
      3m 55s
    2. CSS essentials
      5m 17s
    3. Embedded style sheets
      2m 20s
    4. Linking a style sheet
      2m 19s
    5. Linking to multiple style sheets
      2m 20s
    6. Using linked and embedded style sheets together
      4m 21s
    7. Using imported style sheets
      5m 24s
  3. 57m 48s
    1. ID selector essentials
      6m 38s
    2. Class selector essentials
      4m 9s
    3. Best practices for classes
      4m 52s
    4. Grouped selection
      4m 2s
    5. Descendant selectors
      6m 44s
    6. The sources of style
      6m 38s
    7. Specificity
      8m 21s
    8. Making things important
      4m 32s
    9. Inheritance essentials
      5m 12s
    10. Making things really unstyled
      4m 2s
    11. User style sheets
      2m 38s
  4. 39m 3s
    1. Box model essentials
      7m 35s
    2. Simple floating
      5m 3s
    3. Using float for layout
      5m 5s
    4. Fixing column drop
      5m 35s
    5. Clearing essentials
      4m 20s
    6. Float containment
      6m 35s
    7. Creating a navbar from a list
      4m 50s
  5. 38m 3s
    1. Coloring text
      4m 13s
    2. Defining color in CSS
      8m 12s
    3. Coloring backgrounds
      6m 35s
    4. Applying background images
      4m 19s
    5. Manipulating the direction of background images
      2m 52s
    6. Positioning backgrounds
      7m 23s
    7. Background shorthand
      4m 29s
  6. 58m 28s
    1. Altering line height
      7m 32s
    2. Font style and weight
      5m 45s
    3. Sizing fonts
      9m 59s
    4. Using font families
      10m 38s
    5. Font shorthand
      6m 5s
    6. Justifying text
      4m 56s
    7. Vertically aligning text
      4m 22s
    8. Transforming text
      3m 49s
    9. Text decoration
      5m 22s
  7. 44m 40s
    1. Margin essentials
      14m 21s
    2. Adding borders
      6m 52s
    3. Padding
      9m 17s
    4. Using negative margins
      7m 19s
    5. Margin collapsing
      6m 51s
  8. 20m 38s
    1. Styling tables and captions
      5m 23s
    2. Styling table cells
      6m 30s
    3. Styling a column with classes
      4m 51s
    4. Styling links inside table cells
      3m 54s
  9. 30m 40s
    1. Styling for specific mediums
      4m 3s
    2. Creating a print style sheet
      6m 35s
    3. Hiding layout for print
      4m 11s
    4. Styling for print
      6m 34s
    5. Complex styling for print
      4m 37s
    6. Creating a footer
      4m 40s
  10. 37m 23s
    1. Getting started
      1m 11s
    2. Setting global styles
      4m 4s
    3. Defining masthead and navbar colors
      3m 27s
    4. Layout of the navlink bar
      3m 38s
    5. Using columns
      4m 49s
    6. Setting content styles
      1m 53s
    7. Creating the sidebar boxes
      5m 42s
    8. Creating the sidebar form
      3m 23s
    9. Completing the sidebar
      3m 29s
    10. Making a table
      3m 12s
    11. Creating a footer
      2m 35s
  11. 1m 28s
    1. Goodbye
      1m 28s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.