Explicit Selectors
All selectors in immutable styles are explicit. Explicit selectors only target elements that exactly match a given selector:
1
<p>
2
color: slategray;
3
</p>
Copied!
In the example above the ruleset uses a type selector, which only targets elements of type p that do not have a class. The ruleset will not target elements of type p that do have a class. Immutable styles treat type selectors and selectors with a class as different selectors even though they target the same element type.
In a similar vein explicit selectors also apply to selectors with a class:
1
<button className="btn-primary">
2
background: cornflowerblue;
3
</button>
Copied!
In the example above the ruleset only targets elements of type button that have the class btn-primary. The ruleset will not target elements of type button that combine the btn-primary class with another class:
1
<!-- html -->
2
3
<button class="btn-primary disabled">
4
Submit
5
</button>
Copied!
The HTML element above will not receive the styles defined by the btn-primary ruleset. Combining the btn-primary class with the disabled class represents a different UI state and therefore should be handled explicitly by another ruleset:
1
<button className="btn-primary disabled">
2
background: cornflowerblue;
3
pointer-events: none;
4
opacity: 0.8;
5
</button>
Copied!
The ruleset above uses a modifier class to explicitly target elements of type button with both classes btn-primary and disabled.

Note on Sharing Styles

You may have noticed the same background declaration is defined in both the btn-primary and btn-primary disabled rulesets. Immutable styles provides ways to share styles across similar rulesets, however these fall out of scope of explicit selectors.

Nested Rulesets

Explicit selectors also apply to nested rulesets:
1
<nav>
2
<a>
3
font-size: 14px;
4
</a>
5
</a>
Copied!
In the example above the ruleset only targets elements of type a that are the immediate children of elements of type nav. This is the equivalent of a child combinator (A > B) selector in CSS.
In immutable styles all nested rulesets equate to child combinators in CSS. Child combinators offer several advantages over the less strict descendant selectors (A B) in several ways.
Firstly, the behaviour of child combinators is highly predictable. It is very easy to identify who the target elements are and who the target elements are not, since the structure of the immutable ruleset mirrors that of the target HTML. Secondly, child combinators narrow the scope of who their target elements are. This helps prevent poorly scoped selectors targeting unwanted elements, or clashing with other selectors, which prevents intensional and unintentional overrides.
Last modified 2yr ago
Copy link