carrie287
carrie287 Aug 29, 2026 • 10 views

How to Use ID Selectors in CSS to Style Specific HTML Elements (Grade 7)

Hey there! 👋 Ever wondered how websites know exactly which parts to style? It's like giving each piece of a webpage its own special name tag! We're diving into CSS ID selectors, which are super cool for styling specific elements. Think of it as giving a specific instruction, like 'Hey you, with the name 'title', I want you to be big and blue!' Let's get started! 💻
💻 Computer Science & Technology
🪄

🚀 Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

✨ Generate Custom Content

1 Answers

✅ Best Answer
User Avatar
lee.james27 Jan 1, 2026

📚 What is an ID Selector?

In CSS (Cascading Style Sheets), an ID selector is a way to target and style a single, specific HTML element on a web page. It uses the id attribute of the HTML element. Think of it as a unique identifier, like a person's name. No two elements on the same page should have the same ID!

📜 History and Background

The concept of ID selectors came about with the development of CSS to provide a mechanism for granular control over the styling of web pages. Before CSS, styling was primarily done inline within HTML, which was messy and difficult to maintain. CSS, and ID selectors specifically, allowed for separating content from presentation, making web development much more organized.

🔑 Key Principles of ID Selectors

  • 🎯 Uniqueness: Each ID should be unique within the HTML document. Using the same ID multiple times is invalid HTML.
  • 🖋️ Syntax: ID selectors are denoted by a hash symbol (#) followed by the ID name in the CSS. For example, #myTitle.
  • 💪 Specificity: ID selectors have a high level of specificity in CSS. This means that styles applied with an ID selector will often override styles applied with class selectors or element selectors.
  • ✍️ Application: You add an ID to an HTML element using the id attribute, like this: <h1 id="myTitle">My Title</h1>

💻 Real-World Examples

Let's look at some practical examples of how to use ID selectors in CSS.

Example 1: Styling a Main Heading


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

#main-title {
  color: navy;
  font-size: 32px;
  text-align: center;
}

This CSS code targets the <h1> element with the ID "main-title" and sets its color to navy, its font size to 32 pixels, and aligns the text to the center.

Example 2: Styling a Specific Paragraph


<p id="introduction">This is the introduction to my website.</p>

#introduction {
  font-family: Arial, sans-serif;
  line-height: 1.5;
  margin-bottom: 20px;
}

Here, we're targeting the <p> (paragraph) element with the ID "introduction". The CSS sets the font family, line height, and bottom margin.

Example 3: Using IDs for Navigation Links


<ul>
  <li><a id="home-link" href="#">Home</a></li>
  <li><a id="about-link" href="#">About</a></li>
  <li><a id="contact-link" href="#">Contact</a></li>
</ul>

#home-link {
  color: green;
  font-weight: bold;
}

#about-link {
  color: blue;
}

#contact-link {
  color: red;
}

This example shows how different navigation links can be styled differently using unique IDs.

✍️ Conclusion

ID selectors are a powerful tool in CSS for styling specific HTML elements. Remember to keep your IDs unique and use them wisely to create well-structured and visually appealing web pages. They're like giving each important element its own spotlight!

❓ Practice Quiz

Test your knowledge with these questions!

  1. What symbol is used to denote an ID selector in CSS?
  2. Why should IDs be unique within an HTML document?
  3. What HTML attribute is used to assign an ID to an element?
  4. What is the specificity of an ID selector compared to a class selector?
  5. Give an example of how you would select an element with the ID "footer" in CSS.

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