-
Notifications
You must be signed in to change notification settings - Fork 715
[css-values-5] Proposal for a new matches()
CSS Function
#10594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
match()
CSS Functionmatches()
CSS Function
Wouldn't see : https://codepen.io/romainmenke/pen/wvLGwyb <div id=a>
hello
<div id=b>
world
</div>
</div> :root {
--fg: light-dark(black, white);
--bg: light-dark(white, black);
}
#a {
color-scheme: dark;
color: var(--fg);
background-color: var(--bg);
}
#b {
color-scheme: light;
color: var(--fg);
background-color: var(--bg);
} In this example So in your first examples I think you would need to write: .item {
--symbol: if(matches(.favorite::before) ? "🧡" : "🩶");
&::before {
content: var(--symbol);
}
} I think authors will often be surprised by such behavior because you need to consider the outer selector, how values cascade and inherit and how both will affect the inner selector. Is the added complexity worth it, considering that we can already style elements by matched selector? On the other hand, exactly this complexity and behavior could be the feature here. |
(Please feel free to just read the TL;DR at the end.) (Context) In search of
|
Uh oh!
There was an error while loading. Please reload this page.
Abstract
The
matches()
function is proposed to enhance the conditional application of styles in CSS by checking if the current element matches a specified selector. This function complements the recently accepted newif()
function, providing a more semantic and readable way to handle conditional logic within CSS properties.Motivation
The current method of conditionally applying styles in CSS often involves the use of arguably complex techniques like emulating boolean values using integers (
0
and1
) or toggles using empty values (The --var: ; hack...
), both of which can become complex and less readable.The introduction of
if()
allows for clearer conditionality using boolean logic. However, the need to conditionally style elements based on their selector remains verbose and needs and is only really doable via addtl rules/selectors in the cascade, even if it would be simpler inline in some cases. Thematches()
function aims to address this by allowing a direct approach to conditionally setting styles based on selector matches.This would also bring greater parity with the related JS
Element.prototype.matches
method.Syntax
Usage
The
matches()
function can be used inside any CSS property value expression where conditional styles are necessary based on whether the current element matches a specified selector.Example 1: Basic example
In this example, the
--symbol
custom property is conditionally set to different emoji characters based on whether the.item
element has the.favorite
class.Comparison to existing methods
Example 2.1: Without
if()
andmatches()
This traditional approach involves redundant code as it sets the same property (
--symbol
) in multiple places.Example 2.2: Using
if()
andmatches()
This approach reduces redundancy and centralizes the logic for setting the
--symbol
property.A case for boolean expressions outside
if()
I've opened a separate proposal (#10593) focused on supporting a new "boolean" custom property type, which could hypothetically be valid and type-safe even outside of
if()
. If that's accepted, an example using a math-based switch like the one below could be simplified to use boolean logic.Example 3.1: Using a math-based switch
This example uses a math-based switch to zero out values when a condition is not met.
Example 3.2: Using
matches()
andif()
This moves the conditions inline, but they're a bit complex, so it's not ideal that we have to repeat the conditions in both values.
Example 3.3: Using
matches()
andif()
Using reusable boolean values can offload some of this complexity.
The text was updated successfully, but these errors were encountered: