Possible CSS Bug in FF
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS @property Cross-Browser Fix</title>
<style>
@property --im-a-color {
syntax: "<color>";
inherits: false;
initial-value: red;
}
.red {
--im-a-color: red;
}
.blue {
--im-a-color: blue;
}
/* Default heading color */
h1 {
color: black;
}
/* Explicitly match blue instead of using
a boolean custom property style query */
@container style(--im-a-color: blue) {
h1 {
color: green;
}
}
</style>
</head>
<body>
<div>
<h1>I should be black</h1>
</div>
<div class="red">
<h1>I should also be black</h1>
</div>
<div class="blue">
<h1>I should be green</h1>
</div>
<script>
document.querySelectorAll("body > div").forEach((div, index) => {
const heading = div.querySelector("h1");
console.log({
heading: index + 1,
className: div.className || "(none)",
customProperty: getComputedStyle(div)
.getPropertyValue("--im-a-color")
.trim(),
headingColor: getComputedStyle(heading).color
});
});
</script>
</body>
</html>