Why doesn't percentage padding / margin work on flex items in Firefox and Edge?

后端 未结 2 1525
星月不相逢
星月不相逢 2020-11-22 01:18

I want to have a square div inside a flexbox. So I use:



        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 01:35

    In addition to Michael_B's answer, here is a possible workaround.

    When using percent we often relate that to the viewport width, so with that in mind, viewport units vw/vh can be an option, since it works similar (responsive).

    Stack snippet

    .outer {
      display: flex;
      width: 100%;
      background: blue;
    }
    .inner {
      width: 50%;
      background: yellow;
      padding-bottom: 50vw;
    }


    Updated based on a comment

    If a square is a must, and viewport units or script can't be used, here is another trick using a dummy image.

    Note, as image also a SVG or a Base64 could be used as a datauri to save an extra round trip to the server

    .outer {
      display: flex;
      width: 100%;
      background: blue;
    }
    .inner {
      width: 50%;
      background: yellow;
    }
    .inner img {
      display: block;
      width: 100%;
      visibility: hidden;
    }

提交回复
热议问题