Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Using classes and IDs


CSS: Core Concepts

with James Williamson

Video: Using classes and IDs

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.
Expand all | Collapse all
  1. 4m 54s
    1. Welcome
    2. Using the exercise files
      4m 2s
  2. 1h 8m
    1. Exploring default styling
      4m 56s
    2. CSS authoring tools
      2m 29s
    3. CSS syntax
      4m 45s
    4. Writing a selector
      4m 10s
    5. Setting properties
      8m 40s
    6. Common units of measurement
      7m 47s
    7. Inline styles
      5m 1s
    8. Embedded styles
      5m 19s
    9. Using external style sheets
      10m 35s
    10. Checking for browser support
      8m 48s
    11. Dealing with browser inconsistencies
      5m 30s
  3. 2h 15m
    1. Structuring HTML correctly
      2m 51s
    2. Element selectors
      4m 52s
    3. Class selectors
      6m 4s
    4. ID selectors
      3m 27s
    5. Using classes and IDs
      10m 7s
    6. Element-specific selectors
      4m 35s
    7. The universal selector
      5m 42s
    8. Grouping selectors
      4m 49s
    9. Descendent selectors
      7m 32s
    10. Child selectors
      5m 7s
    11. Adjacent selectors
      5m 30s
    12. Attribute selectors
      12m 43s
    13. Pseudo-class selectors
      3m 54s
    14. Dynamic pseudo-class selectors
      8m 29s
    15. Structural pseudo-class selectors
      6m 45s
    16. Nth-child selectors
      13m 10s
    17. Pseudo-element selectors
      12m 40s
    18. Targeting page content: Lab
      8m 56s
    19. Targeting page content: Solution
      7m 59s
  4. 44m 54s
    1. What happens when styles conflict?
      4m 0s
    2. Understanding the cascade
      5m 47s
    3. Using inheritance
      8m 26s
    4. Selector specificity
      6m 55s
    5. The !important declaration
      4m 5s
    6. Reducing conflicts through planning
      3m 33s
    7. Resolving conflicts: Lab
      6m 45s
    8. Resolving conflicts: Solution
      5m 23s
  5. 1h 47m
    1. Setting a font family
      7m 10s
    2. Using @font-face
      9m 19s
    3. Setting font size
      7m 35s
    4. Font style and font weight
      6m 52s
    5. Transforming text
      3m 58s
    6. Using text variants
      2m 49s
    7. Text decoration options
      4m 26s
    8. Setting text color
      3m 2s
    9. Writing font shorthand notation
      8m 49s
    10. Controlling text alignment
      6m 33s
    11. Letter and word spacing
      9m 11s
    12. Indenting text
      4m 30s
    13. Adjusting paragraph line height
      10m 30s
    14. Controlling the space between elements
      6m 41s
    15. Basic text formatting: Lab
      8m 45s
    16. Basic text formatting: Solution
      7m 14s
  6. 2h 1m
    1. Understanding the box model
      16m 53s
    2. Controlling element spacing
      14m 29s
    3. Controlling interior spacing
      10m 50s
    4. Margin and padding shorthand notation
      6m 27s
    5. Adding borders
      8m 57s
    6. Defining element size
      10m 7s
    7. Creating rounded corners
      6m 58s
    8. Background properties
      2m 51s
    9. Using background images
      5m 11s
    10. Controlling image positioning
      10m 25s
    11. Using multiple backgrounds
      7m 5s
    12. Background shorthand notation
      5m 25s
    13. Styling container elements: Lab
      7m 56s
    14. Styling container elements: Solution
      8m 17s
  7. 47m 51s
    1. Color keyword definitions
      5m 4s
    2. Understanding hexadecimal notation
      6m 5s
    3. Using RGB values
      4m 58s
    4. Using HSL values
      5m 17s
    5. Working with opacity
      2m 23s
    6. Using RGBa and HSLa
      3m 8s
    7. Styling drop shadows
      5m 38s
    8. CSS gradients
      6m 32s
    9. Working with color: Lab
      4m 26s
    10. Working with color: Solution
      4m 20s
  8. 1m 58s
    1. Additional resources
      1m 58s

please wait ...
Watch the Online Video Course CSS: Core Concepts
8h 52m Beginner Nov 22, 2011

Viewers: in countries Watching now:

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.

Topics include:
  • Exploring default styling
  • Writing a selector
  • Setting properties
  • Working with common units of measurement, including ems and pixels
  • Structuring HTML correctly
  • Understanding the cascade and inheritance
  • Setting a font family, font size, text color, and more
  • Understanding the box model
  • Styling container elements
  • Working with RGB vs. HSL values
  • Styling drop shadows
James Williamson

Using classes and IDs

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.

Now, the decision to use an ID or not is often a really tough one to make. A lot of people use IDs because JavaScript allows us to target elements on the page that have an ID and then do something like style it, change the content, or modify it in some way, or attach some type of behavior to it. So that's one reason to use IDs. But really to me, the deciding factors to whether you should use an ID or not is is that content unique and does it need to be identified? If we take a look at our page, for example, we start off with the header element. Now, in HTML5, you can use as many headers as you like.

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.

Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.

Mark all as unwatched Cancel


You have completed CSS: Core Concepts.

Return to your organization's learning portal to continue training, or close this page.


Upgrade to View Courses Offline


With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts. provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now

A trusted source for knowledge.


We provide training to more than 4 million people, and our members tell us that helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about 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

Sign up and receive emails about 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.