Bootstrap fails to make Google Maps responsive

I am currently working with bootstrap and have incorporated Google Maps API 3 into my project.

The issue I am facing is that the #map_canvas element does not adjust responsively; it has a fixed width.

Additionally, when I try to use height: auto and width: auto, the map fails to display on the page.

Can someone please explain why this may be happening?

<style type="text/css">
  body {
    padding-top: 60px;
    padding-bottom: 40px;
  }
#map_canvas {
   height: 400px;
    width: auto;
  }
</style>

<div class="container">
 <div class="row">
  <div class="span6">
   <div id="map_canvas"></div>
  </div>
 </div>
</div>

Answer №1

UPDATED: The original post is no longer accurate, so I have revised my response and enhanced the code.

This approach does not rely on bootstrap or any other frameworks. It can stand alone to create responsive content. A padding based on the aspect ratio is applied to the parent element. Subsequently, the child element is positioned using absolute positioning.

Here is the HTML:

<div class="iframe-container">
    <!-- insert embed code here -->
</div>

The CSS:

.iframe-container{
    position: relative;
    width: 100%;
    padding-bottom: 56.25%; /* Ratio 16:9 ( 100%/16*9 = 56.25% ) */
}
.iframe-container > *{
    display: block;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
}

The provided 'fiddle' showcases how to create:

  • Responsive Google Map
  • Responsive OpenStreetMap
  • Responsive Vimeo Video
  • Responsive Youtube Video

Demo: http://jsfiddle.net/LHQQZ/135/

Answer №2

Utilizing Bootstrap's Responsive Embed functionality, this example showcases a fixed aspect ratio of 16/9:

<div class="embed-responsive embed-responsive-16by9">
    <div id="map_canvas" class="embed-responsive-item" style="border: 1px solid black"></div>
</div>

Answer №3

Super easy. Just adjust the map size based on the viewport height with CSS:

.map {
  height: 80vh;
}

This code sets the .map container to be 80% of the viewport height.

Answer №5

If someone looks at this, they might encounter a similar issue.

I faced a problem with an iframe containing an OpenStreet map that wasn't responsive. I solved it by adding the "img-responsive" class and everything worked perfectly.

<iframe class="img-responsive" width="350" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="YOUR OPENSTREET URL HERE" style="border: 1px solid black"></iframe>

Answer №6

September 2021

It has been eight years since the question was first asked.
If you are still searching for the solution and have not found it yet, here it is.

Bootstrap 5 (tested and verified)

To make Google Map responsive, simply add width="100%" to the iframe tag.

<div class="row">
    <div class="col-6">
        <iframe src="SOURCE" width="100%" OTHER_ATTRIBUTES></iframe>
    </div>
</div>

Answer №7

If you're utilizing the javascript api to embed (instead of using an iframe):

<div class="customMapContainer">
  <div id="customMap"></div>
</div>
.customMapContainer {
    position: relative;
    &:after {
      content: "";
      display: block;
      padding-bottom: 100%;
    }
}

#customMap {
  position: absolute;
  height: 100%;
  width: 100%;
  right: 0;
  overflow: hidden;
}

Answer №8

To set the dimensions of the span6 class, specify its width and height. Next, assign a width and height of 100% to the map_canvas.

Answer №9

Smartik's solution proved to be effective. I have incorporated the changes for the gmaps4rails gem into the CSS as shown below:

.map_container {
padding: 6px;
border-width: 1px;
border-style: solid;
border-color: #ccc #ccc #999 #ccc;
-webkit-box-shadow: rgba(64, 64, 64, 0.5) 0 2px 5px;
-moz-box-shadow: rgba(64, 64, 64, 0.5) 0 2px 5px;
box-shadow: rgba(64, 64, 64, 0.1) 0 2px 5px;
display: block;
}

.gmaps4rails_map {
  display: block;
  height: 400px;
}

Subsequently, these lines were added to the view file:

<div class="map_container">
  <div id="map" class="gmaps4rails_map">
    <div id="map_canvas" class="span9">
      <%= gmaps({"map_options" => { "type" => "ROADMAP", "center_longitude" => 180,"auto_zoom" => false, "zoom" => 16, "auto_adjust" => true}, "markers" => { "data" => @json }})%>
    </div>
  </div> 
</div>

Answer №10

<div class="location_map_container">
    <div id="location_map" class="custom_map_container">
        <div id="map_canvas_location" class="full_width_map">
            <%= customMap({"map_settings" => { "type" => "SATELLITE", "center_latitude" => 90,"auto_zoom" => true, "zoom_level" => 14, "auto_adjustment" => false}, "pins" => { "pin_data" => @locations }})%>
        </div>
    </div> 

Answer №11

Here is a fantastic tutorial offering a smart solution.

To make your map responsive, simply add a div tag around the embed code with the CSS class map-responsive like so:

<div class="map-responsive">
    <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d386950.6511603643!2d-73.70231446529533!3d40.738882125234106!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x89c24fa5d33f083b%3A0xc80b8f06e177fe62!2sNueva+York!5e0!3m2!1ses-419!2sus!4v1445032011908" width="600" height="450" frameborder="0" style="border:0" allowfullscreen></iframe>
</div>

To ensure proper display, include these CSS properties in one of your stylesheet files:

