0

I try to make a leaflet map and added Openstreetmap as a background map. But if I load the map, nothing appears. Checking the firefox network analysis all the required tiles are loaded succesfully, but they do not show.

My HTML code :

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">

    <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin=""/>
    <script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha512-QVftwZFqvtRNi0ZyCtsznlKSWOStnDORoefr1enyq5mVL4tmKB3S/EnC3rRJcxCPavG10IcrVGSmPh6Qw5lwrg==" crossorigin=""></script>

    <link rel="stylesheet" href="main.css">

    <title>title</title>
</head>
<body>
    <div id="map">
    </div>
    <div id="logo">
        <!--- a logo will appear here --->
    </div>
    <script src="map.js"></script>
</body>

My javascript code :

var map = L.map('map', {
    center: [51.505, -0.09],
    zoom: 13
});

var osm = new L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
});
osm.addTo(map);

The div "map" has a higher z-index than "logo", so that should not be the problem.

1 Answer 1

2

Where is map style ?

You should give a height and a width to your map in order to be rendered

<div id="map" style="width: 600px; height: 400px;">

Demo

2
  • I used "#map { width: 100%; height: 100%; padding: 0; margin: 0; z-index: 1; }" in the .css file, but that does not seem to work, since your code fixes it. Any idea what I did wrong?
    – gHupf
    Commented Mar 1, 2019 at 11:44
  • 1
    height: 100% makes no sense in this case as percentage refers to the parent DOM element. 100% of the style that the parent dom element has been assigned. In your case there is no parent element for the <div id="map"> with predefined style and height in that case. Therefore you need to use specific px size. Please accept and or up-vote the answer if it helped you.
    – kboul
    Commented Mar 1, 2019 at 11:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.