Name of the CSS variable to check for.
Optional
value: CssVarValue = undefinedOptional value of the CSS variable to check for.
Optional
target: null | Target = document.documentElementOptional Element, EventTarget, or CSS selector.
true
if the CSS variable name
is present.
elements!InvalidElementError
if the specified target
wasn't found.
HTML
<style>
:root {
--color-fg: green;
--padding-small: "24px";
--is-small: true;
}
</style>
<button id="example" style="--color-bg: blue; --is-big: true;">
Example
</button>
Check Element
const element = findElement("#example")!;
hasCssVar("--color-bg", undefined, element);
// true
hasCssVar("--is-big", "true", element);
// false ("true" cannot be a string, must be the boolean value `true`)
hasCssVar("--color-bg", "blue", element);
// true
Check :root
hasCssVar("--color-fg");
// true
hasCssVar("--is-small", "true");
// false ("true" cannot be a string, must be the boolean value `true`)
hasCssVar("--color-fg", "green");
// true
Checks if the
target
has the CSS variable withname
. If avalue
is specified, checks that the values match.If no
target
is specified, usesdocumentElement
(i.e.:root
).