Skip to content

Instantly share code, notes, and snippets.

@y-a-v-a
Created September 9, 2022 12:11
Show Gist options
  • Save y-a-v-a/d54e57ff01205e91d3bd81dc20e59295 to your computer and use it in GitHub Desktop.
Save y-a-v-a/d54e57ff01205e91d3bd81dc20e59295 to your computer and use it in GitHub Desktop.
Simple feature detection for CSS :has()
(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);
}());
@y-a-v-a
Copy link
Author

y-a-v-a commented Sep 9, 2022

(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.

@y-a-v-a
Copy link
Author

y-a-v-a commented Sep 9, 2022

@supports selector(A:has(b)) {
  html:has(body) {
    color: #fff;
  }
}

@y-a-v-a
Copy link
Author

y-a-v-a commented Sep 12, 2022

CSS.supports('selector(html:has(body))');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment