container-query-js

1.0.2 • Public • Published

container-query

CSS container query polyfill for js

but with additional superpowers

It uses ResizeObserver to monitor changes

demo

Installation

npm i container-query-js

Example

import containerQuery from "container-query-js"

containerQuery({
    600: { //when width of elemets is between 0 - 600 these styles will be applies
        ".card": {
            flexDirection: "row",
            textAlign: "left",
            rangeIn: el => !el.classList.contains("mid-size") && el.classList.add("mid-size"), // Will be called when element width is in the range
            rangeOut: el => el.classList.remove("mid-size") // Will be called when element width is out of range
        }
    },
    350: { //when width of elemets is between 0 - 350 these styles will be applies
        ".card": {
            flexDirection: "column",
            ".content": { //You can style childs based on parents width
                paddingTop: "1em",
                paddingLeft: 0,
                margin: "1.5em 0",
                width: (h, w) => parseInt(w / 2) + "px" // You can also use functions, it will receive height and width of current element
            }
        },
    }
})

Package Sidebar

Install

npm i container-query-js

Weekly Downloads

1

Version

1.0.2

License

ISC

Unpacked Size

380 kB

Total Files

7

Last publish

Collaborators

  • 0shuvo0