positioning a mapbox/leaflet map inside a container div

。_饼干妹妹 提交于 2019-12-05 12:31:10

The only css rule that must be set for Leaflet's (Mapbox is an extended version of Leaflet) element when positioned static/relative is an absolute height:

#map {
    height: 200px;
}

Example: http://plnkr.co/edit/vdeyLv?p=preview

That will work always, doesn't matter how deep the element is nested. When width is not set it will use all the width available. When you want to switch to setting height in percentages, you'll need to make sure that all the anchestor elements of the map element have a height set also:

#html, #body, #map-container {
    height: 100%;
}

#map {
    height: 40%;
}

Example: http://plnkr.co/edit/rAuNC0?p=preview

I guess the reasoning behind using absolute positioning in the Mapbox examples is that one does not have to explain the above because no matter how deep you nest the map's element, it just works.

Just change the 'position:absolute' to 'position:relative' for both divs:

<div id="map-container">
  <div id="map">
  </div>
</div>

#map-container {
    position: relative;
    height: 180px;
    width: 600px;
}

#map {
    position: relative;
    height: inherit;
    width: inherit;
}

Note: The height and width on the '#map' element will be inherited by the parent div '#map-container', which will be '180px' and '600px' respectively in this case or whatever you set.

DEMO: http://plnkr.co/edit/qjIAiud3aUF11iQPDKj0?p=preview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!