CSS Rounded Corners

CSS Rounded Corners — Using only one image file!

There are lots of tutorials on this subject, yet most of the one’s you see require the use of four separate images, which means a lot of fiddly photoshop work; cutting, pasting, naming and saving — all times four.

This method will still need four divs, but it will utilise just one non-symmetrical image, aligned (i.e. not split) as indicated in this diagram:

Rounded corners diagram

As you can see the layout of the divs is not typical of the traditional four-image method, but rather there is one huge wrapping div with two small divs fixed in the top-right and bottom-left corners and the content-containing div aligned to the bottom right corner.

Therefore the HTML will look like this, (notice the lack of recursive nesting):

<div id="top_left">

<div id="top_right"></div>
<div id="bottom_left"></div>
<div id="bottom_right">
Hello there, I'm some content.
</div>

</div>

And here is the the heart of the CSS (note the alignment attributes in the background properties):

#top_left{
background: url(image.jpg) top left no-repeat;
padding-top: 6px;
padding-left: 6px;
position: relative;
}
#top_right{
background: url(image.jpg) top right no-repeat;
width: 6px;
height: 6px;
position: absolute;
right: 0%;
top: 0%;
}
#bottom_left{
background: url(image.jpg) bottom left no-repeat;
width: 6px;

height: 6px;
position: absolute;
left: 0%;
bottom: 0%;
}
#bottom_right{ /*This is where the actual text goes*/
background: url(image.jpg) bottom right no-repeat;
}

Notes

  • The top-left div provides the top-left rounded corner and the top and left borders — thus the need for padding of equal size to the size of the top-right and bottom-left divs. This padding in the top and left sides prevents the bottom-right div from obscuring the wrapping div’s borders and corner.
  • The two small divs in the top-right and bottom-left just provide rounded corners.
  • Absolute positiong is needed for the two small divs — remember that to absolutely position a div, its parent div must be assigned relative position.
  • The bottom-right div actually contains the content text or picture. It provides both a rounded corner and the right and bottom borders. It is also responsible for stretching the top-left-wrapping-div to size.
  • Interestingly this method can be used to display a non-symmetrical detail in the bottom-right corner.
  • Here’s another approach to rounded corners with a single image but using a slightly different method.

CSS for MSIE before version 7

#top_left{
background: url(image.jpg) top left no-repeat;
padding-top: 6px;
padding-left: 6px;
position: relative;
height: 100%; /*This causes absolute positioning to work properly*/
}
#top_right{
margin-right: -1px; /*Adjusts a small offset*/

background: url(image.jpg) top right no-repeat;
width: 6px;
height: 6px;
position: absolute;
right: 0%;
top: 0%;
}
#bottom_left{
margin-bottom: -1px; /*Adjusts a small offset*/
background: url(image.jpg) bottom left no-repeat;
width: 6px;

height: 6px;
position: absolute;
left: 0%;
bottom: 0%;
}
#bottom_right{ /*This is where the actual text goes*/
background: url(image.jpg) bottom right no-repeat;
}

No Trackbacks

You can leave a trackback using this URL: http://www.tombh.co.uk/2008/01/css-rounded-corners/trackback/

10 Comments

  1. It totally works!!
    *Way* easier than fossicking about with multiple image-related malarkey.
    Well impressed..
    Thanks Tom
    You rock
    :o)

    Posted February 21, 2008 at 5:46 pm | Permalink
  2. I would suggest using classes instead of ids in your div. That would let you put more than one box with corners on it on the same page. I actually use the method I posted here:
    http://dynamicimages.heroku.com/articles/3

    That site is a little app I built for those who would rather not use a graphics program to create their initial images. You can check it out at:
    http://www.thoughtless.ca

    It will let you change the size, shape, and color of your corners directly in the css, without using a graphics editor. I find that it save me a lot of time when I haven’t decided on my color scheme yet. Once the colors are decided, I download the images so that I’m not dependent on the app.

    Posted June 6, 2008 at 1:15 pm | Permalink
  3. This is really going to help me get the new http://www.bdcomputers.com working! Thanks much!

    Posted June 8, 2008 at 11:18 pm | Permalink
  4. tombh

    Thanks for the tip Paul.

    that’s a great resource your providing there, every little helps us web devvers :)

    Posted June 11, 2008 at 11:12 am | Permalink
  5. hi tombh, just wanted to point out that you need to have a pair of quotes around the image urls. Great page!

    Posted June 16, 2008 at 11:56 pm | Permalink
  6. tombh

    Though you don’t have to have quotes. Mind you it might not be valid CSS come to think of it. The reason I leave the quotes out is cos the syntax highlighting in my text editor (Quanta Plus) looks prettier like that :)

    Do you know if quoteless URLs in CSS would validate with W3C?

    Posted June 18, 2008 at 8:45 pm | Permalink
  7. According to http://jigsaw.w3.org/css-validator/ it will validate with or without quotes.

    Posted June 25, 2008 at 10:39 am | Permalink
  8. tombh

    That’s good to know, thanks Paul :)

    Posted June 27, 2008 at 8:34 am | Permalink
  9. James

    Hello, Would like to add a method that uses 0 images just pure css Css Rounded Corners

    Posted November 1, 2008 at 10:50 pm | Permalink
  10. this is not happening in firefox3.0.3

    #top_left{
    background: url(”image.jpg”) top left no-repeat;
    padding-top: 6px;
    padding-left: 6px;
    position: relative;
    }
    #top_right{
    background: url(”image.jpg”) top right no-repeat;
    width: 6px;
    height: 6px;
    position: absolute;
    right: 0%;
    top: 0%;
    }
    #bottom_left{
    background: url(”image.jpg”) bottom left no-repeat;
    width: 6px;
    height: 6px;
    position: absolute;
    left: 0%;
    bottom: 0%;
    }
    #bottom_right{ /*This is where the actual text goes*/
    background: url(”image.jpg”) bottom right no-repeat;
    }

    Hello there, I’m some content.

    Posted March 1, 2009 at 3:52 pm | Permalink

Post a Comment

Your email is never shared. Required fields are marked *

*
*