element-match-media
v0.0.3
Published
Like window.matchMedia() but for HTML elements
Maintainers
Readme
element-match-media
Like window.matchMedia() but for HTML elements.
About
Parse media queries from the context of an element.
Usage
elementMatchMedia(element, mediaQueryString)elementis a reference to a DOM node, or a string representing a CSS selector to match withdocument.querySelector()mediaQueryStringis a string containing a media query
This function evaluates the mediaQueryString as though the viewport's dimensions matched those of the element given to the function. It returns the MediaQueryList object.
Most of the time you use this you'll probably be wanting to check the .matches property to see whether the media query evaluates to true or false.
View tests
Demos
Here are some basic demos showing different ways you might hook this plugin up to work with custom selectors or properties in CSS:
.minwidth {
--breakpoint: (min-width: 300px) {
border-color: limegreen;
background: greenyellow;
};
}.minwidth[--breakpoint="(min-width: 300px)"] {
border-color: limegreen;
background: greenyellow;
}