Clipping and Masking in CSS CSS-Tricks
Clipping and Masking in CSS CSS-Tricks
com
8-10 minutes
.element {
clip: rect(10px, 20px, 30px, 40px);
}
.element {
.element {
clip-path: inset(10px 20px 30px 40px);
Note that there are no commas, and the syntax is different, but
ultimately does the same kind of things.
.clip-circle {
clip-path: circle(60px at center);
}
.clip-ellipse {
clip-path: ellipse(60px 40px at 75px 30px);
}
.clip-polygon {
clip-path: polygon(5% 5%, 100% 0%, 100% 75%,
75% 75%, 75% 100%, 50% 75%, 0% 75%);
.clip-examples {
You don’t have to define the clip-path value right in CSS, it can
reference a <clipPath> element defined in SVG. Here’s what
that looks like:
.clip-svg {
clip-path: url(#myClip);
}
Demo:
Left: clip path moves along with image in Google Chrome 54 (good).
Right: clip path is pinned to the upper left and doesn’t move along
with image in Safari 10 (bad).
Animating/Transitioning clip-path
Try it out:
Masking
img {
width: 150px;
-webkit-mask-image: -webkit-gradient(
linear, left top, right bottom,
color-stop(0.00, rgba(0,0,0,1)),
color-stop(0.35, rgba(0,0,0,1)),
color-stop(0.50, rgba(0,0,0,0)),
color-stop(0.65, rgba(0,0,0,0)),
color-stop(1.00, rgba(0,0,0,0)));
}
And it gave birth to things like this old tutorial WebKit Image
Wipes, which still works (you know, in Blink/WebKit land).
Check out this demo in Firefox too (example code lifted from
Dirk Shulze’s article). This kind of thing is a little dangerous at
the moment, as it doesn’t just not work in WebKit/Blink, it
erases the element it’s applied to entirely.
You can also link up an entire SVG file as the mask, like:
.mask {
mask: url(mask.svg);
}
Mask Types
.mask {
mask-type: luminance;
mask-type: alpha;
}
Border Masks
.border-mask {
-webkit-mask-box-image: url(stampTiles.svg)
30 repeat;
mask-border: url(stampTiles.svg) 30 repeat;
Browser Support
It’s so hard so summarize succinctly, since different properties
and even values have different support levels all over the place.
Not to mention how you use them and on what. It’s a
wilderness out there, so I’d recommend using as progressive
enhancement the best you can at the moment. Which might be
a little tough, as there isn’t even Modernizr tests for this stuff
really.
More