positioning a mapbox/leaflet map inside a container div

后端 未结 2 740
猫巷女王i
猫巷女王i 2021-02-08 14:55

I am working with Mapbox in a Ruby on Rails app. For the life of me, I cannot get the map to adhere to any simple CSS. The only CSS that allows the map to appear is giving it an

2条回答
  •  鱼传尺愫
    2021-02-08 15:11

    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.

提交回复
热议问题