Code has been added to clipboard!

Styling Elements According to the CSS ID Selectors

Reading time 2 min
Published Sep 30, 2019
Updated Sep 30, 2019

TL;DR — CSS ID selectors style HTML elements according to their id attribute. ID in CSS is marked by the hash sign (#) similarly to how class selectors have the period.

Matching elements based on ID

The ID selectors in CSS get element by ID attributes. However, the ID selector cannot be used in the document multiple times.

The following example shows the basic use of CSS ID selectors:

Example
#example {
  background-color: lightpink;
  border: solid 2px;
  border-radius: 20px;
  padding: 10px;
  margin: 10px;
}
#example2 {
  background-color: red;
  border: solid 1px green;
  padding: 5px;
  border-radius: 20px;
}

Additionally, you can combine type and ID selectors to style only certain elements with the specified attribute. This example styles the <div> elements with the specified attributes but does not change the <p> element.

Example
div#example {
  background-color: lightpink;
  border: solid 2px;
  border-radius: 20px;
  padding: 10px;
  margin: 10px;
}
div#example2 {
  background-color: red;
  border: solid 1px green;
  padding: 5px;
  border-radius: 20px;
}

Overriding styles of other selectors

Styling elements in CSS by ID attributes replace settings of type and class selectors.

Example
#example {
  background-color: lightpink;
  border: solid 2px;
  border-radius: 20px;
  padding: 10px;
  margin: 10px;
}
h1 {
  background-color: red;
  border: solid 1px green;
  padding: 5px;
  border-radius: 20px;
}

Note: the fact that ID in CSS overrides other selectors might cause problems. Therefore, we recommend using class selectors more frequently to avoid issues.

CSS ID: useful tips

  • The ID in CSS only selects the exact matches, meaning that these selectors are case-sensitive.
  • CSS ID selectors must not begin with a number and must contain at least one character.
Tutorial
CSS3 Features
Syntax
Classes
ID Selectors
Attribute Selectors
Stylesheets
Inline
Internal
External
Box Model
Children Selectors
Pseudo Classes
Pseudo Elements
Variables
Counters
Text
Fonts
Web Fonts
Backgrounds
Background Images
Colors
Gradients
Opacity / Transparency
Shadow Effects
Transitions
Tooltip
Transform
Animations
Layout — Display
Layout — Position
Layout — Float
Layout — Clear
Layout — Horizontal & Vertical Align
Multiple Columns
Introduction
Responsive Web Design — Viewport
Responsive Web Design — Grid View
Responsive Web Design — Media Queries
Responsive Web Design — Flexbox Layout
Responsive Web Design — Images
Responsive Web Design — Videos
Borders
Margin
Padding
Width
Height
Outline
Links
Lists
Tables
Dropdown Menu
Navigation Bar
Images
Image Gallery
Border Images
Forms
Rounded Corners
Buttons
Box-Sizing
Selector Reference
Pagination Examples
Code Examples
CSS3 Browser Support Reference
Functions Reference
Speech Module Reference
Units
Web Safe Font Combinations
Cheat Sheet
:hover
@font-face
@keyframes
@media
align-content
align-items
align-self
all
animation
backface-visibility
background
background-clip
background-color
background-image
background-origin
background-position
background-size
border
border-image
border-radius
border-style
box-shadow
box-sizing
color
columns
filter
flex
flex-basis
flex-direction
flex-flow
flex-grow
flex-shrink
flex-wrap
font
font-family
font-size
font-size-adjust
font-stretch
font-style
font-weight
hanging-punctuation
justify-content
line-height
margin
offset
opacity
order
outline
overflow
padding
perspective
position
resize
tab-size
text-align
text-decoration
text-emphasis
text-transform
text-shadow
transform
transition-property
translate
vertical-align
word-break
word-wrap
z-index