Custom legend

Add a custom legend to your map from HTML contained in your code.

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Custom legend</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.mapbox.com/mapbox.js/v3.1.1/mapbox.js'></script>
<link href='https://api.mapbox.com/mapbox.js/v3.1.1/mapbox.css' rel='stylesheet' />
<style>
  body { margin:0; padding:0; }
  #map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<style>
.legend label,
.legend span {
  display:block;
  float:left;
  height:15px;
  width:20%;
  text-align:center;
  font-size:9px;
  color:#808080;
  }
</style>

<!-- Set the display of this container to none so we can
     add it programmatically to `legendControl` -->
<div id='legend' style='display:none;'>
  <strong>The Title or Explanation of your Map</strong>
  <nav class='legend clearfix'>
    <span style='background:#F1EEF6;'></span>
    <span style='background:#BDC9E1;'></span>
    <span style='background:#74A9CF;'></span>
    <span style='background:#2B8CBE;'></span>
    <span style='background:#045A8D;'></span>
    <label>0 - 20%</label>
    <label>40%</label>
    <label>60%</label>
    <label>80%</label>
    <label>100%</label>
    <small>Source: <a href="#link to source">Name of source</a></small>
</div>
<div id='map' class='dark'></div>

<script>
L.mapbox.accessToken = '<your access token here>';
  var map = L.mapbox.map('map', 'mapbox.dark');
  map.legendControl.addLegend(document.getElementById('legend').innerHTML);
</script>
to create your own custom map and use it in this example.
Use this example by copying its source into your own HTML page and replacing the Map ID with one of your own from your projects. Having trouble with JavaScript? Try out Codecademy or contact our support team.
Copy example