What is the difference between "#" and "." when declaring a set of styles for a particular element? Here are two examples.
.selector {
background-color:red;
property:value;
}
#selector {
background-color:red;
property:value;
}
What is the difference between "#" and "." when declaring a set of styles for a particular element? Here are two examples.
.selector {
background-color:red;
property:value;
}
#selector {
background-color:red;
property:value;
}
The dot signifies a class name while the # 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>
#
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"
)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">
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
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.
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.
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
.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.
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
{ ... }
It is also worth noting that in the cascade, an id (#) selector is more specific than a class (.) 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.
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.