I want to place the zoom control in middle right of the the map i.e. in the middle of the right most side of the map. I have found solution to put the zoom control in different corners using the following code
var map = new L.map("map-container",{ zoomControl: false });
new L.Control.Zoom({ position: 'topleft' }).addTo(map);
So the positions can be
topleft
topright
bottomleft
bottomright
But my goal is to put the control window in the middle right. Or even I put the control in the corner I want to add some margin to the top. How can I do that? Is there any idea?
We can create additional Control placeholder(s), besides the 4 provided corners by default.
A nice advantage is that it allows putting several Controls in one of those placeholders. They will stack without overlapping, as in the standard corners.
JavaScript:
// Create additional Control placeholders
function addControlPlaceholders(map) {
var corners = map._controlCorners,
l = 'leaflet-',
container = map._controlContainer;
function createCorner(vSide, hSide) {
var className = l + vSide + ' ' + l + hSide;
corners[vSide + hSide] = L.DomUtil.create('div', className, container);
}
createCorner('verticalcenter', 'left');
createCorner('verticalcenter', 'right');
}
addControlPlaceholders(map);
// Change the position of the Zoom Control to a newly created placeholder.
map.zoomControl.setPosition('verticalcenterright');
// You can also put other controls in the same placeholder.
L.control.scale({position: 'verticalcenterright'}).addTo(map);
Then it becomes easy styling those placeholders with CSS, because their DOM parent is the map container itself. Hence top
, bottom
, left
and right
can be specified with percentages (which use the parent's dimensions).
CSS:
.leaflet-verticalcenter {
position: absolute;
z-index: 1000;
pointer-events: none;
top: 50%; /* possible because the placeholder's parent is the map */
transform: translateY(-50%); /* using the CSS3 Transform technique */
padding-top: 10px;
}
.leaflet-verticalcenter .leaflet-control {
margin-bottom: 10px;
}
As for vertical centering the placeholder itself, you can use your favourite technique. Here I used the CSS3 Transform to offset the placeholder by half of its own height.
If necessary (e.g. for old browsers compatibility), you can rather use a "calculate-on-load" method to perform this offset, similar to iH8's answer. But you no longer need to run it on map resize, only when adding new Control(s) to the placeholder.
Live demo: https://plnkr.co/edit/bHJwfm598d1Ps7MpLG0k?p=preview
Note: there is currently an open PR (Leaflet/Leaflet #5554) for this, but since it is not compatible with old versions of Internet Explorer, it will not likely be merged in Leaflet core.