<header class="header">
<div class="header__title">
<h1>
<code>contrast()</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/contrast">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, contrast is set to <code>100%</code> using a CSS Custom Property. By moving the range slider, you can increase the amount of contrast applied to the photo.
</p>
<div class="controls">
<label class="controls__label" for="contrast">Contrast:</label>
<input class="controls__input" type="range" id="contrast" min="100" max="500" value="100">
</div>
<div class="example__demo example__demo--contrast">
<img
class="burger"
alt="A burger plated on a cutting board. In the background is a frosty beer."
src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/11907/camila-melim-RZjC6H7to1M-unsplash.jpg" />
</div>
</div>
<aside>
<p>
<small><a href="https://unsplash.com/photos/RZjC6H7to1M">"burger patty on board photo"</a> by <a href="https://unsplash.com/@camilamelim">Camila Melim</a> on <a href="https://unsplash.com/">Unsplash</a>.</small>
</p>
</aside>
</main>
// Demo
:root {
--contrast: 100%;
}
.burger {
filter: brightness(var(--contrast));
}
// Pen Setup
.example__demo--contrast {
padding: 1rem 1rem 0.75rem 1rem;
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('#contrast'));
// 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.