Resolving Google Maps Container DIV Sizing Issues with 100% Dimensions
When embedding a Google Map into a web page, it can be challenging to achieve a container DIV that fully occupies the available space. This issue arises when users attempt to set both the width and height properties of the container DIV to 100%, resulting in an invisible map.
原因:
To display a map within the DIV, Google Maps requires a finite amount of space. Assigning dimensions of 100% instructs the DIV to conform to the dimensions of its parent container. If the parent container itself is not explicitly set to occupy the entire page, the DIV will be scaled down to a smaller size, rendering the map invisible.
解决方案:
To resolve this issue, it is necessary to establish a chain of parent containers that all have a width of 100%. This ensures that the container DIV for the map has sufficient space to expand and fully display its contents. Additionally, assign a numerical value (e.g., 700px) to the height property of the map's container DIV in order to provide a minimum height for the map.
示例代码:
<code class="css">body, html { height: 100%; width: 100%; } div#content { width: 100%; height: 100vh; } div#map_canvas { height: 700px; width: 100%; }</code>
By setting the body, html, and content DIV to 100% width and height, you create a container that covers the entire page. The map_canvas DIV then fills the content DIV while maintaining a height of 700px. This approach ensures that the map is always visible and occupies the intended space.
The above is the detailed content of Why Does My Google Map Not Appear When I Set the Container Div to 100% Width and Height?. For more information, please follow other related articles on the PHP Chinese website!