Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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.
Get unlimited access to all courses for just $25/month.Become a member