CSS ID Selector

The ID Selector in CSS is like a spotlight—it allows you to target a single, unique element on your webpage. When you want to style an element that is one-of-a-kind, this is the tool to use!

1. How to Use an ID Selector

To use an ID selector, start with a hash symbol (#) followed by the unique ID name. Then, define your styles inside curly braces.

CSScopy
/* Style for an element with the ID 'header' */
#header {
  background-color: blue;
  color: white;
}

In HTML, you assign the ID to an element using theid attribute:

HTMLcopy
<h1 id="header">Welcome to My Website</h1>

2. Uniqueness of IDs

Each ID should be unique within a page. Think of an ID as the fingerprint of an element—no two elements should share the same ID.

HTMLcopy
/* HTML example with unique IDs */
<div id="main-content">Content goes here.</div>
<footer id="footer">Footer content here.</footer>

3. Why Use an ID Selector?

ID selectors are the most specific way to style an element, making them perfect for cases where you want absolute control over an element's appearance.

Know someone who'd find this guide helpful? Please share!

Page 8 of 25

Page 8 of 25

logoFALYTOM

Learn technologies like HTML, CSS, JavaScript, React, NodeJS and so much more. Practice your skills with our free games, examples, practice questions & quizzes

    © 2024 Tomilola Group
    • facebook logo
    • twitter logo
    • instagram logo
    • linkedin logo
    • github logo
    • youtube logo