89

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

But my question is quite simple: i have:

<div id="map"></map>

and

#map{ width: 100%; height: 200px }

I'd like to be able to change the height to a form factor. Like in this "in my dreams CSS"

#map { width: 100%; height: width * 1.72 }

I have tried leaving out height, setting to auto, and all sorts of persentages - but only to make the div collapse on me always.

I have no problem writing a js-solution, but hope for a simple cleancut CSS solution, possible CSS3

If not possible, what would be the optimal way to js me out of this?? (timers, events...or the like)

Ivar
  • 6,138
  • 12
  • 49
  • 61
Steen
  • 2,749
  • 2
  • 20
  • 36

12 Answers12

124

Most browsers now support aspect-ratio to help with this exact scenario: https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio

Without using aspect-ratio you can use the solution below.

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:

<div id="map_container">
  <div id="map">
  </div>
</div>   

CSS:

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

#map {
  position: absolute;
  width: 100%;
  height: 100%;
}
João Paulo Macedo
  • 15,297
  • 4
  • 31
  • 41
  • exactly what I wanted. But due to other factors i think, it does not work in my environment (either in twitterbootstrap, or due to the fact, that the contents is injected vis json)......and since greg had a simple js...i'll go with that – Steen Jun 28 '12 at 11:33
  • 2
    @JOPLOmacedo, The link you gave is broken now. :( – Ravi Dhoriya ツ Jul 18 '14 at 10:22
  • 3
    To change this example to other proportion (say 2:1) you have to change only `padding-bottom` (to 50% in this case) and not other instances of `100%` – Rober Nov 11 '14 at 12:24
  • Trick didn't work at all for me. Has this changed in later versions of Chrome? – Aspelund Jun 22 '15 at 11:43
  • 1
    Nope, it hasn't. You're probably missing something small. Check it out here http://jsbin.com/moficipako/edit?html,css,output – João Paulo Macedo Jun 25 '15 at 14:41
  • 1
    This answer is not correct! The padding is calculated based on the containing box, i.e. the parent box (see: [W3C](https://www.w3.org/TR/CSS2/box.html#padding-properties)). So it will only work in cases where the width of the `#map_container` is `100%`. If you set the width of the `#map_container` to a fixed width of e.g. `250px` you will see that the height will still be equal to the width of the parent element. – Marco Miltenburg Mar 11 '16 at 21:35
  • Worked for me when I set padding-bottom to the ratio (height/width) as a percent. – adampasz Feb 13 '18 at 18:47
  • Works like a charm, should be accepted answer, as calc() in this answer is invalid. – Rubenxfd Oct 17 '18 at 08:59
  • 1
    Appreciate the cogent explanation of the (suprising!) underlying CSS rules. – BobHy Apr 10 '21 at 15:33
  • top-notch answer! "Luckily padding is relative to the width - whether it's horizontal or vertical padding": TIL – drew moore Aug 01 '23 at 05:48
56

You could try using vw for height. https://developer.mozilla.org/en/docs/Web/CSS/length

Something like

div#map {
     width: 100%;
     height: 60vw;
}

This would set the width of the div to 60% of the viewport width. You will probably need to use calc to adjust to take padding into account …

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Pit
  • 1,448
  • 1
  • 18
  • 26
  • Can you explain a little more? Why doesn't this give the div a height of 60% view width and a width of 100% of the parent? – Luke Nov 01 '15 at 19:59
  • 2
    Here is the support table for viewport units. http://caniuse.com/#feat=viewport-units – Savas Vedova Feb 03 '16 at 15:00
  • 3
    @LukeP `height: 60%` tries* to set height to 60% of _parent element height_, while `height: 60vw` sets the height to 60% of _viewport width_, so they are quite different. (*Only works if the parent has a set height, as opposed to letting it be however high it needs in order contain it's children, which is usually what you want to do) – ArneHugo Jan 30 '17 at 09:05
38

For this, you will need to utilise JavaScript, or rely on the somewhat supported calc() CSS expression.

window.addEventListener("resize", function(e) {
    var mapElement = document.getElementById("map");
    mapElement.style.height = mapElement.offsetWidth * 1.72;
});

Or using CSS calc (see support here: http://caniuse.com/calc)

#map {
    width: 100%;
    height: calc(100vw * 1.72)
}
Greg
  • 21,235
  • 17
  • 84
  • 107
24
.video {
    width: 100%;
    position: relative;
    padding-bottom: 56.25%; /* ratio 16/9 */
}

.video iframe {
    border: none;
    position: absolute;
    width: 100%;
    height: 100%;
}

16:9

padding-bottom = 9/16 * 100 = 56.25

7

Try viewports

You can use the width data and calculate the height accordingly

This example is for an 150x200px image

width: calc(100vw / 2 - 30px);
height: calc((100vw/2 - 30px) * 1.34);
Stavros
  • 743
  • 8
  • 19
6

You can use CSS like this to crop the image to a certain ratio and then using object-fit to center the image as you want.

CSS:

.ratio-crop {
   aspect-ratio: 1.72 / 1; //using the example size in the question
   object-fit: cover;
}

HTML:

<img src="/images.jpg" class="ratio-crop">
Adriaan Mouton
  • 177
  • 2
  • 3
5

I need to do "fluid" rectangles not squares.... so THANKS to JOPL .... didn't take but a minute....

#map_container {
     position: relative;
     width: 100%;
     padding-bottom: 75%;
}


#map {
    position:absolute;
    width:100%;
    height:100%;
}
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Bill Warren
  • 392
  • 8
  • 11
3

You can set its before and after to force a constant width-to-height ratio

HTML:

<div class="squared"></div>

CSS:

.squared {
  background: #333;
  width: 300px; 
}
.squared::before {
  content: '';
  padding-top: 100%;
  float: left;
}
.squared::after {
  content: '';
  display: block;
  clear: both;
}
Pierre Olivier Tran
  • 1,669
  • 4
  • 24
  • 40
1

Solution with Jquery

$(window).resize(function () {
    var width = $("#map").width();
    $("#map").height(width * 1.72);
});
Anand agrawal
  • 492
  • 6
  • 25
1

I've made similar thing with YouTube's IFRAME where the iframe is inside a grid that always changed based on portrait/landscape so this code worked for:

So the code for this question is:

// Layout resize
let height = window.innerHeight;
let width = window.document.getElementById('player').parentNode.clientWidth;
    height = width / 1.77;
<div id="player"></div>

... etc ..

function onYouTubeIframeAPIReady() {
          // Layout resize
          let height = window.innerHeight;
          let width = window.document.getElementById('player').parentNode.clientWidth;
              height = width / 1.77;

          player = new YT.Player('player', {
            width: '100%',
            height: height,            
            videoId: currentVideoId,
            playerVars: {
              'autoplay': 0,
              'loop': 0,
              'mute': 0,
              'controls': 0,
              'enablejsapi': 1,
              'playsinline': 0,
              'rel': 0,
              'widget_referrer': 'http://my domain ...'
            },
            events: {
              'onReady': onPlayerReady,
              'onStateChange': onPlayerStateChange,
              'onError': onError
            }
          });
        }
Ricky Levi
  • 7,298
  • 1
  • 57
  • 65
0

Let me describe the JS solution as a separate answer:

function handleResize()
{
  var mapElement = document.getElementById("map");
  mapElement.style.height = (mapElement.offsetWidth * 1.72) + "px";
}

<div id="map" onresize="handleResize()">...</div>

(or register the event listener dynamically).

mapElement.style.width * 1.72 will not work, since it requires that the width be set explicitly on the element, either using the width DOM attribute or in the inline style's width CSS property.

Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93
-2
#map { 
    width: 100%; 
    height: 100vw * 1.72 
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Thanh Nhật
  • 777
  • 1
  • 7
  • 14