Here is a little css to create fluid rounded borders in css (no need for JavaScript) that appears to work well in all the browsers I have been able to test. The only exception seems to be Mac IE 5.2, but that browser is a total basket case for correct CSS support anyway. You can see how I used the technique in a live site by visiting Price USA - Shop where you want.

ORB can be used to create any width border and only requires one image to create all four corners. This should really help those who are trying to keep the small image count down - some of the rounded border techniques I have seen require 9 separate images. The key is to use a single composite images and displace it by the required amount to create the final borders. The following example shows how to create a three pixel wide light grey border (#DDD) using a 10 pixel corner. So how is this done?

Image

The image needs to be created as a composite image of the four rounded images. In the example shown below the image is 10×40 pixels with the individual round corners ordered (from top to bottom): top left, top right, bottom left, bottom right. You will need to adjust the image to suit your particular border width and color, as well as the diameter of your curve. 

Grey rounded border composite box image 

CSS


b.bt, b.bt b, b.bb, b.bb b {display: block; height: 10px;font-size: 1px;background:url(images/grey.gif) no-repeat;position:relative}

b.bt {top: -3px; left: -3px}

b.bt b {background-position:100% -10px; left: 6px}

b.bb {background-position:0 -20px; top:3px; left: -3px}

b.bb b {background-position:100% -30px; left: 6px}

#grey_border {border: #ddd 3px solid}

The key things to note here are the height value which needs to be set to the width value of your image and the background-position values for the image which need to be adjust by the width of your border. The values shown in the above example are for a 3 pixel wide border.

HTML

To create the box you just need add the following html inside the div with the border property.

<div id="grey_border">

<b class="bt"></b><b></b>[your content]<b class="bb"></b><b></b>

</div>

I would appreciate feedback if the technique isn’t working in your browser. So far the only browser I have found where it doesn’t work is Mac IE 5.2, but since I’m a Mac person I haven’t been able to test it extensively in all the old Windows browsers.

Sphere: Related Content

7 Responses to “ORB | Fluid CSS One Image Round Borders”

  1. Saskia Says:

    Wouldn’t that be nice. Doesn’t work for me though in Firefox 3.0.1. Does however in IE6?

  2. Saskia Says:

    Well, wouldn’t just be nice: it IS nice! You can disregard/remove my comment earlier; made a css error. Oops. Thanks for this nice solution!

  3. kushin Says:

    I have been looking around all kinds of css solutions for rounded corners, yours is by far the smallest amount of code and works great, thanks for sharing this.

  4. Carlos Says:

    The selectors for the right corners are incorrect for the given HTML. Since follows b.bt and b.bb (and is not contained within them), the correct selector rules are b.bt+b and b.bb+b.

  5. Mamoon Says:

    Hello,
    Boss I am using this code looking very easy and this is my requirement. But its not working with me.

    ——–css———-
    b.bt, b.bt+b, b.bb, b.bb+b{
    display: block;
    height: 10px;
    font-size: 1px;
    background:url(grey.gif) no-repeat;
    position:relative
    }
    b.bt {top: -3px; left: -3px}
    b.bt+b {background-position:100% -10px; left: 3px;}
    b.bb {background-position:0 -20px; top:3px; left: -3px}
    b.bb+b {background-position:100% -30px; left: 3px}
    #grey_border {border: #ddd 3px solid}
    ——–end css——

    ———HTML——–

    sfsdfsd

    —–End HTML——–

  6. Jock Says:

    Can I just ask - sorry if this is an obvious one - but why are you using elements and not, say s?

  7. Daniel Says:

    There is no particular reason for for using elements.

Leave a Reply