<header class="header">
<div class="header__title">
<h1>
<code>brightness()</code>
</h1>
</div>
<div class="header__reference">
<ul class="reference-list">
<li><a class="reference-list__link" href="https://developer.mozilla.org/en-US/docs/Web/CSS/filter-function/brightness">MDN reference</a></li>
<li><a class="reference-list__link" href="https://caniuse.com/#feat=css-filters">caniuse Support</a></li>
</ul>
</div>
</header>
<main>
<div class="example">
<p>
In this example, brightness is set to <code>50%</code> using a CSS Custom Property. By moving the range slider to either the left or right, you can increase or decrease the brightness effect applied to the photo.
</p>
<div class="controls">
<label class="controls__label" for="brightness">Brightness:</label>
<input class="controls__input" type="range" id="brightness" min="0" max="100" value="50">
</div>
<div class="example__demo example__brightness">
<img class="drink" alt="A Manhattan cocktail placed on a wooden table in a low-lit room." src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/11907/jakub-dziubak-gj7BLlSzIFs-unsplash.jpg"/>
</div>
</div>
<aside>
<p>
<small>Photo by <a href="https://unsplash.com/photos/gj7BLlSzIFs">Jakub Dziubak</a> on <a href="https://unsplash.com/">Unsplash</a>.</small>
</p>
</aside>
</main>
// Demo
:root {
--brightness: 50%;
}
.drink {
filter: brightness(var(--brightness));
}
// Pen Setup
.example__brightness {
background-color: var(--color-cinder);
padding: 2rem 2rem 1.75rem 2rem;
img {
height: auto;
width: 100%;
}
}
.controls {
display: flex;
flex-direction: column;
margin-top: var(--size-epsilon);
}
.controls__label {
font-weight: 700;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.controls__input {
margin-top: 0.5rem;
}
View Compiled
// Credit:
// Update CSS Variables with JS
// Wes Bos
// https://codepen.io/wesbos/pen/adQjoY
const rangeSlider = [].slice.call(document.querySelectorAll('#brightness'));
// Listen for changes
rangeSlider.forEach(input => input.addEventListener('change', handleUpdate));
rangeSlider.forEach(input => input.addEventListener('mousemove', handleUpdate));
function handleUpdate(e) {
// Append '%' to the end of the `blur` custom property
const suffix = (this.id === 'base' ? '' : '%');
document.documentElement.style.setProperty(`--${this.id}`, this.value + suffix);
}
This Pen doesn't use any external JavaScript resources.