---
title: "Change a layer's color with buttons | JavaScript maps SDK"
description: "Change layer fill colors dynamically using the setPaintProperty method."
---

> 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.

# Change a layer’s color with buttons

Use [`setPaintProperty`](https://docs.maptiler.com/sdk-js/api/map/#map#setpaintproperty) to change a layer’s fill color.

### NPM module setup

```bash
npm install --save @maptiler/sdk
```

#### main.js

```js
import { Map, MapStyle, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
    container: 'map',
    style: MapStyle.BASE,
    center: [12.338, 45.4385],
    zoom: 17.4
});

const swatches = document.getElementById('swatches');
const layer = document.getElementById('layer');
const colors = [
    '#ffffcc',
    '#a1dab4',
    '#41b6c4',
    '#2c7fb8',
    '#253494',
    '#fed976',
    '#feb24c',
    '#fd8d3c',
    '#f03b20',
    '#bd0026'
];

colors.forEach(function (color) {
    const swatch = document.createElement('button');
    swatch.style.backgroundColor = color;
    swatch.addEventListener('click', function () {
        map.setPaintProperty(layer.value, 'fill-color', color);
    });
    swatches.appendChild(swatch);
});
```

#### index.html

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="robots" content="noindex">
  <title>Change a layer's color with buttons</title>
  <link rel="stylesheet" href="./style.css">
</head>
<body>
  <style>
  .map-overlay {
      font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
      position: absolute;
      width: 200px;
      top: 0;
      left: 0;
      padding: 10px;
  }

  .map-overlay .map-overlay-inner {
      background-color: #fff;
      box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
      border-radius: 3px;
      padding: 10px;
      margin-bottom: 10px;
  }

  .map-overlay-inner fieldset {
      border: none;
      padding: 0;
      margin: 0 0 10px;
  }

  .map-overlay-inner fieldset:last-child {
      margin: 0;
  }

  .map-overlay-inner select {
      width: 100%;
  }

  .map-overlay-inner label {
      display: block;
      font-weight: bold;
      margin: 0 0 5px;
  }

  .map-overlay-inner button {
      display: inline-block;
      width: 36px;
      height: 20px;
      border: none;
      cursor: pointer;
  }

  .map-overlay-inner button:focus {
      outline: none;
  }

  .map-overlay-inner button:hover {
      box-shadow: inset 0 0 0 3px rgba(0, 0, 0, 0.1);
  }
  </style>

  <div id="map"></div>
  <div class="map-overlay top">
  <div class="map-overlay-inner">
      <fieldset>
          <label>Select layer</label>
          <select id="layer" name="layer">
              <option value="Water">Water</option>
              <option value="Building">Buildings</option>
          </select>
      </fieldset>
      <fieldset>
          <label>Choose a color</label>
          <div id="swatches"></div>
      </fieldset>
  </div>
  </div>
  <script type="module" src="./main.js"></script>
</body>
</html>
```

#### style.css

```css
body {margin: 0; padding: 0;}
#map {position: absolute; top: 0; bottom: 0; width: 100%;}
```

### Basic JavaScript setup

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

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Change a layer's color with buttons</title>
  <script src="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.umd.min.js"></script>
  <link href="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.css" rel="stylesheet" />
  <style>
    body {
      margin: 0;
      padding: 0;
    }

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

    .map-overlay {
      font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
      position: absolute;
      width: 200px;
      top: 0;
      left: 0;
      padding: 10px;
    }

    .map-overlay .map-overlay-inner {
      background-color: #fff;
      box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
      border-radius: 3px;
      padding: 10px;
      margin-bottom: 10px;
    }

    .map-overlay-inner fieldset {
      border: none;
      padding: 0;
      margin: 0 0 10px;
    }

    .map-overlay-inner fieldset:last-child {
      margin: 0;
    }

    .map-overlay-inner select {
      width: 100%;
    }

    .map-overlay-inner label {
      display: block;
      font-weight: bold;
      margin: 0 0 5px;
    }

    .map-overlay-inner button {
      display: inline-block;
      width: 36px;
      height: 20px;
      border: none;
      cursor: pointer;
    }

    .map-overlay-inner button:focus {
      outline: none;
    }

    .map-overlay-inner button:hover {
      box-shadow: inset 0 0 0 3px rgba(0, 0, 0, 0.1);
    }
  </style>
</head>

<body>
  <div id="map"></div>
  <div class="map-overlay top">
    <div class="map-overlay-inner">
      <fieldset>
        <label>Select layer</label>
        <select id="layer" name="layer">
          <option value="Water">Water</option>
          <option value="Building">Buildings</option>
        </select>
      </fieldset>
      <fieldset>
        <label>Choose a color</label>
        <div id="swatches"></div>
      </fieldset>
    </div>
  </div>

  <script>
    maptilersdk.config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
    const map = new maptilersdk.Map({
      container: 'map',
      style: maptilersdk.MapStyle.BASE,
      center: [12.338, 45.4385],
      zoom: 17.4
    });

    const swatches = document.getElementById('swatches');
    const layer = document.getElementById('layer');
    const colors = [
      '#ffffcc',
      '#a1dab4',
      '#41b6c4',
      '#2c7fb8',
      '#253494',
      '#fed976',
      '#feb24c',
      '#fd8d3c',
      '#f03b20',
      '#bd0026'
    ];

    colors.forEach(function (color) {
      const swatch = document.createElement('button');
      swatch.style.backgroundColor = color;
      swatch.addEventListener('click', function () {
        map.setPaintProperty(layer.value, 'fill-color', color);
      });
      swatches.appendChild(swatch);
    });
  </script>
</body>

</html>
```

## Related examples

### [Change color based on zoom level](https://docs.maptiler.com/sdk-js/examples/change-building-color-based-on-zoom-level/)

Examples

Dynamically adjust 3D building colors based on zoom levels.

### [Style lines with a data-driven property](https://docs.maptiler.com/sdk-js/examples/data-driven-lines/)

Examples

Create visualizations using data expressions for line colors.

### [Custom color ramp (color ramp)](https://docs.maptiler.com/sdk-js/examples/color-ramp-custom/)

Examples

Visualize point layers by creating custom map color ramps.

### [Point layer colored and sized according to a property (point helper)](https://docs.maptiler.com/sdk-js/examples/helper-point-property-style/)

Examples

Style point layer by property using point helper.
