-
Hajipur, Bihar, 844101
CSS selectors are used to select HTML elements that you want to style. They form the foundation of CSS because without selectors, styles cannot be applied to specific parts of a webpage. A selector tells the browser exactly which elements should receive the defined styles. In this chapter, you will learn what CSS selectors are, why they are important, and the different types of selectors used in CSS with clear explanations and examples.
A CSS selector is the part of a CSS rule that targets one or more HTML elements. The selector comes before the declaration block and defines where the styles should be applied.
p {
color: blue;
}
Here, p is the selector, and it targets all paragraph elements on the page.
Selectors allow developers to control styles precisely and efficiently.
CSS selectors help apply styles in a structured and organized way. Some key benefits include:
Allow styling of specific elements
Reduce repeated code
Improve readability and maintenance
Enable complex layouts and designs
Improve performance when used correctly
Choosing the right selector ensures clean and predictable styling.
The element selector targets HTML elements by their tag name.
h1 {
color: green;
}
This rule applies to all <h1> elements.
Element selectors are useful when you want to style all elements of the same type consistently.
The class selector targets elements with a specific class attribute. It is written using a dot (.) followed by the class name.
.highlight {
background-color: yellow;
}
<p class="highlight">Important text</p>
Class selectors are reusable and commonly used for styling multiple elements in the same way.
The ID selector targets an element with a specific id attribute. It is written using a hash (#) followed by the ID name.
#mainTitle {
font-size: 32px;
}
<h1 id="mainTitle">CSS Selectors</h1>
An ID must be unique on a page
IDs should not be reused
ID selectors have higher priority than class and element selectors.
The universal selector selects all elements on a page. It is represented by an asterisk (*).
* {
margin: 0;
padding: 0;
}
Commonly used to reset default browser styles.
Grouping selectors allow you to apply the same styles to multiple selectors by separating them with commas.
h1, h2, h3 {
color: navy;
}
This reduces repetition and keeps CSS clean.
The descendant selector selects elements that are inside another element.
div p {
color: blue;
}
This rule targets all <p> elements inside a <div>.
The child selector selects elements that are direct children of a specific element. It uses the greater-than symbol (>).
ul > li {
list-style-type: square;
}
This applies only to direct child <li> elements of <ul>.
Attribute selectors target elements based on the presence or value of an attribute.
input[type="text"] {
border: 1px solid gray;
}
This selects only text input fields.
Pseudo-classes define a special state of an element, such as hover or focus.
:hover
:active
:focus
:visited
a:hover {
color: red;
}
Pseudo-elements style specific parts of an element.
::before
::after
::first-letter
::first-line
p::first-letter {
font-size: 24px;
}
This selector selects an element that comes immediately after another element.
h2 + p {
color: gray;
}
This selector selects all sibling elements that follow a specified element.
h2 ~ p {
font-style: italic;
}
Selectors can be combined to create more specific targeting.
div.content p.highlight {
color: green;
}
This selects paragraphs with class highlight inside a div with class content.
Specificity determines which CSS rule is applied when multiple rules target the same element.
Priority order:
Inline styles
ID selectors
Class, attribute, and pseudo-class selectors
Element selectors
Understanding specificity avoids unexpected styling issues.
Beginners often make these mistakes:
Overusing ID selectors
Writing overly complex selectors
Forgetting dots or hashes
Applying styles too broadly
Ignoring specificity rules
Writing simple and clear selectors improves performance and maintainability.
Prefer class selectors over IDs
Keep selectors short and readable
Avoid deep nesting
Use grouping selectors when possible
Comment complex selectors
These practices help create scalable CSS code.
CSS selectors are the core mechanism used to apply styles to HTML elements. They allow precise control over which elements receive styling. By understanding different selector types such as element, class, ID, attribute, pseudo-class, and pseudo-element selectors, you can write clean, efficient, and maintainable CSS. Mastering selectors helps you build flexible designs and avoid conflicts in large projects.
Q1. Target all <p> elements and change their font size to 16px.
Q2. Style all <h1> and <h2> elements to have blue text color using a group selector.
Q3. Create a class .highlight and apply a yellow background color.
Q4. Write a rule to target an element with ID #banner and make its text bold.
Q5. Use the universal selector to remove all margins.
Q6. Create a .button class that changes text color to white.
Q7. Write a CSS rule to apply a font family of Arial to all <div> elements.
Q8. Combine a group selector to style <ul> and <ol> with the same padding.
Q9. Write a class .card to set the border to 1px solid gray.
Q10. Write a rule that applies the same font size to <h1>, <h2>, and <p> elements.