Mudzinga

New to CSS? Learn how ID selectors let you style one special element on your page. See simple examples, follow step-by-step, and start styling with confidence—read now!

5 Minute Read

ID Selectors: Targeting Unique Elements

ID Selectors: Targeting Unique Elements

When you build a web page, sometimes you need to style one specific thing: a special button, a main heading, or a unique message. That’s where ID selectors in CSS come in.

In this beginner-friendly guide, you’ll learn:

  • What an ID is in HTML
  • How to use ID selectors in CSS
  • Common mistakes to avoid
  • Simple examples you can copy and try right away

By the end, you’ll know how to confidently target one unique element and style it exactly how you want.


What is an ID in HTML?

Think of an ID like a name tag for one element on your page.

In HTML, you can give an element an ID like this:

<h1 id="main-title">Welcome to My Page</h1>

Here:

  • <h1> is the element (a big heading)
  • id="main-title" is the ID

An ID should be unique. That means you only use main-title once on the whole page.

You’ll use this ID in your CSS to style that exact element.


What is an ID Selector in CSS?

In CSS, you use a # (hash symbol) to select an ID.

#main-title {
  color: blue;         /* Make the text blue */
  text-align: center;  /* Center the text */
}

Here, #main-title is the ID selector. It tells the browser: “Find the element with id="main-title" and apply these styles.”

CSS ID selector pattern:

#id-name {
  /* styles go here */
}

Just replace id-name with the ID you gave your HTML element.


Example 1: Styling a Unique Heading

Let’s put HTML and CSS together.

Step 1: Write the HTML

<!DOCTYPE html>
<html>
<head>
  <title>ID Selector Example</title>
  <link rel="stylesheet" href="styles.css"><!-- Connects to CSS file -->
</head>
<body>
  <h1 id="main-title">Welcome to My Website</h1>
  <p>This is a normal paragraph without a special ID.</p>
</body>
</html>

Step 2: Add the CSS

In a file called styles.css:

/* Style only the element with id="main-title" */
#main-title {
  color: darkgreen;        /* Text color */
  font-size: 36px;         /* Make it big */
  text-transform: uppercase; /* All caps */
}

What you’ll see

  • The heading “Welcome to My Website” will be large, dark green, and UPPERCASE.
  • The paragraph will look normal.

This shows how an ID selector lets you style one specific element without affecting others.


Example 2: Highlighting a Special Button

Now let’s use an ID to create a special button, like a “Sign Up” button.

Step 1: HTML with a special button

<button id="signup-button">Sign Up</button>
<button>Learn More</button>

Here, only the first button has an ID: signup-button.

Step 2: CSS for the ID selector

/* Style ONLY the signup button */
#signup-button {
  background-color: #007bff; /* Blue background */
  color: white;              /* White text */
  padding: 10px 20px;        /* Space inside the button */
  border: none;              /* Remove border */
  border-radius: 4px;        /* Slightly rounded corners */
}

What you’ll see

  • The Sign Up button will be blue, with white text and rounded corners.
  • The Learn More button will look like a plain default button.

This is perfect when you want one button to stand out from the rest.


Try it yourself

Open a simple HTML file and:

  1. Add a heading with an ID, like id="special-heading".
  2. Add a paragraph without an ID.
  3. In your CSS, style #special-heading to be a different color and size.

Example CSS to experiment with:

#special-heading {
  color: purple;
  font-size: 40px;
  text-decoration: underline;
}

Then, try changing the color, size, or adding text-align: center; and refresh your browser to see what changes.

Notice how only the element with that ID is affected.


Important Rules for Using IDs

Using IDs is simple, but there are a few rules to keep in mind.

1. IDs must be unique

You should only use each ID once per page.

This is wrong:

<h1 id="title">First Title</h1>
<h2 id="title">Second Title</h2> <!-- Same ID: not allowed -->

Instead, use different IDs:

<h1 id="main-title">First Title</h1>
<h2 id="sub-title">Second Title</h2>

2. IDs cannot contain spaces

This is invalid:

<h1 id="main title">Hello</h1> <!-- Space is not allowed -->

