Difficulty encountered when trying to show a leaflet map on compact screens within an HTML/CSS design

I'm encountering a problem with my HTML/CSS layout in which the map element (#map) is not appearing on smaller screens, while the map settings (map-settings) are visible. Below is a simplified version of my code:

<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <!-- Meta tags and title -->
</head>
<body>
  <div class="content-wrapper">
    <div class="content-wrapper-inner">
      <div class="main-content">
        <div id="map"></div>
        <div class="map-settings" id="map-settings">
          <!-- Map settings content -->
        </div>
      </div>
      <div class="sidebar-content">
        <!-- Sidebar content -->
      </div>
    </div>
  </div>
</body>
</html>


CSS:


/* index_page.css */

/* Ensure the map takes up full height of its container */
#map {
  flex: 1;
  width: 100%;
  height: 100%; /* Add this line to ensure the map fills its container's height */
}

/* Ensure the map settings remain visible on smaller screens */
.map-settings {
  background: white;
  padding: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  margin-bottom: 20px; /* Adjust margin to create space between map and settings */
  z-index: 1000;
}

/* Ensure proper layout on smaller screens */
@media (max-width: 768px) {
  .content-wrapper-inner {
    flex-direction: column;
  }

  .main-content {
    width: 100%;
  }

  #map {
    flex: 1;
    height: 300px; /* Set a fixed height for the map on smaller screens */
  }

  .sidebar-content {
    width: 100%;
  }
}


Upon resizing the screen width, I observed that the map disappears while the map settings remain visible. Despite trying to adjust CSS properties and media queries, the map still fails to display properly on smaller screens.

For desktop screens, it appears like this:

https://i.sstatic.net/kEfNcXgb.png

And for mobile screens, the display looks like this:

https://i.sstatic.net/wTGlq5Y8.png

Only the map settings are shown on mobile devices, with the actual map missing from view.

Have already tried adjusting the overflow property without success.

The map uses leaflet, in case that helps in finding a solution.

If anyone could shed light on why the map isn't showing on smaller screens and offer possible remedies, it would be greatly appreciated. Thank you!

Feel free to request further details if needed.

Answer №1

After testing your code in a sandbox, I noticed that it functions correctly on mobile devices but not on desktops, which is the opposite of the issue you described.

The problem lies in the fact that the map should fill its container, yet the container (.main-content) lacks height specifications. As a result, the display is distorted on desktop screens while appearing correctly on mobile devices due to the defined height of 300px. To resolve this, it's essential to ensure .main-content has a height by utilizing flex properties and setting an appropriate aspect-ratio (alternatively, assigning a fixed height).

You can implement something like the following:

var map = L.map('map').setView([51.505, -0.09], 13);

L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
/* index_page.css */

.content-wrapper-inner{
  display: flex;
}
/* Ensure the map takes up full height of its container */
#map {
  width: 100%;
  height: 100%; /* Add this line to ensure the map fills its container's height */
}
.sidebar-content {
  flex: 1;
}
.main-content {
  flex: 3;
  aspect-ratio: 1;
}

/* Ensure the map settings remain visible on smaller screens */
.map-settings {
  background: white;
  padding: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  margin-bottom: 20px; /* Adjust margin to create space between map and settings */
  z-index: 1000;
}

/* Ensure proper layout on smaller screens */
@media (max-width: 768px) {
  .content-wrapper-inner {
    flex-direction: column;
  }
  .main-content {
    aspect-ratio: auto;
  }

  #map {
    height: 300px; /* Set a fixed height for the map on smaller screens */
  }
}
<div class="content-wrapper">
    <div class="content-wrapper-inner">
      <div class="main-content">
        <div id="map"></div>
        <div class="map-settings" id="map-settings">
          map settings
        </div>
      </div>
      <div class="sidebar-content">
        sidebar
      </div>
    </div>
  </div>

<link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e28e8783848e8796a2d3ccdbccd6">[email protected]</a>/dist/leaflet.css" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin="" />
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d61686c6b6168794d3c23342339">[email protected]</a>/dist/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>

Answer №2

One key issue to address is that the #map element lacks a defined height for smaller screens, resulting in it collapsing and not displaying correctly.

To rectify this issue, below is an updated CSS snippet that guarantees the #map element has a specified height on smaller screens:

#map {
  flex: 1;
  width: 100%;
  height: 100%; 
}

@media (max-width: 768px) {
  .content-wrapper-inner {
    flex-direction: column;
  }

  .main-content {
    width: 100%;
  }

  #map {
    flex: 1;
    height: 300px; 
  }

  .sidebar-content {
    width: 100%;
  }
}

Prior to this adjustment, the #map element would vanish on smaller screens due to the absence of a designated height. By introducing a fixed height (height: 300px;) for the #map within the media query targeting screens with a maximum width of 768px, the map is now guaranteed to maintain a visible height on smaller devices.

I trust this information proves beneficial.

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

Following the execution of npm install, I encountered an issue stating: "set-value has encountered Prototype

