Jump to content
Rickname

.class and #id, what's the difference?

Recommended Posts

I'm quite new to coding, and have big trouble understanding the logic in even the supposedly easiest languages to learn. However, what is the functional differences between .class and #id? Is there situations where only either of them can be used? I can't see any difference by reading the W3 pages about them  (respectively https://www.w3schools.com/cssref/sel_class.asp and https://www.w3schools.com/cssref/sel_id.asp).

Share this post


Link to post
Share on other sites

id ref can be only applied to a single element on a page, a class reference can refer to multiple elements with same class reference on a page.

Share this post


Link to post
Share on other sites

So classes are more flexible ref than id. But what is special about id? I can't imaging why it exists if it only can do what classes can do, and that with narrower field of usage.

Share this post


Link to post
Share on other sites

For CSS you can target a specific singular id that refers to a specific area from a website layout ,that has multiple classes that are also used within other areas that each have a unique id.

Then by referring to this id and then the class name as in '#idref .classname'  I can change the styling of these classes, without affecting others elsewhere.

For JavaScript you can do the same to manipulate specific elements within that id identifier element only .

Simple example would be #header, #left, #right, #content, #left-aside, #right-aside, which specifically identify a specific area of your site, which you need to style and target individually.

Edited by dsonesuk

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×