---
title: "How to use Leaflet | Leaflet"
description: "Create and display Leaflet maps on web pages with MapTiler."
---

> For AI agents: a documentation index is available at [/llms.txt](https://docs.maptiler.com/llms.txt). Markdown versions of all pages are available by appending .md to any URL.

# How to use Leaflet

This tutorial shows how to create a Leaflet map and display it on a web page using MapTiler. This tutorial explains how to use Leaflet with basic JavaScript from a **CDN**. To use Leaflet from **NPM**, see the step-by-step tutorial [How to use Leaflet with **NPM**](https://docs.maptiler.com/leaflet/examples/npm-get-started/).

1.  Create a basic HTML file.
    
    ```html
    <!DOCTYPE html>
      <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Display a map</title>
        <style>
          body {
            margin: 0;
            padding: 0;
          }
        </style>
      </head>
      <body>
    
      </body>
      <script>
    
      </script>
      </html>
    ```
    
2.  Include the Leaflet JavaScript and CSS files in the `<head>` of your HTML file.
    
    ```html
    <title>Display a map</title>
      <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
      <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
      <style>
    ```
    
3.  Create a `<div>` element with a certain id where you want your map to be.
    
    Add `<div>` tag into your page. This div will be the container where the map will be loaded.
    
    ```html
    <body>
         <div id="map"></div>
       </body>
    ```
    
4.  The div must have non-zero height.
    

```css
body {
  margin: 0;
  padding: 0;
}

#map {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 100%;
}
```

1.  Finally, load the map with the style

```js
const key = 'YOUR_MAPTILER_API_KEY_HERE';
const map = L.map('map').setView([0, 0], 2);
L.tileLayer(`https://api.maptiler.com/maps/streets-v4/{z}/{x}/{y}.png?key=${key}`,{
  tileSize: 512,
  zoomOffset: -1,
  minZoom: 1,
  attribution: "\u003ca href=\"https://www.maptiler.com/copyright/\" target=\"_blank\"\u003e\u0026copy; MapTiler\u003c/a\u003e \u003ca href=\"https://www.openstreetmap.org/copyright\" target=\"_blank\"\u003e\u0026copy; OpenStreetMap contributors\u003c/a\u003e",
  crossOrigin: true
}).addTo(map);
```

1.  Replace `YOUR_MAPTILER_API_KEY_HERE` with [your own API key](https://cloud.maptiler.com/account/keys/). Make sure to [secure the key](https://docs.maptiler.com/guides/credentials/api-key/#secure-key) before you publish it.

### Complete source code

```html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Display a map</title>
  <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
  <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
  <style>
    body {
      margin: 0;
      padding: 0;
    }

    #map {
      position: absolute;
      top: 0;
      bottom: 0;
      width: 100%;
    }
  </style>
</head>

<body>
  <div id="map"></div>
</body>
<script>
  const key = 'YOUR_MAPTILER_API_KEY_HERE';

  const map = L.map('map').setView([0, 0], 2);
  const layer = L.tileLayer(`https://api.maptiler.com/maps/streets-v4/{z}/{x}/{y}.png?key=${key}`, {
    tileSize: 512,
    zoomOffset: -1,
    minZoom: 1,
    attribution: "\u003ca href=\"https://www.maptiler.com/copyright/\" target=\"_blank\"\u003e\u0026copy; MapTiler\u003c/a\u003e \u003ca href=\"https://www.openstreetmap.org/copyright\" target=\"_blank\"\u003e\u0026copy; OpenStreetMap contributors\u003c/a\u003e",
    crossOrigin: true
  }).addTo(map);
</script>

</html>
```

## Learn more

Leaflet doesn’t support vector tiles by default. For basemaps, it is **recommended** to use it with traditional **raster tiles** (Mercator XYZ).

There are two ways to use MapTiler maps in Leaflet:

-   [Raster tiles](https://docs.maptiler.com/leaflet/examples/raster-tiles-in-leaflet-js/)
-   [Vector tiles](https://docs.maptiler.com/leaflet/examples/vector-tiles-in-leaflet-js/)

If you do not know the difference between Raster and Vector or you have doubts about which format is better for you, consult the article [Raster vs Vector Map Tiles: What Is the Difference Between the Two Data Types?](https://docs.maptiler.com/guides/how-maps-work/raster-vector-tiles/)

Check out the [How to use Leaflet with NPM](https://docs.maptiler.com/leaflet/examples/npm-get-started/) tutorial.

## Related examples

### [Get started](https://docs.maptiler.com/leaflet/examples/get-started/)

Tutorials

Quickly get started with Leaflet JS web mapping library.

### [Display Marker](https://docs.maptiler.com/leaflet/examples/default-marker/)

Tutorials

Add default map markers using Leaflet JS web mapping library.

### [Leaflet with NPM](https://docs.maptiler.com/leaflet/examples/npm-get-started/)

Tutorials

Install Leaflet via NPM to create and display web maps.

### [Vector Tiles in Leaflet](https://docs.maptiler.com/leaflet/examples/vector-tiles-in-leaflet-js/)

Tutorials

Display MapTiler vector tiles in Leaflet using maptilersdk plugin.
