In CSS what is the difference between "." and "#" when declaring a set of styles?
CssCss SelectorsCss Problem Overview
What is the difference between #
and .
when declaring a set of styles for an element and what are the semantics that come into play when deciding which one to use?
Css Solutions
Solution 1 - Css
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 attributeid="foo"
.foo {}
will style all elements with an attributeclass="foo"
(you can have multiple classes assigned to an element too, just separate them with spaces, e.g.class="foo bar"
)
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. Typical uses
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.
EDIT: Looks like Selectutorial might have gone to the big website in the sky, so try this archive link instead.
Solution 2 - Css
The #
means that it matches the id
of an element. The .
signifies the class name:
<div id="myRedText">This will be red.</div>
<div class="blueText">this will be blue.</div>
#myRedText {
color: red;
}
.blueText {
color: blue;
}
Note that in a HTML document, the id attribute must be unique, so if you have more than one element needing a specific style, you should use a class name.
Solution 3 - Css
The dot(.
) signifies a class name while the hash (#
) signifies an element with a specific id attribute. The class will apply to any element decorated with that particular class, while the # style will only apply to the element with that particular id.
Class name:
<style>
.class { ... }
</style>
<div class="class"></div>
<span class="class></span>
<a href="..." class="class">...</a>
Named element:
<style>
#name { ... }
</style>
<div id="name"></div>
Solution 4 - Css
It is also worth noting that in the cascade, an id (#
) selector is more specific than a b (.
) selector. Therefore, rules in the id statement will override rules in the class statement.
For example, if both of the following statements:
.headline {
color:red;
font-size: 3em;
}
#specials {
color:blue;
font-style: italic;
}
are applied to the same HTML element:
<h1 id="specials" class="headline">Today's Specials</h1>
the color:blue rule would override the color:red rule.
Solution 5 - Css
A couple of quick extensions on what has already been said...
An id
must be unique, but you can use the same id to make different styles more specific.
For example, given this HTML extract:
<div id="sidebar">
<h2>Heading</h2>
<ul class="menu">
...
</ul>
</div>
<div id="content">
<h2>Heading</h2>
...
</div>
<div id="footer">
<ul class="menu">
...
</ul>
</div>
You could apply different styles with these:
#sidebar h2
{ ... }
#sidebar .menu
{ ... }
#content h2
{ ... }
#footer .menu
{ ... }
Another useful thing to know: you can have multiple classes on an element, by space-delimiting them...
<ul class="main menu">...</ul>
<ul class="other menu">...</ul>
Which allows you to have common styling in .menu
with specific styles using .main.menu
and .sub.menu
.menu
{ ... }
.main.menu
{ ... }
.other.menu
{ ... }
Solution 6 - Css
Like pretty much everyone has stated already:
A period (.
) indicates a class, and a hash (#
) indicates an ID.
The fundamental difference between is that you can reuse a class on your page over and over, whereas an ID can be used once. That is, of course, if you are sticking to WC3 standards.
A page will still render if you have multiple elements with the same ID, but you will run into problems if/when you try to dynamically update said elements by calling them with their ID, since they are not unique.
It is also useful to note that ID properties will supersede class properties.
Solution 7 - Css
.class
targets the following element:
<div class="class"></div>
#class
targets the following element:
<div id="class"></div>
Note that the id MUST be unique throughout the document, whilst any number of elements may share a class.
Solution 8 - Css
The # is an id selector. It matches only elements with a matching id. Next style rule will match the element that has an id attribute with a value of "green":
#green {color: green}
See http://www.w3schools.com/css/css_syntax.asp for more information
Solution 9 - Css
Here is my aproach of explaining te rules .style
and #style
are part of a matrix.
that if not in the right order, they can override each other, or cause conflicts.
Here is the line up.
Matrix
#style 0,0,1,0 id
.style 0,1,0,0 class
if you want override these two you can use <style></style>
witch has a matrix level or 1,0,0,0.
And @media query's will override everything above...
I am not sure about this but i think the ID selector #
can only be used once in a page.