Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
In this hands-on course, James Williamson demonstrates the concepts that form the foundation of Cascading Style Sheets (CSS), including styling text, adding margins and padding, and controlling how images display. The course also explores the tools needed to work with CSS, the differences between embedded and external styles, how to use selectors to target elements, and what to do when styles conflict.
One of the biggest mistakes I see new web designers make is thinking of classes and IDs as simply a means of styling page content. While classes and ID selectors certainly allow us to do that, it is not the primary purpose of those attributes. Class and ID attributes exists to help give your content more meaning. Take a look at the page that I have opened here. I'm using HTML5, and while HTML5 helps extend the meaning of our code with elements like header, article, and footer, it still might not be as descriptive as you'd like it to be.
In many cases, HTML simply lacks a corresponding tag to the type of content you wish to identify. For example, throughout our code we have several artists who we referenced, but we have no way of identifying them as artists through existing HTML tags. So when you have cases like that, class and ID attributes can help you extend the meaning of your code. Now, if you approach using classes and IDs in that manner, your page structure will be more meaningful and its content much easier to style.
This is probably easier to illustrate by seeing it in action, so let's go ahead and take a look at using IDs and classes properly. I've got the considerations.htm file opened, and this can be found in the Chapter_02 folder, 02_05. So the first thing I want to do is just kind of go through some of the existing code and see if there's any existing class and ID attributes that need to be maybe a little bit more descriptive. So as I scroll through, I can see really the only thing that we have in here right now in terms of classes and IDs are the classes that we have around all of the dates in this list.
Obviously I want to style them as bold because I've created a class that says bold. The thing is, that doesn't really mean anything. We know what it means in terms of how we want the text to look, but in terms of conveying the meaning of the content of that tag, it doesn't do a thing for us. So we can make this a lot more meaningful by changing the class attribute bold to date. That means something. So now if somebody is perusing this code, whether it's machine or human readable, the word date actually imparts meaning in terms of what is inside the contents of the span tag.
Now obviously, if I do that, I have to scroll up and change the corresponding class selector as well, so I'm going to change that from .bold to .date. Now, in terms of how this functions, it won't change at all. It will look exactly the same. It will style exactly the same, but now instead, it actually has semantic meaning to it, instead of just this presentational meaning that it had before. Okay. So now I want to take a look at maybe extending the meaning of the content on the page, and the first thing I want to do is go through and identify the elements that would benefit from an ID.
They can be inside of articles. They can be inside of sections. You can really place them anywhere that you want. This is the main header on the page. So I want to go ahead and identify as such, and the easiest way to do that is to simply add an ID attribute. So inside the first header tag, I'm going to type in ID, and the ID that I'm going to give this is pageHeader, so a meaningful ID attribute to identify exactly what that element is. For article, I notice that the content of this article are the Latest News, so the article, I'm going to go ahead and give it an ID as well, and I'm going to just give that an ID of news.
As I go down the page, I can see some of the other sort of top-level structural elements. I have an aside and inside the aside, I see that that is focusing on the Featured Artist, so I'm going to reflect that in an ID as well. So id=featuredArtist. Notice that for IDs you're going to use a single word; you don't want to use multiple words. Same thing for classes. And I'm just using CamelCase naming here. So lowercase first word and then every other word after that is going to be uppercase. Then finally, for footer, I'm just going to give it an ID of pageFooter, and then save my file.
So now I've identified some of the main regions on the page using an ID, and it makes it a little bit easier to see what type of content those regions are going to focus on. Now occasionally you're going to want to extend the meaning and an ID won't be the appropriate element. Let's take, for example, all the artists that are named in our page. So here we have Toby Malina. Here we have Jeff Layton, Shea Hansen, and finally Simon Allardice is mentioned a couple of times as well. So this content is content that is mentioned several times on the page, and it's mentioned in several different ways.
For example, here the artist name is the only thing that's inside this element, whereas every other instance the artist name is just part of the content of an element. It's in those instances that a class is perfect. Remember, you can use a class more than once on a page, so unlike an ID where I can only have one element on the page with the ID of featuredArtist, I can have multiple elements on the page with a class of artist, So that's exactly what we're going to do. So I'm going to highlight the text Toby Malina here. Now, I'm going to let Aptana Studio help me here.
Aptana Studio has a keyboard shortcut, Shift+Ctrl+W, that allows me to wrap highlighted code with a tag. It would be just as easy for me to type in an opening span tag on the front of this, then a closing span tag on the back. I'm just going to save myself a little bit of time. If your code editor has that type of functionality, feel free to take advantage of it. So what I'm going to do is I'm just going to go ahead and wrap each of these with a span tag. So I've done it for Toby Malina. I'm going to do it here for Jeff Layton, just going to wrap them in a span tag. Span tags essentially have no meaning on their own, but they allow you to sort of identify inline content, and that's why I'm using them here.
Now, I don't need to use them everywhere. Here, for instance, Simon Allardice is the only content inside this heading, so I really don't need to surround that with a span tag. I'm going to go ahead and do that here as well. Excellent! Okay, so now, I'm going to go in and just apply my class. And yes, I could have done that same time. I just want to make sure that we're focusing on the actual application of the class itself. So I'm just going to go ahead and say class=artist, and I want to do that for each one of these. So for Jeff Layton, it's a class of artist as well. Same thing for Shea Hansen.
Here is where we see one of the great things about class attributes: they can be applied to any tag. So here for the h2, for example, I'm going to go ahead and give it a class of artist and then finish that up by applying it to the span tag. So it doesn't really matter what type of element you place it on; you can go ahead and apply that class anywhere that you want. So now, we've extended the meaning in our code considerably. We now have all of our sections identified in terms of what the content of that section is, and then we have all of our artists and dates identified by classes. So even if you were just sort of reading through this code, you would know a lot more about the content, and it also makes the code a little bit more machine readable too, because scripts can be created to go through and apply different formatting or different behaviors for copying content based on these different class and ID attributes.
So notice that we haven't even talked about styling. So the most important thing here is extending the meaning of our code so that the content has more semantic meaning. Now, that also means that we are now able to style it a little bit easier as well. So if I scroll back up into my styles, I could begin to add to the styles that we already have on the page. Now, the first thing I'm going to do is I'm going to style my artist styles, so I'm just going to go up and below the date rule, I'm just going to type in .artist. And here, I'm just going to do a font- weight of bold, so we're going to identify all artists visually by bolding the text.
I'm going to do a font-style of italics, so we're also going to italicize it, and then for color, I'm going to go ahead and do navy. Then we'll go ahead and save that. Now, I'm going to follow this up with some of the ID selectors, and I'm just going to style some of my page regions visually. And again, because we've extended the meaning of our page regions through use of IDs, that makes it a lot easier for us to do. So I'm going to go right below the artist selector that we just wrote, and I'm going to go ahead and do pageHeader first.
So notice that again IDs start with the pound symbol, so #pageHeader, no space there. And we're just going to do some really simple styling here. We're going to give it a background of gray, a color of white, so that will be the text color, and a padding of 15 pixels. After pageHeader, we'll go ahead and pick up featuredArtist, and we'll style that one with a width of 300 pixels, a padding of 15 pixels as well, and finally, a background of tan.
So we'll save that, preview that in my browser, and there is our styling. Now, it's doubtful that this page is going to win any design awards, but the point here is that you should think really, really carefully about the relationship between semantics and style before you start to use classes and IDs. Try to limit the use of class and ID attributes, for the most part, to instances where additional meaning is either required or simply desired by you as an author. Now, I have to admit, I do find myself occasionally adding an arbitrary class attribute for styling purposes, but the majority of the time, I'm only adding class attributes when adding additional meaning is necessary.
You also want to make sure that if you do add meaning through classes and IDs that you're consistent in how they're used throughout the site. Establishing those rules early on as you plan your site will help you create well-structured sites and in turn, make your styles a lot easier to write.
There are currently no FAQs about CSS: Core Concepts.
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.