CSS Portal

CSS ID Selector

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The id selector is one of the most specific and powerful selectors in CSS, used to target a single element on a page that has a unique id attribute. Unlike class selectors, which can apply to multiple elements, an id must be unique within the HTML document, making it ideal for styling or manipulating a specific element directly. Using the id selector allows developers to apply precise styling, such as changing colors, dimensions, or layout, to one element without affecting others.

When using the id selector, it is common to pair it with various class selectors or element selectors to increase specificity or create more complex styling rules. For example, you might use an id selector to style a specific header on a page while other headers remain unaffected. This makes it especially useful for unique components like navigation bars, hero sections, or modal dialogs.

The id selector is frequently combined with CSS properties like color, background-color, or display to modify an element’s appearance. For instance, if you have a page section with id="hero", you could write:

#hero {
  background-color: #f0f0f0;
  padding: 2rem;
  text-align: center;
}

In this example, only the element with id="hero" will receive the specified background, padding, and alignment, leaving all other elements untouched.

The specificity of the id selector also makes it useful in overriding styles from less specific selectors. For example, even if a class applies a certain style to many elements, a rule using the id selector for a particular element will take precedence. This property of specificity makes id selectors powerful but should be used thoughtfully to avoid overly rigid CSS that is difficult to maintain.

Syntax

#id { css declarations; }

Example

<body>

<h1 id="main-title">This is a unique heading</h1>
<p>This is a regular paragraph that won't be affected by the ID selector.</p>

</body>
/* The # symbol tells the browser to look for an ID named 'main-title' */
#main-title {
color: #2e86de;
font-size: 36px;
text-align: center;
background-color: #f1f2f6;
padding: 20px;
border-radius: 8px;
}

Browser Support

The following information will show you the current browser support for the CSS ID selector. Hover over a browser icon to see the version that first introduced support for this selector.

This CSS id selector is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 2nd January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!