CSS scale height to match width - possibly with a formfactor

前端 未结 10 1782
天涯浪人
天涯浪人 2020-12-04 11:28

I have implemented a GoogleMapsV3 map in a twitterBootstrap basic responsive design site.

But my question is quite simple: i have:

10条回答
  •  甜味超标
    2020-12-04 11:31

    Here it is. Pure CSS. You do need one extra 'container' element.

    The fiddle

    (tinkerbin, actually): http://tinkerbin.com/rQ71nWDT (Tinkerbin is dead.)

    The solution.

    Note I'm using an 100% throughout the example. You can use whichever percentage you'd like.

    Since height percentages are relative to the height of the parent element, we can't rely on it. We must rely on something else. Luckily padding is relative to the width - whether it's horizontal or vertical padding. In padding-xyz: 100%, 100% equals 100% of the box's width.

    Unfortunately, padding is just that, padding. The content-box's height is 0. No problem!

    Stick an absolutely positioned element, give it 100% width, 100% height and use it as your actual content box. The 100% height works because percentage heights on absolutely positioned elements are relative to the padding-box of the box their relatively positioned to.

    HTML:

    CSS:

    #map_container {
      position: relative;
      width: 100%;
      padding-bottom: 100%;
    }
    
    #map {
      position: absolute;
      width: 100%;
      height: 100%;
    }
    

提交回复
热议问题