IDs allow you to assign a unique identifier to an HTML element. This allows you to define a style that can only be used by the element you assign the ID to.
CSS ID Syntax
The syntax for declaring a CSS ID is the same as for classes - except that instead of using a dot, you use a hash (#).
Again, similar to classes, if you want to use the same id name for multiple elements, but each with a different style, you can prefix the hash with the HTML element name.
CSS ID Example
CSS IDs can be very useful
IDs vs Classes
Given classes and IDs are very similar, you may be wondering which one to use. This depends on the situation.
When to use classes
You should use classes when your style needs to be applied multiple times on the same page. For example, you might have many
<h1> elements that need the same style applied.
When to use IDs
You should use IDs if only one element on the page should have the style applied, and/or you need a unique identifier for that element. For example, you might assign an ID to a
<div> tag which contains your left menu. The styles for this ID could contain the position, background-color, float properties, size etc. You probably wouldn't want any other element on the page to use this particular style.