Use dashes or underscores instead:

<h1 id="main-title">Hello</h1>
<h1 id="main_title">Hello</h1>

3. IDs are case-sensitive in CSS

This means #MainTitle and #maintitle are not the same.

Keep your ID names simple and consistent, like:

  • main-title
  • signup-button
  • hero-section

Example 3: Combining IDs with Other Elements

You can mix ID selectors with other CSS selectors to be even more precise.

Let’s say you have this HTML:

<section id="hero">
  <h1>Welcome!</h1>
  <p>Your journey starts here.</p>
</section>

And this CSS:

/* Style the entire section with id="hero" */
#hero {
  background-color: #f5f5f5; /* Light gray background */
  padding: 20px;             /* Space inside the section */
}

/* Style ONLY the heading inside #hero */
#hero h1 {
  color: #333;      /* Dark gray text */
  font-size: 32px;  /* Larger text */
}

/* Style ONLY the paragraph inside #hero */
#hero p {
  color: #555;      /* Slightly lighter gray */
}

What’s happening here?

  • #hero styles the whole section.
  • #hero h1 styles the heading inside that section.
  • #hero p styles the paragraph inside that section.

This lets you design a neat “hero” area at the top of your page.


When Should You Use an ID vs. a Class?

You might also hear about classes (written with a dot, like .button). For now, just remember this simple rule:

  • Use an ID when the element is one-of-a-kind on the page.
    • Example: main header, site logo area, unique “Sign Up” banner.
  • Use a class when you want to style many elements the same way.
    • Example: all buttons, all cards, all warning messages.

In CSS:

  • ID selector: #main-title { ... }
  • Class selector: .button { ... }

You’ll often use both in real projects. Starting with IDs is a good way to learn how CSS targeting works.


Common Mistakes (and How to Avoid Them)

  1. Forgetting the # in CSS
main-title {  /* ❌ Wrong */
  color: red;
}

#main-title { /* ✅ Right */
  color: red;
}
  1. Reusing the same ID many times

If you catch yourself copy-pasting the same id="something" on multiple elements, switch to a class instead.

  1. Spelling mismatch between HTML and CSS
<h1 id="maintitle">Hello</h1>
#main-title {  /* ❌ Won’t match */
  color: blue;
}

The names must match exactly: same letters, same dashes, no extra characters.


Try it yourself: Mini Project

Create a simple web page with:

  1. A main heading with id="page-title".
  2. A special button with id="cta-button" (CTA = Call To Action).
  3. A section with id="intro" that has a heading and a paragraph.

Then, in your CSS:

#page-title {
  text-align: center;
  color: darkblue;
}

#cta-button {
  background-color: orange;
  color: white;
  padding: 12px 24px;
}

#intro {
  background-color: #eef;
  padding: 15px;
}

Refresh your page and enjoy your styled layout. Change colors, sizes, and spacing until it looks how you like.

Celebrate that: you just used ID selectors to control key parts of your page.


Recap: Key Takeaways

  • An ID is a unique name you give to one HTML element with id="something".
  • An ID selector in CSS starts with #, like #main-title { ... }.
  • IDs should be unique, have no spaces, and be written consistently.
  • Use IDs to style one special element, like a main heading, hero section, or featured button.
  • You can combine ID selectors with element names (like #hero h1) for more precise control.

You’ve taken an important step in learning CSS. Keep experimenting with IDs, and when you’re ready, explore class selectors to style groups of elements. With these tools, you’re well on your way to building pages that look exactly how you imagine.

About Percy Mudzinga

This article was automatically generated by an AI-powered blog system built by Percy.
Percy Mudzinga is a Senior Full-Stack Software Engineer based in Harare, Zimbabwe, with nearly a decade of experience building enterprise web and mobile applications. He specializes in React, Vue.js, Flutter, and Node.js.

Never Miss an Update

Subscribe to our newsletter and get the latest articles delivered directly to your inbox every week.

No spam, unsubscribe anytime. We respect your privacy.

© 2025 Mudzinga. All rights reserved.