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 Selectors Basic Attribute Selectors

dane wesolko
dane wesolko
7,353 Points

Class Attributes Use

Curious ... I understand the how to write class attribute code. But in daily practice what / where / how would they be used?

3 Answers

Luke Bellamy
Luke Bellamy
3,592 Points

They are best used in javascript as you can switch the class of an object but not its id. Also they are great if multiple elements can be styled in the same way. For example if you want all divs that are 300px wide to have a border then you could give them a class called 'sdiv' and set the properties of that class to have a border. I find them to be really useful in javascript. If I need something to change its values when I press a button I can easily use a class to do this as writing out all of the styles over and over again (.style = "") can take forever and its much easier to just use classes.

Luke Bellamy
Luke Bellamy
3,592 Points

They are best used in javascript as you can switch the class of an object but not its id. Also they are great if multiple elements can be styled in the same way. For example if you want all divs that are 300px wide to have a border then you could give them a class called 'sdiv' and set the properties of that class to have a border. I find them to be really useful in javascript. If I need something to change its values when I press a button I can easily use a class to do this as writing out all of the styles over and over again (.style = "") can take forever and its much easier to just use classes.

dane wesolko
dane wesolko
7,353 Points

nice ... thanks Luke ... I realize that I am getting just an overall understanding of CSS ... but it really helps to have an idea on how they can or are used.