2 replies [Last post]
nhakhand
nhakhand's picture
Offline
newbie
Last seen: 5 years 31 weeks ago
Timezone: GMT+6
Joined: 2014-10-13
Posts: 1
Points: 2

Can you please explain where I will use class selector or Id selector ?Please make examples.

gautamz07
gautamz07's picture
Offline
Enthusiast
Last seen: 3 years 36 weeks ago
Timezone: GMT+5.5
Joined: 2014-04-24
Posts: 265
Points: 403

Reply

1st off remember one thing ID is unique , which means you cannot use it twice !!!!!!!!

and a class can be reused as much as you want to .

ID attraibutes are usually used in the menu's of a website because you don't want the menu item to be repeated . (in common mans language , if you click on the #home section , you don't want to be having 2 home sections , a section got to be unique eg . home , aboutus , contact us).

coming to the class attribute , suppose you have multiple elements and each of those elements require the same styling , what you'd do is put the style in a class and apply the class to the HTML element . thats what i mean when i say a class can be used multiple times .

eg. suppose you want all your H1 tags to be red in color , make a class .red(you can give it any name) and than add the styling in it and than go to your HTML document and apply the class

CSS code

.red{
   color:red;
}

HTML Code

<h1 class="red"> I am a Heading</h1>
<h1 class="red"> I am a Heading</h1>
<h1 class="red"> I am a Heading</h1>

Moral of the story : when you fine an elemnt that you'd like to be unique and not repeated more than once give it an ID , and when you find a bunch of HTML elements that require the same styling , use the class.

gautamz07
gautamz07's picture
Offline
Enthusiast
Last seen: 3 years 36 weeks ago
Timezone: GMT+5.5
Joined: 2014-04-24
Posts: 265
Points: 403

another Pointer

As you delve more into web development or how HTML and CSS communicate with each other , u'll find much more purpose for the ID attribute , than just when your starting off .