257

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?

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
Sam Becker
  • 19,231
  • 14
  • 60
  • 80

9 Answers9

387

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.

EDIT: Looks like Selectutorial might have gone to the big website in the sky, so try this archive link instead.

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • 2
    I believe that most browsers will, if you (invalidly) specify multiple elements with the same ID, apply a rule with an ID selector to all of those elements. – Miles Mar 02 '09 at 18:40
  • 1
    @Miles is correct - I think it's more accurate to say /#/ targets the "ID" attribute, and /./ targets "class". Although ID *should* be unique, CSS engine doesn't validate that or care. – Rex M Mar 02 '09 at 20:50
  • @Rex M - It is useful though that given a single element it cannot have more than one id whether it's unique or not. I tried this under Firefox and elements with two ids are ignored. Elements can however have multiple classes. – Usagi Nov 16 '13 at 21:35
  • Is div.sidebar the same thing as div #sidebar? – Ali Gajani Jan 04 '14 at 23:26
  • The Selectutorial link is bad :( – Zeek2 Jul 05 '18 at 12:37
  • That's a shame. Hopefully the site will come back, but I've added an archive link. – Paul Dixon Jul 09 '18 at 10:14
31

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.

nickf
  • 537,072
  • 198
  • 649
  • 721
10

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>
arielf
  • 5,802
  • 1
  • 36
  • 48
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
8

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.

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
Jans Carton
  • 196
  • 3
7

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
{ ... }
Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
5

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.

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
Anders
  • 12,088
  • 34
  • 98
  • 146
4

.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.

nickf
  • 537,072
  • 198
  • 649
  • 721
Bobby Jack
  • 15,689
  • 15
  • 65
  • 97
2

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

tehvan
  • 10,189
  • 5
  • 27
  • 31
-2

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.