I've encountered a problem with my React Native app while running npm install. The error message reads, "found 15 vulnerabilities (5 moderate, 10 high) in 1182 scanned packages." Upon further analysis using npm audit, it seems that the majority of err ...

Is there a way to align my heading with my paragraph?

I am experiencing an issue with my CSS styling. The content following the top navigation bar is not aligning properly. I utilized the float: left and right properties in my code and then added a clearfix to align the next header, but unfortunately, it is n ...

Modifying CSS Styles using JQuery

I've been attempting to use JQuery to change a CSS property without success, despite trying multiple solutions found on SO. Here's what I've attempted (with numberHours being a previously defined variable): $(".cd-schedule .events .top-inf ...

choosing between different options within Angular reactive forms

I am attempting to create a select element with one option for each item in my classes array. Here is the TypeScript file: @Component({ selector: 'app-create-deck', templateUrl: './create-deck.component.html', styleUrls: [' ...

Initiating a server call to fetch data using jQuery's AJAX function

I need to create a simple AJAX request to check if a username already exists in the database. The challenge lies in the JavaScript function that sends the request: function usernameCheck(){ $.post("http://example.com/signup", {username: $("#username"). ...

Exporting two functions in JavaScript

Currently utilizing React, Redux, and experimenting with Material-UI integration. The example codes provided by Redux and Material-UI libraries include an 'export' statement at the end. Redux: export default connect(mapStateToProps, actions)(my ...

What is the process for designing custom width columns using Bootstrap?

I am working on a table with three columns that is styled using the Bootstrap class "table table-striped". However, I need the first column to be 100px in width, the second column to be 400px, and the third column to be 200px. How can I achieve this cust ...

Creating a JavaScript function that calculates the sum of values in a table

Here is a snippet of my HTML code: <TR Row="1" CLASS="ThemeAlignCenter"> <TD id="wrdActive_Row1" style="width: 50px" CLASS="rdThemeDataTableCell"><SPAN id="lblactive_Row1">6</SPAN></TD> .. ...

Incorporating an else statement into a function that handles AJAX calls upon receiving a response

My code is almost perfect, but there's just one issue. Whenever an invalid email is entered, the else statement in the PHP response makes it look like the form was still successful. How can I modify my current code to display the appropriate error mes ...

ExpressJS Template Caching System

Encountering issues with template caching in my MEAN app. The navigation bar uses conditional logic to show/hide buttons based on user status. Upon page load, values are null or false until login (views, isLoggedIn). The problem arises post-login - despit ...

Conceal a div once the user scrolls to a certain position using vanilla JavaScript

As a beginner in the field of web development, I am currently working on creating a blog website. Code Function - One of the challenges I'm facing is implementing a floating pagination bar that needs to be hidden when a visitor scrolls near the foote ...

Create a jQuery popup form with a variety of interactive buttons

Is it possible to implement a modal form in jQuery that can be triggered by multiple buttons? All the buttons have identical names and IDs. The goal is to display the modal form whenever any button is clicked. For this example, let's consider having ...

Dynamic extension of classes in SCSSORExtending classes

When working with Vue, I encountered a situation similar to :style="{ [`--chip-bg-hover`]: hoverColorValue, [`--chip-bg-active`]: activeColor, [`--chip-border-hover`]: borderHoverColorValue, }" Then, i ...

Distinct vertical menus with varying widths but sharing the same CSS code for the submenus

How can I create a vertical menu bar with submenus on the right side? The first submenu appears correctly, but the second one is narrower than the first. It seems like they have the same code, yet they look different. Below is the HTML code: <div clas ...

Is it redundant to use flatMap in RXJS?

I recently came across an enlightening article on RXJS which delves into the concept of flatMap. After understanding its purpose - to flatten observable of observables into a single observable sequence (similar to SelectMany in C#) - I noticed an interes ...

What causes the height and width properties in a div to remain unchanged even after using the zoom/scale CSS?

I'm trying to center a CSS spinner on the page, but I am struggling with making it happen. Even when scaled down by 50%, the height and width remain at 200px. <div class="center" style=""> <div class="uil-default-css" style="width: 200px ...

Tooltips envelope around themselves

Several paragraphs contain tooltips, but some of them break onto a new line instead of staying within the text. <div class="row"> ... </div> Fiddle: https://jsfiddle.net/LhjmLw8k/29/ The issue is that certain words like "Trasimeno" and ...

Is it possible to retrieve the `arguments` objects if one of the parameters is named "arguments"?

This code snippet will output 1: (function (params) { console.log(params); }(1, 2)); The params object has replaced the default arguments. Can we retrieve the original arguments object within the function's scope? ...

How to effectively manipulate nested arrays in JavaScript without altering their references

Welcome everyone, I've been working on a project using next.js and I've encountered an issue with filtering posts. The filter function works perfectly when the posts are in a simple array like ObjChild. However, I have another section on my site ...

What could be causing my screen reader to repeat lines?

I have this HTML structure: <button ng-disabled="vm.updating" ng-click="doSomething()" class="something" type="submit" aria-label="average score"> <span ng hide="hideConditional()" class="font-white">score</span> <span ng-show= ...