Can media queries resize based on a div element instead of the screen?

前端 未结 11 1918
[愿得一人]
[愿得一人] 2020-11-22 04:17

I would like to use media queries to resize elements based on the size of a div element they are in. I cannot use the screen size as the div is jus

11条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 04:41

    This is currently not possible with CSS alone as @BoltClock wrote in the accepted answer, but you can work around that by using JavaScript.

    I created a container query (aka element query) prolyfill to solve this kind of issue. It works a bit different than other scripts, so you don’t have to edit the HTML code of your elements. All you have to do is include the script and use it in your CSS like so:

    .element:container(width > 99px) {
        /* If its container is at least 100px wide */
    }
    

    https://github.com/ausi/cq-prolyfill

提交回复
热议问题