Created
September 9, 2022 12:11
-
-
Save y-a-v-a/d54e57ff01205e91d3bd81dc20e59295 to your computer and use it in GitHub Desktop.
Simple feature detection for CSS :has()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
const s = document.createElement('style'); | |
s.textContent = `.supportsHas:has(p):after {content: "hasHas";}`; | |
document.head.appendChild(s); | |
const supportsHas = document.createElement('div'); | |
supportsHas.classList.add('supportsHas'); | |
const p = document.createElement('p'); | |
supportsHas.appendChild(p); | |
document.body.appendChild(supportsHas); | |
if (getComputedStyle(document.body, 'after')?.content === '"hasHas"') { | |
console.log('supports :has()'); | |
} else { | |
console.log('does not suport :has()'); | |
} | |
document.head.removeChild(s); | |
document.body.removeChild(supportsHas); | |
}()); |
(function(w,d,p){w[p]=!1;try
{d.querySelector('html:has(head)');
w[p]=!0;}catch{}}
(window,document,'_supportsCSSHas'));
Is the shortest way so far.
@supports selector(A:has(b)) {
html:has(body) {
color: #fff;
}
}
CSS.supports('selector(html:has(body))');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It can be done even shorter: