Atlaslog
Atlaslog / Field Notes / Loading & display
Field note · Loading & display

Map misaligned inside a modal, drawer or tab

Quick answer

A map initialised while display: none gives the library a 0x0 container, so only the top-left corner is drawn later. Dragging or zooming sometimes repairs it because those gestures force an internal size recalculation.

Last verified 2026-11-03 · Multiple versions · Cite this page
Applies to: Leaflet · OpenLayers · Cesium
Symptom
Checklist
  1. Determine whether the container was hidden at the moment of initialisation.
  2. If so, create the map after the modal transition ends rather than on page load.
  3. If initialisation must happen early, call invalidateSize() or updateSize() when the container becomes visible.
  4. Attach a debounced ResizeObserver for layouts that change size dynamically.
  5. For Cesium, call viewer.resize() or viewer.scene.requestRender(), especially with requestRenderMode enabled.
Fix

Initialise once the container is visible, or call map.invalidateSize() in Leaflet / map.updateSize() in OpenLayers on show; debounce a ResizeObserver so layout-driven size changes are handled centrally.

Code
function openDialog() {
  dialog.style.display = 'block';
  requestAnimationFrame(function () {
    if (!map) {
      map = L.map('map', { center: [39.9, 116.4], zoom: 12 });
      L.tileLayer(url).addTo(map);
    }
  });
}
// Leaflet
map.invalidateSize();

// OpenLayers
map.updateSize();
Watch out
All 120 field notesChinese originalRunnable examples
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Cite this page

Atlaslog (2026). Map misaligned inside a modal, drawer or tab. https://www.jingweizhi.com/en/cookbook/map-in-modal.html (accessed 2026-11-03).