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

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

    I was also thinking of media queries, but then I found this:

    • http://www.mademyday.de/css-height-equals-width-with-pure-css.html
    • Maintain the aspect ratio of a div with CSS

    Just create a wrapper

    with a percentage value for padding-bottom, like this:

    div {
      width: 100%;
      padding-bottom: 75%;
      background:gold; /** <-- For the demo **/
    }

    It will result in a

    with height equal to 75% of the width of its container (a 4:3 aspect ratio).

    This technique can also be coupled with media queries and a bit of ad hoc knowledge about page layout for even more finer-grained control.

    It's enough for my needs. Which might be enough for your needs too.

提交回复
热议问题