CSS Clipping and Masking: Examples and Observations - Vanseo Design |
CSS Clipping and Masking: Examples and Observations Posted: 12 Sep 2013 05:30 AM PDT On Monday I walked through the spec for css clipping and masking. Since the post grew a little long, I promised some examples for today. I’ll also offer a few observations at the end of the post.
The setup for these examples is actually quite simple. The html is little more than a div filled with text for some of the examples and an image for others. The difference you see in the screenshots below is mainly the difference in how the div or image was clipped or what I used to mask it. As a reminder browser support for both clipping and masking isn’t quite there yet, but it’s not that bad either. More browsers support both clipping and masking than don’t support them. In practice I couldn’t get these examples to work in Safari, though they did work in Webkit Nightly. I’m not sure why, since Safari is supposed to have support. The screenshots below are all from Chrome Canary. Shapes ReminderThe clipping examples below use basic shapes to clip an element or image so as a quick reminder here’s how those shapes work. If you need more detail about how these work, you can check my post on css shapes.
Clipping ExamplesI thought I would start easy and work with clipping before masking. The rather simple html I used is below.
Each of the paragraphs was filled with some lorem ipsum text and I added some general and arbitrary default styling.
Here’s how the above looks without any clipping applied. To play around I just added the -webkit-clip-path property with a variety of values. First I tried circles and ellipses. Below are the results along with the clip-path and value used.
For the next few example I reduced the height of the original div to 330px and clipped the original with different polygons.
The triangle I was able to work out for myself easily enough, but I didn’t try to figure out the more complex polygons. Instead I used the polygon tool to visually create them and let the tool give me back the code to use. Masking ExamplesFor masking I decided to use an image instead of a simple rectangle. Once the again the html is trivial.
The image is one I took a few years ago in Central Park of the Strawberry Fields memorial for John Lennon. Here’s how it looks without any styling applied Even though this is the masking example I thought I would first clip the image and I experimented using a circle.
You can see that using a circle for the clip-path doesn’t mean you’ll end up with a circle. By varying the x and y offsets and the size of the radius you can create more interesting shapes. Let’s get to some masking. First I tried to mask the image with simple linear and radial gradients
After that I decided to have a little more fun and play around with some of the other masking properties The example on the left in the image above uses the css below. Setting the size of the mask to something less than 100% and repeating it led to an interesting effect.
Changing the mask size to 10% produced the example in the middle and changing the gradient to linear and the mask size to 20% produced the example on the right. The code for this last example is below.
Next I decided to experiment with some image masks. I grabbed a drawing I had done years ago of Starry Night to use as an image mask. I opened the image in Pixelmator, selected a portion of it and applied a gradient. Then I created varying levels of transparency in the entire image to create 3 different masks. The css was simple. It just needed a link to the image mask I wanted to use.
Below are the results of applying each of the above masks to the original Strawberry Fields image. For my last bit of experimentation I created a couple of images of text and used each as the mask. Again the css was simple as it’s the path to the image mask.
You can see the results below. The only difference between the two is the font used for the text. Don’t ask me which fonts though, since I forgot which I used. The text on the right is actually the same as on the left. More ExamplesI’m hardly the only person to play around with css clipping and masking so here are a few more examples from others.
These are plenty more examples online if you care to search. These are just a few I happened across while researching this post. Observations and Closing ThoughtsI didn’t try every combination of every clipping and masking property. I barely scratched the surface of possibilities. Still you can likely see how a little bit of experimentation and creativity can lead to some interesting results. One thing I didn’t try at all was SVG clipping and masking, mainly due to my own lack of skills working with SVG. I never could figure out how the source-type properties worked. No matter which value (luminance or alpha) I used, the results always looked exactly the same. I assume the issue was in the masks I applied and that the ones I used wouldn’t show any meaningful difference between luminance and alpha. It was very easy to get both clipping and masking to work. What took a little more time to understand was how to control them, at least to whatever degree I actually learned to control them. Like I said at the start I also couldn’t get these examples working in Safari, though they did work in Wekbit Nightly. I’m not sure why as Safari is supposed to support clipping and masking. Finally in my post about css shapes I mentioned that I wasn’t able to constrain the background behind a shape and that it remained a rectangular box despite my efforts. Alan commented that it could be done using -webkit-clip-path with the same shape as -webkit-shape-outside. Yep. It works perfectly. Thanks Alan. I’d encourage you to experiment on your own. I maybe played around for half an hour at most to create everything I showed above plus quite a few more examples I didn’t show. Again while css clipping and masking isn’t going to work in every browser, there isn’t much harm in using either as far as I can tell. Non-supporting browsers show the original element or image so you can clip and mask away in the name of progressive enhancement now if you’d like. There’s one last topic I want to cover in regards to future css and that’s filtering. It’ll take more than a single post to cover and I’ll begin the discussion on Monday. The post CSS Clipping and Masking: Examples and Observations appeared first on Vanseo Design. |
You are subscribed to email updates from Vanseo Design » Blog To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |