I would like to show a leaflet map inside a flex container in order to permit easy resizing of the map as other (initially hidden) elements in the parent are displayed/hidden. Here is what I am doing
- I have a flex container set up with two div elements stacked in it - occupying equal heights by default
- The bottom half contains an image
- The top half contains the actual map which has to be created inside a dynamically added div element.
- There is a third flex div element, infoBox which is hidden initially. The eventual idea being that when
display:flex
is set on infoBox it causes the other contents to shrink down (ideally disappear but it looks like Flexbox does not do that)
Where this idea is failing currently is that I simply cannot get the map to show in the first place. Examining the added imap element in the Chrome developer console reveals that its height is stuck at 0.
This almost certainly has something to do with the fact that I am using a flex container rather than a fixed height one. However, I am unable to find a way around the issue. How can I get this to work as intended?
var imap = document.createElement('div');
imap.id = 'imap';
document.getElementById('mapBox').appendChild(imap);
var lmap = L.map('imap',{center:[51.510067,-0.133869],zoom:10});
#container
{
display:flex;
flex-direction:column-reverse;
min-height:400px;
}
#picBox
{
flex:5;
background-image:url(https://placeimg.com/1000/1000/tech);
background-size:cover;
background-repeat:no-repeat;
background-position:center;
}
#mapBox
{
flex:5;
position:relative;
border:1px solid red;
}
#infoBox
{
flex:20;
border:1px solid blue;
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.css" rel="stylesheet"/>
<div id='container'>
<div id='infoBox'>Stackoverflow is really cool!</div>
<div id='picBox'> </div>
<div id='mapBox'>
</div>
</div>