Layers (32)
Display buildings in 3DExtrude polygons for 3D indoor mappingAdd a 3D modelAdjust a layer's opacityAnimate a lineAnimate a series of imagesAnimate a pointChange building color based on zoom levelChange the case of labelsDisplay HTML clusters with custom propertiesCreate and style clustersChange a layer's color with buttonsAdd a custom style layerStyle circles with a data-driven propertyStyle lines with a data-driven propertyDisplay and style rich text labelsUse a fallback imageAdd a pattern to a polygonAdd a new layer below labelsAdd a GeoJSON lineDraw GeoJSON pointsAdd hillshadingCreate a heatmap layerCreate a gradient line using an expressionAdd multiple geometries from one GeoJSON sourceStyle ocean depth dataShow and hide layersUpdate a choropleth layer by zoom levelChange worldview of administrative boundariesVariable label placementVisualize population densityAdd a GeoJSON polygon

Change a layer's color with buttons

Using setPaintProperty to change a layer's fill color.

<!DOCTYPE html>
<meta charset="utf-8" />
<title>Change a layer's color with buttons</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://api.mapbox.com/mapbox-gl-js/v1.6.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.6.1/mapbox-gl.css" rel="stylesheet" />
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);
<div id="map"></div>
<div class="map-overlay top">
<div class="map-overlay-inner">
<label>Select layer</label>
<select id="layer" name="layer">
<option value="water">Water</option>
<option value="building">Buildings</option>
<label>Choose a color</label>
<div id="swatches"></div>
mapboxgl.accessToken = '<your access token here>';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v10',
center: [12.338, 45.4385],
zoom: 17.4
var swatches = document.getElementById('swatches');
var layer = document.getElementById('layer');
var colors = [
colors.forEach(function(color) {
var swatch = document.createElement('button');
swatch.style.backgroundColor = color;
swatch.addEventListener('click', function() {
map.setPaintProperty(layer.value, 'fill-color', color);
Was this example helpful?