DEV Community

Deepak Devanand
Deepak Devanand

Posted on

Leaflet Map - Dark Theme

Leaflet.js is arguably the best client-side map-rendering library. While there's a plethora of OpenStreet Map tile providers, none of the free basemap layer have a dark-themed map (tiles).

By applying css filter property on specific leaflet classes, we can have a "hacky" dark map.

.leaflet-layer,
.leaflet-control-zoom-in,
.leaflet-control-zoom-out,
.leaflet-control-attribution {
  filter: invert(100%) hue-rotate(180deg) brightness(95%) contrast(90%);
}
Enter fullscreen mode Exit fullscreen mode
  • invert(100%) completely inverts the color, essentially adding dark background to the map.
  • hue-rotate(180deg) is optional, but converts the predominant blue color (large roads) on the map to red.

Top comments (5)

Collapse
 
ozgurg profile image
Özgür

Thank you. Additionally, setting the background color of the container makes its background dark as well, which is noticeable when panning (the light background color is visible while panning).

Image description

.leaflet-container {
    background: #000
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
anthonylzq profile image
Anthony Luzquiños

This is great, thanks!

Collapse
 
miralo profile image
Michele Mirandola

Great! Thanks for this snippet! :-)

Collapse
 
jamesvanderpump profile image
James Vanderpump

Thanks for sharing, this is so cool!

Collapse
 
falselight profile image
YURII DE.

Thank you ;-) quickly solution