Selectors
Target HTML elements using element, class, id, and compound selectors.
CSS selectors are one of the most important parts of CSS. They allow you to choose specific HTML elements and apply styles to them. Without selectors, CSS would not know which elements on a webpage should receive a particular style.
Think of selectors as addresses. If you want to send a letter to a specific house, you need the correct address. In the same way, CSS uses selectors to find the exact HTML elements that need styling.
For example, if you want all headings to appear blue, a selector helps CSS identify those headings.
Why Are CSS Selectors Important?
Selectors help developers:
- Style specific elements on a webpage
- Apply consistent designs
- Reduce repetitive code
- Target individual sections of a website
- Create flexible and maintainable stylesheets
Imagine an online store with hundreds of products. Instead of styling each product manually, selectors allow you to style all product titles, prices, and buttons with just a few lines of CSS.
Element Selector
The element selector targets HTML elements by their tag name.
HTML
<h1>Welcome</h1>
<p>This is a paragraph.</p>CSS
h1 {
color: blue;
}
p {
color: gray;
}In this example, all h1 elements become blue and all p elements become gray.
Class Selector
A class selector targets elements that share the same class name. Classes are useful when multiple elements need the same styling.
HTML
<p class="highlight">Special Offer</p>
<p class="highlight">Limited Time Discount</p>CSS
.highlight {
color: red;
font-weight: bold;
}The dot before the class name tells CSS that it is selecting a class.
ID Selector
An ID selector targets a single unique element.
HTML
<h1 id="main-title">My Website</h1>CSS
#main-title {
color: green;
}The hash symbol is used for ID selectors.
Since IDs should be unique, they are typically used for important sections such as headers, navigation bars, or banners.
Universal Selector
The universal selector targets every element on a webpage.
* {
margin: 0;
padding: 0;
}This selector is often used to reset default browser spacing.
Group Selector
Sometimes multiple elements need the same styles. Instead of writing separate rules, you can group selectors.
h1, h2, h3 {
color: navy;
}This rule applies the same color to all three heading types.
Attribute Selector
Attribute selectors target elements based on their attributes.
HTML
<input type="text">CSS
input[type="text"] {
border: 2px solid blue;
}This selector styles only text input fields.
Real-World Example
Imagine you are building a blog website:
- Element selectors style all paragraphs.
- Class selectors style featured articles.
- ID selectors style the main header.
- Group selectors style all headings consistently.
By combining different selectors, developers can create professional and organized website designs with minimal code.
Conclusion
CSS selectors are the foundation of styling web pages. They tell CSS exactly which elements should receive specific styles. Whether you are targeting a heading, a button, a special section, or an entire webpage, selectors give you precise control over your design. Learning selectors is one of the first and most important steps toward mastering CSS.