Welcome to The Coding College! In this article, we’ll dive into CSS Selectors, a core concept of CSS that allows you to target HTML elements and apply styles to them. Mastering CSS selectors is essential for writing efficient, maintainable, and dynamic stylesheets.
What Are CSS Selectors?
CSS selectors define the elements on a web page to which a style applies. They are the “bridge” between your HTML structure and your CSS rules. By using selectors, you can apply styles to specific elements, groups of elements, or elements based on their attributes, states, or relationships.
Types of CSS Selectors
1. Universal Selector (*
)
Targets all elements on a page.
* {
margin: 0;
padding: 0;
}
Use Case: Reset default browser styles for consistency.
2. Type Selector
Targets all elements of a specific type (HTML tags).
p {
font-size: 16px;
line-height: 1.5;
}
Use Case: Apply consistent styles to all paragraphs, headings, or other elements.
3. Class Selector (.classname
)
Targets elements with a specific class attribute.
.highlight {
background-color: yellow;
font-weight: bold;
}
HTML Example:
<p class="highlight">This text is highlighted.</p>
Use Case: Apply styles to a group of elements that share a common class.
4. ID Selector (#idname
)
Targets a single element with a unique ID attribute.
#main-header {
font-size: 24px;
text-align: center;
}
HTML Example:
<h1 id="main-header">Welcome to The Coding College</h1>
Use Case: Apply styles to a unique element, such as a header or footer.
5. Group Selector
Targets multiple elements by separating selectors with commas.
h1, h2, h3 {
font-family: Arial, sans-serif;
}
Use Case: Apply shared styles to multiple elements.
6. Descendant Selector (Space)
Targets elements nested within another element.
div p {
color: gray;
}
HTML Example:
<div>
<p>This paragraph is inside a div.</p>
</div>
Use Case: Style elements based on their hierarchical structure.
7. Child Selector (>
)
Targets direct children of an element.
ul > li {
list-style: none;
}
HTML Example:
<ul>
<li>First item</li> <!-- Styled -->
<ul>
<li>Nested item</li> <!-- Not styled -->
</ul>
</ul>
Use Case: Focus styles on direct descendants only.
8. Adjacent Sibling Selector (+
)
Targets an element immediately following another element.
h1 + p {
margin-top: 10px;
}
HTML Example:
<h1>Heading</h1>
<p>Paragraph after heading.</p>
Use Case: Style elements that are visually or logically paired.
9. General Sibling Selector (~
)
Targets all siblings of an element.
h1 ~ p {
color: blue;
}
HTML Example:
<h1>Heading</h1>
<p>First paragraph.</p>
<p>Second paragraph.</p>
Use Case: Apply styles to all elements sharing the same parent.
10. Attribute Selector
Targets elements based on their attributes and attribute values.
- Basic Attribute Selector:
input[type] {
border: 1px solid black;
}
- Attribute Value Selector:
input[type="text"] {
border: 2px solid blue;
}
HTML Example:
<input type="text">
<input type="password">
Use Case: Style form elements or elements with specific attributes.
Advanced Selectors
1. Pseudo-classes
Style elements based on their state.
a:hover {
color: red;
}
Common pseudo-classes:
:hover
: Styles when hovered over.:focus
: Styles when focused.:nth-child(n)
: Styles specific child elements.
2. Pseudo-elements
Style specific parts of an element.
p::first-line {
font-weight: bold;
}
Common pseudo-elements:
::before
: Inserts content before an element.::after
: Inserts content after an element.::first-line
: Styles the first line of text.
Combining Selectors
You can combine selectors to create powerful styles:
div#container .highlight:first-child {
color: green;
}
Example Explained: Targets the first child of elements with the highlight
class inside a div
with the ID container
.
Selector Specificity
When multiple rules target the same element, CSS uses specificity to decide which rule applies.
Specificity hierarchy:
- Inline styles (highest priority).
- ID selectors.
- Class, attribute, and pseudo-class selectors.
- Type and universal selectors (lowest priority).
Conclusion
CSS selectors are the foundation of web styling, allowing you to target elements with precision and efficiency. By mastering the various types of selectors, you can create flexible, maintainable, and scalable stylesheets.
Ready to take your skills further? Dive into advanced topics like CSS Grid and Flexbox in our upcoming guides!