Skip to content

Attach a popup to a marker instance

Attach a popup to a marker and display it on click.

<!DOCTYPE html>
<html lang="en">
    <title>Attach a popup to a marker instance</title>
    <meta property="og:description" content="Attach a popup to a marker and display it on click." />
    <meta charset='utf-8'>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel='stylesheet' href='' />
    <script src=''></script>
        body { margin: 0; padding: 0; }
        html, body, #map { height: 100%; }
    #marker {
        background-image: url('');
        background-size: cover;
        width: 50px;
        height: 50px;
        border-radius: 50%;
        cursor: pointer;

    .maplibregl-popup {
        max-width: 200px;

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

    const monument = [-77.0353, 38.8895];
    const map = new maplibregl.Map({
        container: 'map',
        style: '',
        center: monument,
        zoom: 15

    // create the popup
    const popup = new maplibregl.Popup({offset: 25}).setText(
        'Construction on the Washington Monument began in 1848.'

    // create DOM element for the marker
    const el = document.createElement('div'); = 'marker';

    // create the marker
    new maplibregl.Marker({element: el})
        .setPopup(popup) // sets a popup on this marker