.map-responsive{
    overflow:hidden;
    padding-bottom:56.25%;
    position:relative;
    height:0;
}
.map-responsive iframe{
    left:0;
    top:0;
    height:100%;
    width:100%;
    position:absolute;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the jQuery Mobile DOM element to return 'undefined' when using .val()?

Currently, I am experimenting with Jquery Mobile and facing an issue where my Form elements are returning 'undefined'. I suspect that the problem lies in the fact that my form may not be created when the onclick() function is triggered by the Mem ...

Guidance on transferring servlet variables to an HTML page through redirection in Java

I am looking for a way to open an HTML page from my Java servlet and utilize the variables of that servlet to populate the form fields in the HTML page when it is opened. Below is the code snippet of my servlet: import java.io.*; import java.sql.*; impor ...

Creating a corner ribbon for a Material UI Card: A step-by-step guide

Can anyone provide pointers on incorporating a corner ribbon to a Material-UI card? Currently, I am utilizing makeStyles for styling from Material UI. ...

Inject a dynamic URL parameter into an iframe without the need for server-side scripting

I'm really stuck and could use some assistance with the following issue, as I am unable to solve it on my own :( When a user is redirected to a form (provided via an iframe), there is a dynamic URL involved: website.com/form?id=123 The code resp ...

Creating a table in HTML using the innerHTML property

document.getElementById("outputDiv").innerHTML = ""; document.getElementById("outputDiv").innerHTML += "<table border=1 width=100%><tr>"; for(j=1;j<=10;j++) { document.getElementById("outputDiv").innerHTML += "<td align=center>"+St ...

Navigating from an offspring link to a social media platform page

I just launched my website and noticed that some sites and ads redirect users to their social media pages directly from the site. For example: mysite.com/facebook redirects to facebook.com/mysitesfbpage mysite.com/twitter redirects to twitter.com/mysite ...

Why is it that using div with a 100% width doesn't behave as one would anticipate?

Learning CSS has been an interesting journey so far. It's not always as intuitive as one might think, especially in web development. :) I recently attempted to create a simple, static progress bar using the following HTML file: <html> <he ...

In HTML, specify that the height of both the `html` and

I am facing an issue with the container-about div. I have set its height to 100% so that the div occupies the entire width and height after the header div. The problem is that currently, I cannot scroll to view the full text. It would be great if there wa ...

What is the best way to display a single instance of a React component that is declared in multiple locations within the app?

Imagine I have 2 main components, A and B. Now, component C needs to be created inside both A and B. How can I guarantee that the same exact instance of C is generated in both places? Essentially, I want them to stay synchronized - any changes made to one ...

Boundaries on Maps: A guide to verifying addresses within a boundary

User provides address on the website. If the address falls within the defined boundary, it is marked as "Eligible". If outside the boundary, labeled as "Ineligible". Are there any existing widgets or code snippets available to achieve this functio ...

Ways to include Bootstrap Tooltips on an input field that has the type of "file"

I attempted the code below: <input type="file" name="file" id="fileField" data-toggle="tooltip" data-placement="bottom"/> <script> $(document).ready(function() { $('[data-toggle="tooltip"]').tooltip(); }); </script> ...

Adjusting CSS to realign images on various screen types

I am facing an issue with displaying a background image on a static web page. The height appears differently on various LCD screens. How can I ensure that the height adjusts to approximately 80% of the area? I have tried giving a percentage in the style fo ...

Hamburger icon is failing to appear in the collapsed navbar for certain items

My React application is not displaying the items of the collapsed navbar when using the Bootstrap navbar for mobile responsiveness. Can anyone please suggest what might be wrong here? <!-- Bootstrap 4.1.3 library --> <link rel="stylesheet" href ...

Having trouble vertically centering a div within a grid using bootstrap

How can I vertically center the card with the other content in the right div? https://i.sstatic.net/FSPmU.png This is my code: le="margin-bottom:100px"> I Agree With the terms and service Request Code <link href="https://stackp ...

The items in the Bootstrap dropdown are not displaying

I am currently working on a project using Angular 12, and I encountered an issue with my Bootstrap dropdown menu not displaying any items. Below is the HTML code snippet causing the problem: <nav class="navbar navbar-expand navbar-dark"> ...

Deactivate additional fields when choosing an option from the drop-down selection menu

When designing a form with a select dropdown that displays various options, I encountered an issue. I want to disable certain fields if a specific option is chosen from the dropdown. For instance, if "Within Company" is selected in the transaction type, I ...

Is it possible to apply a margin to <details><summary> elements?

I am struggling with adding a margin to each of the children in my nested <details> elements .container { margin: 20px; } .parent, .child { outline: none; border: none; user-select: none; cursor: pointer; } <div class="container"> ...

Insert data into a drop-down menu and organize it

When I choose a value in one Select, the other options are removed and the previous value is added to them. How can I use JQuery to add the previous value to Select and then sort it alphabetically? ...

The pop-up dialog on Facebook is limited to the width of the like button, cutting off any excess information

Feel free to check out my blog here. When attempting to click the "LIKE" button, the pop-up appears at the correct height, but the width is limited to the size of the button. Below is the CSS code I am using: .blog_social_media { float: right; b ...

The issue of jQuery's .last() function triggering multiple times with just a single click on the last

I currently have a set of tabs containing radio buttons and I need to trigger an event when the last option is selected (meaning any radio button within the final tab, not just the last radio button). Below is the HTML code: <div id="smartwizard" clas ...