Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie, I want to talk about a more precise way to target elements on the page and that's through the use of class selectors. Class selectors allow you to target any element on the page that has the same class attribute. Let's take a look at the syntax and how it works. So I have the class-selector.htm file open from the Chapter_02/02_03 folder. So, if we take a look at the structure of the page, it's very similar to what we had last time, only this time instead of one aside, notice that we have two asides.
So if I preview this on my page, I can see that we have some default styling, and I can see that both of those asides have this sort of black border around it. And maybe I have a situation coming up where I wanted the border color for each of the asides to be a little bit different. Well, using element selectors, like we did in the last movie, there's no real way to do that, so let's take a look at how classes will help us do that. So I am back in my code, and the first thing I want to do is apply a class attribute because class selectors and class attributes work sort of hand in hand.
So right after the first aside tag, inside the actual opening tag, I am going to go ahead and apply a class attribute, and I want that class attribute to be aside1. And then for the second one, I am going to go down and type in a class attribute of aside2, but you probably saw that coming. Now not very descriptive, not very semantic, but for what we're doing in this exercise, we just want to make sure we use those two class attributes. So now that it I have those class attributes on my page, I can target them with a class selector.
So I am going to scroll up to my styles and just in this little section right here where it says, "add styles here," I am going to add a couple of selectors to allow me to do just that. Now whenever you are writing a class selector, unlike an element selector where you would just write in, say, the word aside1, it doesn't work that way; you have to let the browser know that you are wanting to target a class on the page and you do that by typing in first a period. So you type in a period before the name of the class, so that's how we identify classes. In this case, we have the aside1, which Aptana tried to help me out with, but I sort of ignored it.
And inside of the declaration for aside1, I am going to type in "border: 1px" for one pixel space, "solid" so that we want a solid border, space "green," the green color. We are going to do the same thing for our second aside, so ".aside2 border: 1px solid" and again we are just going to change the color, "red." So if I save this, I go back into my browser, refresh this, you can see that now one aside has a green border, the second aside has a red border.
Now this is very exciting and for most graphic designers, they are like cool, cool, cool! I can finally target things the way that I want to on the page, and then they just start using class selectors a lot. Class selectors are not always the most efficient way to do something, and let me demonstrate that by going back to our code. So, let's say we want to target some of our headings a little bit differently. So I'm going to scroll back down again into the structure of the page itself, and the first heading, h1, here, I am just going to go ahead and give it a class="blueHeading". I am going to do the same thing for the h2, so I am just going to give it a class of blueHeading.
Now, what this tells us is that when we have a class attribute, we can apply it as many times on the page as we want. So if you need to use it one time, that's fine; if you need to use it fifty times on the page, that's fine too. I am going to go down to the h2 here that's inside my first aside and I am going to go ahead and give that a class of greenHeading, and then I am going to go down to the second h2, which in the second aside, and I am going to go ahead and give that a class of redHeading. I bet you can see where all this is heading--no pun intended.
I am going to go back up to my styles and now I am going to write selectors for this. So remember, when we write a class selector, we start with a period, so .blueHeading. And indeed I am just going to change its color to blue, so we are just going to use a keyword there. And I will go down and do greenHeading and then change its color to green, or set its color to green I should say. And I'll do redHeading, set its color to red. I am going to save that and let me go back my browser, refresh it, and now my headings are the colors that I want: blue for the first two and then green and red for the second two.
Now, looking at this code, you might say, well, okay, so what's wrong with that? Well, think about this from a maintenance standpoint. If every time you want to change something, or apply a styling, you go crazy with classes-- and actually Jeffrey Zeldman coined a term for this; he called it classitis. Do you know what classitis is? Classes all over the place. And really, we are not supposed to be using class attributes unless they actually mean something. Unfortunately, none of these really mean anything. blueHeading on the surface doesn't describe this heading. It doesn't tell us anything about the heading, in terms of its content; it just tells us what the heading is supposed to look like.
aside1 and aside2, they don't tell us anything about these aside elements; they just tell us this is the first aside, this is the second aside. So semantically, our classes aren't all that great right now. So instead of rushing to use classes, you need to really carefully consider how to best utilize them. Now I have seen designers really too often rely on them for almost every single styling of elements on the page, and that's the wrong way to do it, because if you use too many class attributes, it can be really inefficient in terms of going back and modifying things. Imagine having go through your entire site and find every instance of greenHeading and change that to say yellowHeading.
That would be really, really inefficient. So, in a moment I am going to discuss the strategy behind using classes and class selectors in a little bit more detail. but before we get to talking about strategy, we have to talk about one more selector type and that's the ID selector, and we are going to do that in our next movie.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.