Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS CSS Foundations Values and Units Absolute Length Units

Fedor Andreev
Fedor Andreev
6,438 Points

What is the main difference between: <div> <div class=> <div id=>

Now I understand that ID are special and they can only be used once and class are not special and can be used multiple times to target something.

But what is <div> by itself then? <div> <div class=> I'm confused between these two> What would the big main difference be between these two?

3 Answers

Theodore Kruczek
Theodore Kruczek
8,800 Points

Think of "id" like your name and "class" like your species (human). If we know you are human, we can assume a lot of things about how you look. If we know your name, we can pick you out of a group of humans.

So a div's class will help us know what it looks like - its fonts, colors, etc. Establishing it's id will allow us to reference it in other places.

I hope that was a decent non-technical answer.

class and id are html attributes and we can give values to them like this:

<div id="social-navigation" class="blue-background"> 

The id and the class values are selected by your CSS stylesheet so they do a similar thing. These attributes help organize our styles so that we are not repeating our code so much. A big difference would be that you can give an HTML element multiple class values by separating them with a space. You cannot give the id attribute multiple values

<style>

#social-navigation li {
  padding: 0;
  border: 1px solid black;
}

#main-menu li {
 padding: 20px;
 border-left: 1px solid white;
}

.blue-background {
  background-color: blue;
}

</style>

<div id="social-navigation" class="grid_5 blue-background">

  <ul>
   <li>Facebook Link</li>
   <li>Twitter Link</li>
   <li>Instagram Link</li>
  </ul>

</div>

<div id="main-menu" class="grid_10 blue-background"> 

  <ul>
   <li>Home</li>
   <li>About</li>
   <li>Contact</li>
  </ul>

</div>

<!-- Check out how I used blue-background class value twice  -->

<!-- Above I have two menus and on my website I like all my menus to have a blue background a common style throughout my site -->

<!-- Although I have two menus, I want to style them uniquely, so I use a ID's for those unique styles. -->
Roland Cedo
Roland Cedo
21,261 Points

The short of it:

< DIV > Divides or groups content in your HTML

< DIV CLASS="..." > Allows you to name your DIV. You can make the name anything you want.

Example: < div class="treehouse" >

You can also give multiple DIV's the same class name which will group them together when you use the the class name in your CSS by using "." followed by the class name

Example: .treehouse { background-color: tomato; }"

< DIV ID="..." > Allows you to name a DIV similar to the Class. The difference with using ID is that you can only use it for one DIV and you have to use the "#" symbol when selecting this ID in your CSS. (using ID 's also have higher specificity)

Example:

"#treehouse { background-color: black; }"

The long of it: http://www.w3schools.com/css/css_id_class.asp