Creating Beveled Images with CSS

    James Edwards
    Share

    Recently, I wanted a simple CSS method for adding a beveled effect to images. It’s easy enough to create a sense of depth with normal outset borders (below left), but I was after an effect that would actually look like part of the image, as though it were a bevel on the image itself (below right).

    Demo 2: The borders look like bevelling on the image itself.Demo 1: The image has gray outset borders around it.

    In the end I found four different ways of doing it, each with different levels of support: from the cleanest approach that only worked in one browser, to the most robust that works in everything back to IE6.

    All of them work on the same core principal; black borders for shade and white borders for highlighting are overlaid on top of an image, and then blended with some form of opacity. In each case, browsers without support for that technique will simply show the image as normal.

    Technique 1: Using Generated Content on the Image (Demo)

    • Pros: Ultra-clean technique requires no additional markup
    • Cons: Only works in Opera

    With this first technique we create a pseudo-element using :after, then style it to be perfectly overlaid on top of the image. Then we add borders to the overlaid element, and use RGBA to define each border color: the top and left borders are rgba(255,255,255,0.4), white with 40% opacity; and the bottom and right borders are rgba(0,0,0,0.4), black with 40% opacity:

    img.beveled{    position:relative;}img.beveled:after{    position:absolute;    left:0;    top:0;    display:block;    content:"