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

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

    From a layout perspective, it is possible using modern techniques.

    Its made up (I believe) by Heydon Pickering. He details the process here: http://www.heydonworks.com/article/the-flexbox-holy-albatross

    Chris Coyier picks it up and works through a demo of it here: https://css-tricks.com/putting-the-flexbox-albatross-to-real-use/

    To restate the issue, below we see 3 of the same component, each made up of three orange divs labelled a, b and c.

    The second two's blocks display vertically, because they are limited on horizontal room, while the top components 3 blocks are laid out horizontally.

    It uses the flex-basis CSS property and CSS Variables to create this effect.

    .panel{
      display: flex;
      flex-wrap: wrap;
      border: 1px solid #f00;
      $breakpoint: 600px;
      --multiplier: calc( #{$breakpoint} - 100%);
      .element{
        min-width: 33%;
        max-width: 100%;
        flex-grow: 1;
        flex-basis: calc( var(--multiplier) * 999 );
      }
    }
    

    Demo

    Heydon's article is 1000 words explaining it in detail, and I'd highly recommend reading it.

提交回复
热议问题