Difference between ". " and "#" in css

Posted by Sekar.C under Interview Questions on 4/16/2012 | Points: 10 | Views : 1098 | Status : [Member] | Replies : 3
hi,
Difference between ". " and "#" in css
Regards
sekar.c

Regards
Sekar.c



Responses

Posted by: Sabarimahesh on: 4/16/2012 [Member] Bronze | Points: 25

Up
0
Down
Yes, they are different...

# is an id selector, used to target a single specific element with a unique id, but . is a class selector used to target multiple elements with a particular class. To put it another way:

#foo {} will style the single element declared with an attribute id="foo"
.foo {} will style all elements with an attribute class="foo" (you can have multiple classes assigned to an element too, just separate them with spaces, e.g. class="foo bar")
Typical uses

Generally speaking, you use # for styling something you know is only going to appear once, for example, things like high level layout divs such sidebars, banner areas etc.

Classes are used where the style is repeated, e.g. say you head a special form of header for error messages, you could create a style h1.error {} which would only apply to <h1 class="error">

Specificity

Another aspect where selectors differ is in their specificity - an id selector is deemed to be more specific than class selector. This means that where styles conflict on an element, the ones defined with the more specific selector will override less specific selectors. For example, given <div id="sidebar" class="box"> any rules for #sidebar with override conflicting rules for .box

Learn more about CSS selectors

See Selectutorial for more great primers on CSS selectors - they are incredibly powerful, and if your conception is simply that "# is used for DIVs" you'd do well to read up on exactly how to use CSS more effectively.

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sunny4989 on: 4/17/2012 [Member] Starter | Points: 25

Up
0
Down
'#' is referred as "ID Selector"
'.' is referred as "Class Selector"

'#' is used for specifying style for unique element
'.' is used for specifying style for group of element

------------------------------------------------
Learn throughout life

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tra072011 on: 8/19/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

You can find this info by using search box in the top of website with some keywords related before posting questions

If you want to get more materials that related to this topic, you can visit: http://interviewquestionstoask.info/target-interview-questions/

Best regards.


Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response