41

In my Application I was making div of map as

<div id="map" style="height: 610px; width:100%"></div>

but to make my map responsive I want to make height also 100%, if I make height: 100% then it is not working.

How can I make height also variable like width so that map can be seen properly on any device.

Demo : http://jsfiddle.net/CcYp6/

If you change height & width of map then you will not get map.

vaibhav shah
  • 4,939
  • 19
  • 58
  • 96

4 Answers4

41

You need to set the parent elements to height: 100%; first

html, body {
   height: 100%;
}

Demo

Demo (This won't work as no parent height is defined)

Explanation: Why do you need to do that? So when you specify an element's height in % then the 1st question that arises is: 100% of what? By default, a div has height of 0px, so 100% for a div simply won't work, but setting the parent elements height to 100%; will work.

magi182
  • 3,427
  • 1
  • 15
  • 23
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
29

You have to set the div size with JavaScript.

$("#map").height($(window).height()).width($(window).width());
map.invalidateSize();

You can find a complete example here.

jgillich
  • 71,459
  • 6
  • 57
  • 85
18

Use height="100vh" works in newer browser. but its ok.

user8207609
  • 181
  • 1
  • 2
2

Put a position: relative wrapper parent around it, then position your map absolutely to fill that parent:

.map-wrapper {
  position: relative;
}

#map {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

<div class="map-wrapper">
  <div id="map"></div>
</div>

If #map already has a parent that you can position relatively, just use that; you won't need the .map-wrapper element.

Leaflet Quick Start Guide could be clearer on this point as it's a common use case. The mobile tutorial hints at it.

MSC
  • 3,286
  • 5
  • 29
  • 47