Before-After Image Comparison Slider: Lightweight Web Component
This component helps you display visual changes compactly. Instead of placing two images side-by-side and forcing the user to scroll, you can layer them in the same space. The user then controls a slider to reveal one image over the other, making the comparison direct and interactive
1. Install & import Before-After Image Comparison Slider via NPM:
npm install @markpbaggett/before-after
2. Or load the main script from a CDN.
<script src="https://unpkg.com/@markpbaggett/before-after"></script>
3. Add the <before-after /> component to the webpage, define the before and after image URLs, and set a fixed height (the width adapts to its container). The start-position attribute controls where the slider begins.
<before-after before="before.jpg" after="after.jpg" style="height: 400px;" >
4. Use the start-position attribute to control where the slider begins. Defaults to 50 percent.
<before-after before="before.jpg" after="after.jpg" style="height: 400px;" start-position="30" >
5. Customize the text labels displayed on the before/after image sections.
<before-after before="before.jpg" after="after.jpg" style="height: 400px;" start-position="30" before-label="Before" after-label="After" >
6. You can style the component using standard CSS. Since it’s a web component with a shadow DOM, external styles don’t cascade into the component by default, but you can still customize the appearance:
before-after {
display: block;
height: 500px; /* Required for visibility */ width: 80%;
margin: 2rem auto;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
} Q: What happens if my “before” and “after” images have different dimensions?
A: The component uses object-fit: contain for the images. This means both images will be scaled down to fit within the component’s dimensions while maintaining their aspect ratio. You won’t get any weird stretching, but you might see some empty space if the aspect ratios differ significantly.
Q: How can I customize the look of the slider handle and the dividing line?
A: Because the component uses a closed Shadow DOM, its internal elements aren’t directly targetable by your page’s CSS. The library does not expose CSS custom properties for theming these parts. You would have to fork the repository and modify the CSS string directly in the JavaScript file to make visual changes.
Q: Can I control the slider position programmatically?
A: Yes. The component exposes the BeforeAfter class on the window object. You can select the element and update its start-position attribute, and the component will re-render. For more complex interactions, you could also set the attribute directly: document.querySelector('before-after').setAttribute('start-position', '75').
The post Before-After Image Comparison Slider: Lightweight Web Component appeared first on CSS Script.
Commissioner of Homeland Security Jeff Long, left, seated next to Tennessee Highway Patrol Col. Matt…
Gov. Bill Lee's administration has proposed a disaster assistance fund -- initially created by the…
Amazon is going through something of a massive restocking mission this week for Pokémon cards,…
Amazon is going through something of a massive restocking mission this week for Pokémon cards,…
Magic: The Gathering has kicked off its Teenage Mutant Ninja Turtles set prerelease weekend, but…
The much-delayed Spider-Man: Beyond the Spider-Verse currently has a June 18, 2027 release date. If…
This website uses cookies.