Advanced Selectors: Custom Properties and Complex Conditions
Advanced Selectors: Custom Properties and Complex Conditions
Introduction
Advanced selectors in CSS allow for precise targeting of elements based on complex criteria, such as combining multiple selectors, using pseudo-classes, or leveraging custom properties. These selectors enhance the flexibility and power of CSS, enabling developers to create dynamic and maintainable styles.
Custom Properties
Custom properties (variables) in CSS provide a way to store reusable values, such as colors, fonts, or spacing, and apply them across the stylesheet. They are defined using the -- prefix and can be used directly in selectors.
Syntax:
:root {
--primary-color: #333;
}
.box {
color: var(--primary-color);
}
Custom properties can be embedded within selectors to create dynamic, conditional styles. For example, a selector can reference a custom property to apply a specific theme:
.header {
background-color: var(--primary-color);
font-family: var(--font-family);
}
Advanced Use Cases:
- Nested Selectors: Combine multiple selectors with
andororto target specific elements..nav > .link and .footer .button { display: block; } - Pseudo-Classes: Use custom properties in pseudo-classes to apply styles based on user interactions.
.button:hover { background-color: var(--hover-color); }
Complex Conditions
Complex conditions involve combining multiple selectors, pseudo-classes, or pseudo-elements using logical operators (and, or, not). These conditions allow for fine-grained control over element selection.
Logical Operators:
and: Applies a selector only if all conditions are met..nav > .link and .footer .button { display: block; }or: Applies a selector if any of the conditions are met..nav > .link or .footer .button { margin: 10px; }not: Negates a condition..nav > .link not .footer .button { color: red; }
Nested Selectors:
Combine multiple selectors with and or or to target specific elements.
.header .nav > .link and .footer .button {
font-weight: bold;
}
Advanced Techniques
- Nested Selectors: Use
andororto target elements with multiple criteria. - Pseudo-Classes: Integrate custom properties with pseudo-classes to create dynamic behavior.
- Pseudo-Elements: Use custom properties in pseudo-elements to apply styles based on their position.
.button::before { content: attr(data-label); color: var(--primary-color); }
Examples
Custom Properties in Selectors:
:root {
--primary-color: #333;
}
.header {
background-color: var(--primary-color);
font-family: var(--font-family);
}
.footer {
color: var(--secondary-color);
}
Complex Conditions:
.nav > .link and .footer .button {
display: block;
}
.nav > .link or .footer .button {
margin: 10px;
}
Nested Selectors with Conditions:
.header .nav > .link and .footer .button {
font-weight: bold;
}
Conclusion
Advanced selectors, including custom properties and complex conditions, empower developers to create highly targeted and maintainable CSS. By leveraging these techniques, designers can build flexible, responsive, and scalable styles that adapt to changing requirements.