CSS clip-path in WordPress themes
clip-path cuts an element to a shape: everything outside the shape is not painted. Slanted section edges, hexagon avatars and “wipe” reveals used to need extra markup, transparent PNGs or SVG masks; now they are one CSS property, and the shape can even be animated.
Problem: A theme design has a hero with a slanted bottom edge, round and hexagonal avatars and images that reveal from left to right on scroll.
Solution: Use clip-path with the basic shapes polygon(), circle(), ellipse() and inset(), animate between two shapes with the same number of points, and add the reveal class with an Intersection Observer.
Slanted section edge
.ha-hero {
clip-path: polygon(0 0, 100% 0, 100% 85%, 0 100%);
padding: 6rem 2rem 8rem;
background: #1a1a2e;
color: #fff;
}
The points are x/y pairs, clockwise from the top left. The bottom padding is larger because the slant cuts away the lower part of the box; keep the text out of that area.
Avatars
.ha-avatar {
width: 120px;
height: 120px;
object-fit: cover;
}
.ha-avatar--circle {
clip-path: circle(50%);
}
.ha-avatar--hex {
clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
}
For a simple circle border-radius: 50% does the same and also rounds the outline and the shadow; clip-path pays off for shapes that border-radius cannot make.
Reveal on scroll
.js .ha-reveal {
clip-path: inset(0 100% 0 0);
transition: clip-path 0.7s cubic-bezier(0.77, 0, 0.18, 1);
}
.js .ha-reveal.is-visible {
clip-path: inset(0 0 0 0);
}
@media (prefers-reduced-motion: reduce) {
.js .ha-reveal {
clip-path: none;
transition: none;
}
}
const observer = new IntersectionObserver( ( entries ) => {
entries.forEach( ( entry ) => {
if ( entry.isIntersecting ) {
entry.target.classList.add( 'is-visible' );
observer.unobserve( entry.target );
}
} );
}, { threshold: 0.2 } );
document.querySelectorAll( '.ha-reveal' ).forEach( ( el ) => observer.observe( el ) );
The observer is explained in Intersection Observer for scroll animations. Without JavaScript the images would stay hidden. Scope the rule to a class that the script sets first, document.documentElement.classList.add( 'js' ), and write the selector as .js .ha-reveal.
Shape on hover
.ha-badge {
clip-path: polygon(50% 0, 100% 38%, 82% 100%, 18% 100%, 0 38%);
transition: clip-path 0.3s ease;
}
.ha-badge:hover {
clip-path: polygon(50% 5%, 95% 38%, 79% 95%, 21% 95%, 5% 38%);
}
A transition between two polygons works only when both have the same number of points.
NOTE: clip-path also cuts off box-shadow, outlines and the focus ring, and clicks outside the shape do not reach the element. Put the shadow on a parent with filter: drop-shadow(), and do not clip links and buttons so that their focus indicator disappears.