HTC Desire in CSS3 – Part 2
January 20, 2011
(Back to Part 1)
Gradients and Box Shadows
Step 6
Step 7
In CSS3 we can use a gradient instead of an image. Remember you can write url(myimg.jpg)? Anywhere you could do that, like background-image.
It's easiest to use a helper like the Ultimate CSS Gradient Generator to create your gradients. Then just copy the generated code. It does take some playing around to get the effect you're after.
This is the gradient:
Opera and IE9 don't support CSS3 gradients yet (they're quite experimental as you can see by the varying syntax).
Step 8
Another gradient to get the shading and 3d effect around the 'inner' light grey area.
A good tip: to get sharp contrasts/edges and blocks of colour, you can set the same colour at multiple points, and multiple colours at the same point. E.g. black 0%, black 60%, white 60%, white 61%, black 61%, black 100% will give you a simple white line on a black background.
With a linear gradient the effect is not quite as nifty as it could be if it followed the shape of the border.
Step 9
But... actually, we can colour along the curved borders.
One way is, unsurprisingly, using a border-top or border-bottom.
Another is a more exciting new property: box-shadow, which, naturally, appears outside/behind the box. This has the added advantage that it doesn't add to the element size (which border does).
Using these we can put some highlights and lowlights in the appropriate places.
As you can see, you can define more than one box shadow on the same box. The first two parameters are the shadow offset, the third is the fuzziness, and then the colour (and if you use rgba you can have transparent box shadows). For even more features see: CSS3.info, W3C CSS3.
Chrome - inset box shadows are very buggy, but the fix should be going out soon (it's ok in the dev channel build).
Webkit and Firefox 3.6 also have problems with curved borders - they're meant to transition smoothly between borders of different sizes, but that's not there yet and you get a sharp cutoff. Normal box-shadow is nicely supported by both though, which can give a similar effect.
Step 10
More shininess:
Making good use of inset here for some extra shading inside the lower grey area.
Hover over the style rules to see where they're applied: the inset shadow shifted downwards was entirely accidental at first, but looked nifty.
Step 11
Just getting the shading at the top of the phone right.
Summary
Here's the latest phone on a separate page.
We still haven't used any images. Just saying.
But here are some - so you can see how that last one renders in various browsers.
- Firefox 4 betaLooks best - I used it while developing.
- Firefox 3.6.13 Border smoothing is missing.
- Opera 11Gradients missing.
- IE9Gradients missing.
- Safari 5.0.3 (Win)Border smoothing and percentages in border-radius are missing.
- AndroidBorder smoothing and percentages in border-radius are missing.
- Chrome Dev (10.0.648.11)Border smoothing is missing.
- Chrome Stable (8.0.552.237)Border smoothing missing. Inset box shadows are broken.
- IE8Because I know you were curious.
To continue...
But we aren't done yet! On to Part 3.
Where were we? Code dump, this is how far we've got: