Creating iOS 7 effects with CSS3: translucency and transparency
So, the iOS 7 Beta has been out for a couple of weeks, and while the opinions over whether it’s amazing, terrible, too much like Android or Windows Phone continues, it’s fair to say that, as Maxine observed a week ago here at Web Directions:
it’s hard to imagine today’s release still not having a massive impact on the design of not just iPhone apps, but apps developed for any platform, including the web, and web sites themselves more generally
So, we thought we’d take a look at some of the more notable design features from the beta, and how you might implement them using web technology. Note that while we’re part of the iOS Developer Program, we’re basing these observations purely on Apple’s publicly released images and videos.
In this first instalment, we’ll look at the use of translucency in the interface. Here’s what we are going to build: there’s a bit more to it than you might think!
Transparency and Translucency
While the word on the street in advance of the release was that Apple would be adopting a “flat” style of UI with iOS7, as has become popular on the web and on various platforms such as Android, Windows Phone, and Windows 8, Apple went out of their way to emphasise the concept of “Depth”. One of the key ways they’ve brought a depth to the new OS is through translucency.
Now, you might be wondering what the fuss is, after all, we’ve had transparency in interfaces for years.
But translucency is not necessarily transparency. For an object to be transparent, any light passing through it should not be dispersed, so the image of something behind it remains as sharp as if the transparent object weren’t there.
We’re probably most used to semi-transparency in UIs, where an image behind another is darkened, but the definition of that object is not lowered. Here’s a very simple example we’d all be familiar with.
We could do this in a couple of ways on the web. We could use the CSS property opacity
. This as you almost certainly know, takes a value between 0 and 1, where the lower the value, the more transparent an element is (opacity is the opposite of translucency, and we’ll see in a moment why the property arguably should have been called transparency
. So, we might use this CSS
h2 { opacity: .3 }
But the problem is, this applies to the entire element (not just the background), as well as all of its descendant elements. We can’t even do this
h2 * { opacity: 1 }
to “turn off” opacity for descendent elements. Once opacity is set, it’s on for the element, and all its descendants.
So opacity
certainly isn’t what we always want for creating transparency in our designs. We could use a PNG image, with alpha channel transparency as a background-image
for the element, but CSS provides a simpler, more generalizable way to do this, using RGBa colors. RGBa colors are like regular RGB colors, but also take an opacity value between 0, and 1 as a fourth parameter. As an example, the effect achieved above uses rgba(255, 255, 255, .8)
, a white background color with an opacity of 80% (or in other words, 20% transparency).
Note how the image behind is still sharp, its edges aren’t blurred. Now take a look at Apple’s Control Center above. The icons of the homescreen it overlays are ‘smudged’ as if seen through frosted glass. This is not simply transparency as we know it on the web—it’s translucency. Translucency is essentially a superset of transparency. Where with transparency light is not dispersed as it passes through the object, with translucency, it may be. Think of it like looking through frosted glass. Now, how can we do this with web technologies?
Translucency
To tell the truth, there’s no easy way to achieve this effect right now in the browser. There are a number of ways of emulating aspects of the effect, with varying degrees of support. We’ll take a look at some of these now.
CSS Filters
The closest we’re likely to get right now are CSS filters. Not to be confused with either Internet Explorer’s old, non standard, and no longer supported in IE10 filter
property, or the various filter techniques we used to rely on to hide CSS from various browsers in the good old days.
At present, this technique is only supported in modern webkit based browsers, using the -webkit-
vendor prefix, and on iOS6+ and Blackberry 10+ (though not the standard Android browser). The good thing is that it falls back nicely for browsers that don’t support CSS filters.
A filter is a “graphical operation that is applied to an element as it is drawn into the document”. Filters can be quite simple, and there are a small number of “canned” filters for achieving common effects such as blurring an image. There are also more complex approaches to creating filters, all the way up to creating shaders in a shader language.
Today we’re just going to use the basic “blur” effect (other basic effects include grayscale, sepia, contrast and invert). We use these effects by using the filter
property, which takes a function as its value. In this case, the blur
function, which takes a value specifying how blurry the effect should be.
Here, we’ll blur an element with the id of “homescreen”
#homescreen { -webkit-filter: blur(10px); filter: blur(10px); }
Which will produce a result rather like this (before on the left, after on the right)
So, this looks perfect! Why don’t we use this technique as follows:
- We’ll have a foreground “Control Center” element, which contains the controls we want to present to the user
- Behind this, with the same dimensions, we’ll have another element, blurred using the blur filter, as the “background” for the controls
- All this in front of our homescreen element. Note that this time we won’t blur the homescreen as we just did, rather the “background” of the foreground element.
Schematically, this is what we’re doing
And here’s what we get
Which isn’t quite what we expected. Why? Well, the blur effect only applies to the element itself, so it’s not affecting the elements shining through the background of the blurred element. We’ll have to apply the blur to the homescreen element itself. Let’s do that and see what happens.
This is a lot closer to what we want. Behind the foreground element, we have our homescreen element blurred. But, we have one big stumbling block—we’ve also blurred the part of the homescreen that’s peeking over the top. Not what we want. This is a limitation of the basic CSS filters, they apply to the whole of an element, not just parts of them.
So, we’re going to have to come up with a hack. Here’s my thinking. Firstly, we no longer need our fake background element. We’ll just apply a semi-transparent background color using rgba to the control panel element itself. But here’s how I’m going to fake the only-partly-blurred homescreen element. We’re going to clone the element. Then we’ll apply the blur to the cloned homescreen, but not the original. Lastly, we only want part of the cloned element to show, not all of it. Hmm, how to achieve this all? Let’s position the cloned element behind the “real” homescreen element.
Here’s how we might visualize that
And let’s see how that works in the browser
Which is starting to get there. It’s not a general solution, and it’s certainly less than ideal having to clone an existing element. But for now it’s a start.
Animating the translucent overlay
Just to show this is not a completely hacked up solution, we can even animate the effect of the overlay sliding up over the homescreen, and down when no longer needed. We’ll do this using a tiny bit of JavaScript, and CSS transitions for the animation itself.
Our JavaScript will simply be
- set the
height
of the ‘real’ homescreen element to 20%, andtop
of the Control Center element to 20% to slide the control center element up - set the
height
of the ‘real’ homescreen element to 100% and the top of the Control Center element to 100% to slide the control center element down
You can see this in action here.
Notice that we don’t have to change the cloned homescreen element at all. It just sits there quietly behind the ‘real’ homescreen. Here’s more or less all the JavaScript we need to do this
if(slidingUp === true) { //sliding the ControlCenter up homeScreen.style.height = "20%" controlCenter.style.top = "20%" } else { //sliding the ControlCenter down homeScreen.style.height = "100%" controlCenter.style.top = "100%" }
Alternatively, we could create two classes, such as “up” and “down”, and simply change the class of each element, with the actual styling taking place in our style sheet. This better separates presentation from behavior.
Now for the animation, we just have two simple CSS transitions, one for the height
property of the homescreen element
transition: height .75s
And one for the top
property of the Control Center element
transition: top .75s
You’ll want to add a -webkit-
prefixed version as well for some webkit browsers, including Android and iOS. Of course, to really animate this in the most lifelike way possible for touch devices, you’ll want to make the speed of the animation a function of how quickly the user ‘flicked’ the Control Center up or down. But hopefully this demonstrates how we can implement an animatable, translucent overlay effect, using CSS filters. It’s also backwards compatible, as browsers which don’t support filters will show the semi-transparent overlay, without the blurring of the homescreen element which is behind it.
Caveats
This article is not meant to provide a bullet-proof way of creating translucent effects. Rather, it’s an exploration of the various features of CSS are available to create an effect we’re likely to be seeing quite a lot of on the web and elswhere in coming months. The properties we’ve looked at here will work mostly in webkit browsers, with the advantage that they fall back nicely for browsers which don’t as yet support CSS Filters.
There is in fact another way to create the translucent effect, using SVG filters, that is more widely supported, and also does away with the need for our cloned background element. Sadly, at present, while SVG filters are quite well supported in modern browsers, this technique won’t actually work in most browsers.
More Translucency
There’s other uses of translucency in iOS 7 that present additional challenges in terms of implementation using web technologies, which we’ll explore soon in part II.
Further reading and watching
- For more on CSS Filters generally, Alex Danilo’s Web Directions Code presentation, Create Impact with CSS Filters
- Five iOS 7 web design trends we can expect to see more of
- More on CSS Transitions and Animations from Web Diretions
Technologies covered in this post
Browser Support via Can I Use
Great reading, every weekend.
We round up the best writing about the web and send it your way each Friday.