Set Google Maps Container DIV width and height 100%

CssGoogle MapsGoogle Maps-Api-3Css PositionGoogle Maps-Api-2

Css Problem Overview


I loaded Google Maps API v3 and print Google Map in div. But when set width & height to 100% and auto I can't see the Map.

Here is HTML code snippet.

<!-- Maps Container -->
<div id="map_canvas" style="height:100%;width:100px;margin:0 auto;"></div>

Is there a way to fix this issue?

Css Solutions


Solution 1 - Css

You have to set all parent containers to a 100% width if you want to cover the whole page with it. You have to set an absolute value at width and height for the #content div at the very least.

body, html {
  height: 100%;
  width: 100%;
}

div#content {
  width: 100%; height: 100%;
}

Solution 2 - Css

Setting Map Container to position to relative do the trick. Here is HTML.

<body>
	<!-- Map container -->
	<div id="map_canvas"></div>
</body>

And Simple CSS.

<style>
html, body, #map_canvas {
	width: 100%;
	height: 100%;
	margin: 0;
	padding: 0;
}
#map_canvas {
	position: relative;
}
</style>

Tested on all browsers. Here is the Screenshot.

enter image description here

Solution 3 - Css

Very few people realize the power of css positioning. To set the map to occupy 100% height of it's parent container do following:

> #map_canvas_container {position: relative;} > > #map_canvas {position: absolute; top: 0; right: 0; bottom: 0; left: 0;}

If you have any non absolutely positioned elements inside #map_canvas_container they will set the height of it and the map will take the exact available space.

Solution 4 - Css

If you can't affect your parents elements (like in a nested components situation) you can use height: 100vh which will make it a full window (=view) height;

Solution 5 - Css

This Work for me.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css"> 

#cont{
	position: relative;
	width: 300px;
	height: 300px;
}
#map_canvas{
	overflow: hidden;
	position: absolute;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;
}
</style> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=APIKEY"></script>
<script type="text/javascript">
function initialize() {
    console.log("Initializing...");
  var latlng = new google.maps.LatLng(LAT, LNG);
    var myOptions = {
      zoom: 10,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
}
</script>
</head>

<body onload="initialize()">
<div id="cont">
<div id="map_canvas" style="width: 100%; height: 100%;"></div>
</div>
</body>
</html>

Solution 6 - Css

You can set height to -webkit-fill-available

<!-- Maps Container -->
<div id="map_canvas" style="height:-webkit-fill-available;width:100px;"></div>

Solution 7 - Css

Gmap writes inline style position to relative to the div. Overwrite that with :

google.maps.event.addListener(map, 'tilesloaded', function(){
    document.getElementById('maps').style.position = 'static';
    document.getElementById('maps').style.background = 'none';
});

Hope it helps.

Solution 8 - Css

Better late than never! I made mine a class:

.map
{
	position:absolute;
	top:64px;
	width:1100px;
	height:735px;
	overflow:hidden;
	border:1px solid rgb(211,211,211);
	border-radius:3px;
}

and then

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

Solution 9 - Css

If that div is the only thing on your page, set:

body, html {
  height: 100%;
  width: 100%;
}

Solution 10 - Css

I struggled a lot to find the answer.

You don't really need to do anything with body size. All you need to remove the inline style from the map code:

<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.co.uk/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=new+york&amp;aq=&amp;sll=53.546224,-2.106543&amp;sspn=0.02453,0.084543&amp;ie=UTF8&amp;hq=&amp;hnear=New+York,+United+States&amp;t=m&amp;z=10&amp;iwloc=A&amp;output=embed"></iframe><br /><small><a href="https://maps.google.co.uk/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=new+york&amp;aq=&amp;sll=53.546224,-2.106543&amp;sspn=0.02453,0.084543&amp;ie=UTF8&amp;hq=&amp;hnear=New+York,+United+States&amp;t=m&amp;z=10&amp;iwloc=A" style="color:#0000FF;text-align:left">View Larger Map</a></small>

remove all the inline style and add class or ID and then style it the way you like.

Solution 11 - Css

This worked for me.

map_canvas {position: absolute; top: 0; right: 0; bottom: 0; left: 0;}

Solution 12 - Css

I just added inline style .
<div id="map_canvas" style="width:750px;height:484px;"></div>
And it worked for me .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBBKingView Question on Stackoverflow
Solution 1 - CssMachielView Answer on Stackoverflow
Solution 2 - CssMadan SapkotaView Answer on Stackoverflow
Solution 3 - CssCyberRobotView Answer on Stackoverflow
Solution 4 - CssYoniXwView Answer on Stackoverflow
Solution 5 - CssMostafaView Answer on Stackoverflow
Solution 6 - CssNaresh ChennuriView Answer on Stackoverflow
Solution 7 - CssabhilashvView Answer on Stackoverflow
Solution 8 - Cssdeveloper68View Answer on Stackoverflow
Solution 9 - CssJuan G. HurtadoView Answer on Stackoverflow
Solution 10 - CssAndyView Answer on Stackoverflow
Solution 11 - CssVilas ChavanView Answer on Stackoverflow
Solution 12 - CssNikhil_K_RView Answer on Stackoverflow