Skip to content

Instantly share code, notes, and snippets.

@oscarmarina
Last active April 15, 2023 13:19
Show Gist options
  • Select an option

  • Save oscarmarina/30fc6279fd600bbebc65f6a0bb642947 to your computer and use it in GitHub Desktop.

Select an option

Save oscarmarina/30fc6279fd600bbebc65f6a0bb642947 to your computer and use it in GitHub Desktop.
Testing Support for CSS Pseudo-classes & Pseudo-elements
/**
* Checks if the given CSS pseudo-selector is supported by the browser.
*
* @param {string} selector - The CSS pseudo-selector to check.
* @returns {boolean} - True if the selector is supported, false otherwise.
*/
export const isPseudoSelectorSupported = (selector) => {
const style = document.createElement('style');
let isSupported;
try {
document.head.appendChild(style);
style.sheet.insertRule(`${selector} {}`);
isSupported = true;
} catch {
isSupported = false;
} finally {
style.remove();
}
return isSupported;
};
/*
console.log(isPseudoSelectorSupported(':focus-visible'));
console.log(isPseudoSelectorSupported('::part(foo)'));
console.log(isPseudoSelectorSupported(':host-context(foo)'));
console.log(isPseudoSelectorSupported(':has(foo)'));
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment