I have posted about double borders with CSS before but there was a useful tip by Andy Hume (@andyhume) at a recent London Web Standards meeting where he described the use of CSS3 box-shadow (previously on this blog, Box Shadow and Image Hover Effects) to give the effect of double borders on an element. With box-shadow, if you use zero for the horizontal offset and blur radius values and a small pixel value for vertical offset, this will give you another ‘border’ on any element. If the element already has a border, this means that you can create double borders with different colors. Here’s how it works.
Using this CSS:
.anotherboxshadowclass {
border: solid 2px #CCC;
-moz-box-shadow: 0px 2px 0px #69C;
-webkit-box-shadow: 0px 2px 0px #69C;
box-shadow: 0px 2px 0px #69C;
}
… will give a grey and blue ‘double border’ like this:
Here is a paragraph with a double border effect using box-shadow. You should see a grey ‘inner’ border and a blue ‘outer’ border on the bottom of this paragraph.
Of course, the usual caveats about browser support for CSS3 box-shadow still apply. It is supported by Firefox, Chrome and Safari but not Internet Explorer 8. However, if you are using CSS3 properties in your work, I think this a nice tip.
PS: You can also use negative values to change the double border position from bottom to top and, of course, swap the vertical/horizontal values to put the border on the right (or left, with a negative value). And Simon Thulbourn suggested that you can use more than one box-shadow to get the effect on two or more sides.
Stunning CSS3: A Project-Based Guide: Use this book to work through a series of practical yet cutting-edge projects. Each chapter walks you through standalone exercises that you can integrate into projects you're working on, or use as inspiration.
» Buy from Amazon.com · Amazon.co.uk
ThemeForest sell a range of site templates from some fantastic designers. They also sell some great WordPress themes!
Online invoicing made easy with CurdBee!
» Sign up
Join SugarSync for online backup. Sync your files between Mac, PC and mobile phone. Get 5 GB FREE and up to 10 GB bonus space!
HTML5 and CSS3 for the Real World will show you how to create websites using these new methods.
This easy-to-follow guide covers everything you need to know to get started. You’ll master the new semantic markup available in HTML5, as well as how to use CSS3 without sacrificing clean markup or resorting to complex workarounds. Buy the Book! · FREE Chapters
Dmitry Pashkevich 11 January 2012, 12:34 1
Unfortunately, this approach doesn’t allow you to create double borders with style other than ‘solid’ (i.e. dashed, dotted borders).
Clive Walker 11 January 2012, 13:10 2
@Dmitry: That’s true for the ‘outer border’ but the inner border can of course be dashed, dotted etc