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

前端 未结 11 1934
[愿得一人]
[愿得一人] 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 05:00

    The question is very vague. As BoltClock says, media queries only know the dimensions of the device. However, you can use media queries in combination with descender selectors to perform adjustments.

    .wide_container { width: 50em }
    
    .narrow_container { width: 20em }
    
    .my_element { border: 1px solid }
    
    @media (max-width: 30em) {
        .wide_container .my_element {
            color: blue;
        }
    
        .narrow_container .my_element {
            color: red;
        }
    }
    
    @media (max-width: 50em) {
        .wide_container .my_element {
            color: orange;
        }
    
        .narrow_container .my_element {
            color: green;
        }
    }
    

    The only other solution requires JS.

提交回复
热议问题