Essentials · Angular, React and OpenLayers
View padding for content over the map
Offset an OpenLayers view optical center permanently with the view padding option so fit, setCenter, zoom controls and rotation respect UI overlays.

What this example shows
Southern Poland opens at zoom 6 with translucent dark bands covering 170 pixels at the top, 50 on the right, 30 at the bottom and 150 on the left, while zoom buttons are shifted inward to the top-left corner of the clear window. Unlike per-call fit padding, the camera’s permanent optical centre sits over Kraków right in the middle of the uncovered rectangle.
Fitting geometries without extra arguments, calling setCenter, clicking zoom buttons or rotating with Alt+Shift and drag all pivot naturally around that offset centre.
When you need it
Geoportals, fleet consoles and property explorers with persistent headers, sidebars or bottom sheets need the entire camera system to treat the uncovered sub-window as the true centre of the map.
Configuring [padding]="padding" once on the view eliminates repetitive inset arguments on every camera call and fixes wheel/button zoom and rotation pivots while still rendering seamless tiles underneath translucent UI panels.
The code
The example uses cc-openlayers, my Angular component library for OpenLayers: each aol-… element creates the OpenLayers object it names, updates it from its inputs and disposes of it with the component.
view-padding.tsimport { ChangeDetectionStrategy, Component, viewChild } from '@angular/core';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceGeoJSONComponent,
SourceOsmComponent,
StyleComponent,
StyleFillComponent,
StyleStrokeComponent,
ViewComponent,
} from 'cc-openlayers';
import type SimpleGeometry from 'ol/geom/SimpleGeometry';
import { fromLonLat } from 'ol/proj';
/** A view whose padding keeps its centre and its fitted extents clear of the bands that stand for content over the map's edges. */
@Component({
selector: 'app-view-padding',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceGeoJSONComponent,
SourceOsmComponent,
StyleComponent,
StyleFillComponent,
StyleStrokeComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<div class="padded-map" [style]="edges">
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<aol-view [zoom]="6" [padding]="padding">
<aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
</aol-view>
<aol-layer-tile><aol-source-osm /></aol-layer-tile>
<aol-layer-vector>
<aol-style>
<aol-style-fill color="rgba(255, 255, 255, 0.6)" />
<aol-style-stroke color="#319FD3" [width]="1" />
</aol-style>
<aol-source-geojson url="assets/poland/poland.geojson" featureProjection="EPSG:3857" />
</aol-layer-vector>
</aol-map>
<div class="padding-band top"></div>
<div class="padding-band right"></div>
<div class="padding-band bottom"></div>
<div class="padding-band left"></div>
</div>
<div class="example-controls">
<button type="button" (click)="zoomToPoland()">Zoom to Poland</button>
<button type="button" (click)="centerOnKrakow()">Center on Kraków</button>
<p class="example-hint">
The bands stand for content over the map's edges: the view's padding of 170 px at the top, 50 px on the right,
30 px at the bottom and 150 px on the left keeps its centre and its fitted extents clear of them. Hold Alt+Shift
and drag to rotate around the shifted centre.
</p>
</div>
`,
})
export class ViewPadding {
protected readonly padding = [170, 50, 30, 150];
/** The padding as the sizes of the bands that stand for content over the map's edges. */
protected readonly edges = {
'--top': `${this.padding[0]}px`,
'--right': `${this.padding[1]}px`,
'--bottom': `${this.padding[2]}px`,
'--left': `${this.padding[3]}px`,
};
private readonly view = viewChild.required(ViewComponent);
private readonly source = viewChild.required(SourceGeoJSONComponent);
protected zoomToPoland(): void {
const geometry = this.source().instance.getFeatures()[0]?.getGeometry();
if (geometry) this.view().instance.fit(geometry as SimpleGeometry);
}
protected centerOnKrakow(): void {
this.view().instance.setCenter(fromLonLat([19.9368, 50.0614]));
}
} The React version uses the library's React adapter, cc-openlayers/react: each component creates the OpenLayers object it names when it mounts, updates it from its props and disposes of it when it unmounts.
view-padding.react.tsximport { useRef, type CSSProperties } from 'react';
import type OLView from 'ol/View.js';
import type OLVectorSource from 'ol/source/Vector.js';
import type SimpleGeometry from 'ol/geom/SimpleGeometry.js';
import { fromLonLat } from 'ol/proj.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
Fill,
GeoJSONSource,
Map,
OSMSource,
Stroke,
Style,
TileLayer,
VectorLayer,
View,
} from 'cc-openlayers/react';
const padding = [170, 50, 30, 150];
/** The padding as the sizes of the bands that stand for content over the map's edges. */
const edges = {
'--top': `${padding[0]}px`,
'--right': `${padding[1]}px`,
'--bottom': `${padding[2]}px`,
'--left': `${padding[3]}px`,
} as CSSProperties;
/** A view whose padding keeps its centre and its fitted extents clear of the bands that stand for content over the map's edges. */
export function ViewPadding() {
const view = useRef<OLView>(null);
const source = useRef<OLVectorSource>(null);
const zoomToPoland = () => {
const geometry = source.current?.getFeatures()[0]?.getGeometry();
if (geometry) view.current?.fit(geometry as SimpleGeometry);
};
const centerOnKrakow = () => {
view.current?.setCenter(fromLonLat([19.9368, 50.0614]));
};
return (
<>
<div className="padded-map" style={edges}>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View ref={view} zoom={6} padding={padding}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<VectorLayer>
<Style>
<Fill color="rgba(255, 255, 255, 0.6)" />
<Stroke color="#319FD3" width={1} />
</Style>
<GeoJSONSource ref={source} url="assets/poland/poland.geojson" featureProjection="EPSG:3857" />
</VectorLayer>
</Map>
<div className="padding-band top"></div>
<div className="padding-band right"></div>
<div className="padding-band bottom"></div>
<div className="padding-band left"></div>
</div>
<div className="example-controls">
<button type="button" onClick={zoomToPoland}>
Zoom to Poland
</button>
<button type="button" onClick={centerOnKrakow}>
Center on Kraków
</button>
<p className="example-hint">
The bands stand for content over the map's edges: the view's padding of 170 px at the top, 50 px on the right,
30 px at the bottom and 150 px on the left keeps its centre and its fitted extents clear of them. Hold
Alt+Shift and drag to rotate around the shifted centre.
</p>
</div>
</>
);
}How the React version works
The view has a padding of 170, 50, 30 and 150 pixels, so its centre and fitted extents avoid the bands at the edges. Unlike the center example, the padding belongs to the view itself.
What both versions share
OpenLayers renders the map and handles the interactions; the cc-openlayers/core layer gives both adapters the same view binding (MapViewBinding) and the same conversion of coordinates from EPSG:4326 to the map's projection. The padding bands and their styles are global gallery classes.
From Angular to React
| Angular | React |
|---|---|
<aol-view [padding]="padding"> | <View padding={padding}> with a module constant |
viewChild of the view and source with .instance | useRef passed to View and GeoJSONSource |
<aol-style> inside the layer | <Style> with Fill and Stroke inside VectorLayer |
Who creates, updates and disposes
Map creates the ol/Map in useLayoutEffect and disposes of it on unmount; each layer, source and interaction component adds its object to the parent it gets from context and removes it in its effect's cleanup, as ngOnDestroy does in Angular. The padding array is a module constant, so the adapter does not set it again on later renders.
Architecture and OpenLayers mechanics
Binding [padding]="padding" ([170, 50, 30, 150]) on the OpenLayers view shifts its internal viewport center away from the masked edges in CSS pixels. Every subsequent setCenter call places its coordinate at that shifted midpoint, fit(geometry as SimpleGeometry) frames features inside the clear rectangle automatically, and rotation gestures orbit the offset centre.
Shared CSS variables position both the visual overlay bands and the OpenLayers control containers so on-map buttons never hide behind covering panels.
How it works
[padding]="padding"applies 170, 50, 30 and 150 pixel insets (top, right, bottom, left) directly to the view, moving its optical centre to the middle of the free area.fit(geometry as SimpleGeometry)needs no per-callpaddingoption because a padded view constrains fitted extents to its uncovered region automatically.setCenter, +/- zoom buttons and Alt+Shift rotation all pivot around the shifted centre of the free window.- CSS variables fed by the same
paddingtuple align both the translucent edge bands and the repositioned map controls.
Pitfalls
- With view padding active, the view’s centre coordinate corresponds to the middle of the uncovered area rather than the middle of the <canvas> DOM box.
- Passing per-call
paddingtofitadds on top of the view’s ownpaddingand double-insets the geometry—specify the inset in one place only. - Default HTML controls do not move themselves when view
paddingis set; offset .ol-zoom and .ol-attribution in CSS so sidebars and bottom sheets do not cover them.
Related examples
The same feature in the OpenLayers examples .
Background: state and lifecycle of maps in Angular with OpenLayers, which the components of cc-openlayers take care of.