Karate Corners: XHTML/CSS Rounded CornersOct 6, 2008

I’ve seen a lot of different ways to create round corners and boxes in web sites, and quite frankly I haven’t exactly fallen in love with any of them. A lot of the methods that I’ve seen use either (1) a table structure which I try to avoid at all costs, (2) too many nested <div> tags, (3) complex CSS, or (4) too many different images that have to be loaded all at once.

Update: Read my subsequent post for a more advanced example of this technique!
Update: Read a even more advanced version of this technique in an even newer tutorial!
Update: Find out how to automatically generate the HTML and CSS for this technique using jQuery.

I took the best of the best and came up with a very simple way to create totally scalable boxes with round corners. I’ve tested this on Internet Explorer 6, IE 7, Mozilla FireFox 2, Firefox 3, Opera 9, and Safari 3.

The Code

The HTML code is relatively simple. You have an outer <div> tag with four “corner” <div>s inside of it. Each of these corner <div>s are positioned with CSS so that they always appear at each corner of the outer <div> box.

HTML:

<div class="cornerBox">
    <div class="corner TL"></div>
    <div class="corner TR"></div>
    <div class="corner BL"></div>
    <div class="corner BR"></div>
    <div class="cornerBoxInner">
        <p>Lorem ipsum dolor sit amet.</p>
    </div>
</div>

CSS:

.cornerBox {
	position: relative;
	background: #cfcfcf;
	width: 100%;
}
.corner {
	position: absolute;
	width: 10px;
	height: 10px;
	background: url('corners.gif') no-repeat;
	font-size: 0%;
}
.cornerBoxInner {
	padding: 10px;
}
.TL {
	top: 0;
	left: 0;
	background-position: 0 0;
}
.TR {
	top: 0;
	right: 0;
	background-position: -10px 0;
}
.BL {
	bottom: 0;
	left: 0;
	background-position: 0 -10px;
}
.BR {
	bottom: 0;
	right: 0;
	background-position: -10px -10px;
}

The Image

The 20x20 corner image used in this demo.

Only one image is used for this box (shown above). In my example, I created a 20×20 circle, which is comprised of four 10×10 corners. By shifting the background image around, you can eliminate the need to download four separate GIF files on each of your boxes.

The Result

Click on this image to see the demo, where you can resize your browser window to see how the box scales as the height and width changes.

Karate Corners! Click on the image above to see the HTML demo. Happy styling!

Share this article:
  • Digg
  • del.icio.us
  • StumbleUpon
  • Twitter
  • Facebook
  • LinkedIn
  • Technorati
  • Mixx
  • Design Float
  • Google Bookmarks
  • RSS
  • PDF
  • Print
  • email

58 Responses to “Karate Corners: XHTML/CSS Rounded Corners”

  1. Comba says:

    Kyle,

    It seems IE 6 that Im running is not displaying the correct border on Bottom-Left, Bottom-Right and Top-Right. I think it has something to do with background-positioning. It displays the Top-Left corner on all the rest of the corners. Do you think its a syntax problem? What should I look out for?


  2. [...] that the demo also utilizes Karate Corners to create rounded edges on the tabs. Learn more about Karate corners here. Share this [...]

  3. omkar says:

    Simple gr8 …….

  4. Abhinav says:

    The easiest and shortest way I’ve seen till now.

    Thank you…

  5. colin says:

    Were have you been?! Love this, I hated the other methods of using 4 graphics and tables and whatever else. This image sprite ROCKS and is easy to adapt my image/border/color, just ONE true image. I’d tattoo this url to my arm but it’s a bit long, i’ll just Tweet instead.
    SO SIMPLE, jealous you got it first!

  6. Muhammad Ali FArooq says:

    this is the best and very simple way to create totally scalable boxes with round corners.
    i have not implemented this yet but the idea is very good.

    thanks, you saved my day

Leave a Reply