> For the complete documentation index, see [llms.txt](https://docs.mapbox.com/mapbox-gl-js/llms.txt)

# Adjust a layer's opacity

Drag the range slider to adjust the opacity of a [raster layer](https://docs.mapbox.com/style-spec/reference/layers/#raster) on top of a map.

> Example code:

**JavaScript**

```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Adjust a layer's opacity</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v3.28.1/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.28.1/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<style>
    .map-overlay {
        font:
            bold 12px/20px 'Helvetica Neue',
            Arial,
            Helvetica,
            sans-serif;
        position: absolute;
        width: 25%;
        top: 0;
        left: 0;
        padding: 10px;
    }

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

    .map-overlay label {
        display: block;
        margin: 0 0 10px;
    }

    .map-overlay input {
        background-color: transparent;
        display: inline-block;
        width: 100%;
        position: relative;
        margin: 0;
        cursor: ew-resize;
    }
</style>

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

<div class="map-overlay top">
    <div class="map-overlay-inner">
        <label>Layer opacity: <span id="slider-value">100%</span></label>
        <input id="slider" type="range" min="0" max="100" step="0" value="100">
    </div>
</div>

<script>
    const map = new mapboxgl.Map({
        // TO MAKE THE MAP APPEAR YOU MUST
        // ADD YOUR ACCESS TOKEN FROM
        // https://account.mapbox.com
        accessToken: 'YOUR_MAPBOX_ACCESS_TOKEN',
        container: 'map',
        // Choose from Mapbox's core styles, or make your own style with Mapbox Studio
        style: 'mapbox://styles/mapbox/standard',
        config: {
            basemap: {
                theme: 'monochrome'
            }
        },
        center: [-87.6321, 41.8362],
        minZoom: 9.5,
        maxZoom: 13,
        zoom: 9.5
    });

    const slider = document.getElementById('slider');
    const sliderValue = document.getElementById('slider-value');

    map.on('load', () => {
        map.addSource('chicago', {
            'type': 'raster',
            'url': 'mapbox://mapbox.u8yyzaor'
        });
        map.addLayer({
            'id': 'chicago',
            'source': 'chicago',
            'type': 'raster'
        });

        slider.addEventListener('input', (e) => {
            // Adjust the layers opacity. layer here is arbitrary - this could
            // be another layer name found in your style or a custom layer
            // added on the fly using `addSource`.
            map.setPaintProperty(
                'chicago',
                'raster-opacity',
                parseInt(e.target.value, 10) / 100
            );

            // Value indicator
            sliderValue.textContent = e.target.value + '%';
        });
    });
</script>

</body>
</html>
```

**React**

```jsx
import React, { useEffect, useRef, useState } from 'react';
import * as mapboxgl from 'mapbox-gl/esm';
import 'mapbox-gl/dist/mapbox-gl.css';

const MapboxExample = () => {
  const mapContainerRef = useRef();
  const mapRef = useRef();
  const [sliderValue, setSliderValue] = useState(100);

  const handleChange = (e) => {
    mapRef.current.setPaintProperty(
      'chicago',
      'raster-opacity',
      parseInt(e.target.value, 10) / 100
    );

    setSliderValue(e.target.value);
  };

  useEffect(() => {
    mapRef.current = new mapboxgl.Map({
      // TO MAKE THE MAP APPEAR YOU MUST
      // ADD YOUR ACCESS TOKEN FROM
      // https://account.mapbox.com
      accessToken: 'YOUR_MAPBOX_ACCESS_TOKEN',
      container: mapContainerRef.current,
      style: 'mapbox://styles/mapbox/standard',
      config: {
        basemap: {
          theme: 'monochrome'
        }
      },
      center: [-87.6321, 41.8362],
      minZoom: 9.5,
      maxZoom: 13,
      zoom: 9.5
    });

    mapRef.current.on('load', () => {
      mapRef.current.addSource('chicago', {
        type: 'raster',
        url: 'mapbox://mapbox.u8yyzaor'
      });
      mapRef.current.addLayer({
        id: 'chicago',
        source: 'chicago',
        type: 'raster'
      });
    });
  }, []);

  return (
    <div style={{ height: '100%', position: 'relative' }}>
      <div ref={mapContainerRef} style={{ height: '100%' }}></div>
      <div
        style={{
          font: `12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif`,
          position: 'absolute',
          width: '25%',
          top: 0,
          left: 0,
          padding: '10px'
        }}
      >
        <div
          style={{
            backgroundColor: '#fff',
            boxShadow: '0 1px 2px rgba(0, 0, 0, 0.2)',
            borderRadius: '3px',
            padding: '10px',
            marginBottom: '10px'
          }}
        >
          <label
            style={{
              fontWeight: 'bold',
              lineHeight: '24px',
              display: 'block',
              margin: '0 0 10px'
            }}
          >
            Layer opacity: {sliderValue}%
          </label>
          <input
            type="range"
            onChange={handleChange}
            min="0"
            max="100"
            step="0"
            value={sliderValue}
          />
        </div>
      </div>
    </div>
  );
};

export default MapboxExample;
```