Rounded corners in CSS

I was talking to Jesper about the dotted CSS borders trick and the subject of rounded corners in CSS came up so I showed him my method. There are other ways that I’ve seen it done, but the other methods always require lots of complex HTML and CSS. I figure that lots of nested divs aren’t much better than using a table, so my way doesn’t require much in the way of HTML or CSS. Here’s how I do it.

Create four images for your corners. Most graphics programs have a tool that will create rounded-off squares. I’ll be using this square here…

…and I’m going to cut off the corners to get my four images:

In the spot where I want the box to show up, I create a container div to hold the box, a div for the top row and a div for the bottom row. Between the top and bottom rows, I add my content. In the top and bottom row divs, I add the left corner image and set the inline style to read display: none;. This makes the image invisible unless I make it visible through the stylesheet. That way, I can hide the effect from incompatible browsers by not showing them the stylesheet.

<div class="roundcont">
   <div class="roundtop">
	 <img src="tl.gif" alt="" 
	 width="15" height="15" class="corner" 
	 style="display: none" />
   </div>

   <p>Lorem ipsum dolor sit amet, consectetur adipisicing 
   elit, sed do eiusmod tempor incididunt ut labore et 
   dolore magna aliqua. Ut enim ad minim veniam, quis 
   nostrud exercitation ullamco laboris nisi ut aliquip 
   ex ea commodo consequat. Duis aute irure dolor in 
   reprehenderit in voluptate velit esse cillum dolore eu 
   fugiat nulla pariatur. Excepteur sint occaecat cupidatat 
   non proident, sunt in culpa qui officia deserunt mollit 
   anim id est laborum.</p>
  
   <div class="roundbottom">
	 <img src="bl.gif" alt="" 
	 width="15" height="15" class="corner" 
	 style="display: none" />
   </div>
</div>	

The CSS sets the width and background color of the container object and the color of the text inside. The margins on interior paragraphs are set so that the text doesn’t sit right up against the edge of the box.

Then the top and bottom divs are given a background image that contains the right corner images and the left corner images from the HTML code are enabled.

.roundcont {
	width: 250px;
	background-color: #f90;
	color: #fff;
}

.roundcont p {
	margin: 0 10px;
}

.roundtop { 
	background: url(tr.gif) no-repeat top right; 
}

.roundbottom {
	background: url(br.gif) no-repeat top right; 
}

img.corner {
   width: 15px;
   height: 15px;
   border: none;
   display: block !important;
}

Here’s how it looks when it’s all put together.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

This works in IE6, Mozilla 1.3, and Opera 7 on Windows. It should work in most other modern browsers as well.

Update: Julian Bond asked if this will work with the container div set to 100% width. Here’s the box again. The only thing changed is the width of the roundcont class.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

 

Adam Kalsey
April 3, 2009 9:39 AM

Perdele: That’s not valid CSS. That won’t work in any browser at all.

Stephen
April 6, 2009 12:43 PM

How would you do this with an outline?

Roxana Castañeda
April 21, 2009 12:16 PM

Hi Adam,

Thank you very much for your code. It works beautifully.

Now, how would you do it if you wish the borders with a different color than the inside?

Thanks in advance for your help.

Warm regards,

Rox from hot Lima in Peru, South America

Yonas Teka
April 30, 2009 8:29 AM

very good method to use . it might be better if there is a way to curve the edges without the images

Tim
May 15, 2009 8:52 AM

I have this same problem.

Hi I want a different color as background color, but then these white edges appears arround my corners.

Any solutions for this?

=(

Michael
May 17, 2009 6:50 PM

TIM - I think the white edges around your corners are caused by a white matte on the .gif img corners.

I’m guessing that you have a fairly dark background - right?

TO FIX - re-create the corners in whichever graphics program you use. When you save for web set the matte to the same color as your background.

STEPHEN - Easiest way to get a border around this is to recreate the image with a border and save them with no transperancy, then just use border: solid 1px #ccc; on the class .roundcont. Ajust colour and size to suit and you’ll have a rounded corner box with a border.…

Warbo
June 8, 2009 1:41 PM

This may look like a lot of coding to do round corners, it really saves a lot of effort if you have to make more than one round-cornered box. You only have to write the css once and you can use it over and over.

Plus, it’s a lot easier to manage than the old nine-cell table method.

Larry
June 16, 2009 10:53 PM

Good tutorial if I’ve got complex imagery to use as a background. For the easy, everyday rounded corners and rounded div backgrounds (widgets), I use www.easyimg.com.

Thanks again.

Web Design Sussex
June 18, 2009 6:44 AM

I believe in Microsoft Web Developer Studio or whatever they call it, you can do rounded corners at the click of a button… whats the chances that its not compliant code though!?

Keenan
June 21, 2009 1:53 AM

Hi Adam,

Your css works great,

http://evolution.server101.com/contacts.htm

I used it for the trading hours.

Question for you, how do I put it with in the existing dive that makes up the right content instead of it clinging to the left edge like it does now.

Mordy
June 25, 2009 12:38 AM

Effective but still easy to follow…I wish those two things went together more often :)

I’ve been looking for a technique simple enough to convert into a reusable user-control for an asp.net project I’m working on and this might do it.

Thanks for sharing Adam - much appreciated.

red
July 6, 2009 1:15 AM

Keenan, Adam’s css technique is super but your site.…. :)

hasfa
July 19, 2009 1:04 AM

nice tutorial and easy to understand. thanks Adam smile

hasfa
July 19, 2009 1:04 AM

nice tutorial and easy to understand. thanks Adam smile

Godly
July 22, 2009 12:50 AM

Its a nice Coding thank you…

These are the last 15 comments. Read all 360 comments here.

This discussion has been closed.

Follow me on Twitter

Lijit Search

Best Of

  • Embrace the medium The Web is different than print, television, or any other medium. To be successful, designers must embrace those differences.
  • Customer reference questions. Sample questions to ask customer references when choosing a software vendor.
  • Simplified Form Errors One of the most frustrating experiences on the Web is filling out forms. When mistakes are made, the user is often left guessing what they need to correct. We've taken an approach that shows the user in no uncertain terms what needs to be fixed.
  • Debunking predictions Read/Write Web's authors have some goofy predictions.
  • The best of 2006 I wrote a lot of drivel in 2006. Here's the things that are less crappy than the rest.
  • More of the best »

Recently Read

Get More

Subscribe | Archives

Recently

Ideas, Risk, and Investors (Jan 1)
Over at SacStarts, I have piece up discussing a common question I get from entrepreneurs....
VoiceXML for web developers (Dec 17)
Building voice applications isn't hard at all. Any web developer can do it.
De-skunking a dog (Oct 27)
How to clean up your pet after a skunk attack.
Pressure sales via Twitter (Oct 16)
Sticking an ad in my face when we first meet is a good way to lose my interest.
Loma Prieta, 20 years later (Oct 13)
Looking at the earthquake from October 17, 1989
Red light cameras don't work (Oct 13)
Cameras installed to catch people running red lights aren't about traffic safety at all.
Jack-o-lantern pumpkin carving patterns (Oct 12)
It's a tradition, what can I say?
SEO realities (Oct 12)
The real search engine optimization. Works every time.

Subscribe to this site's feed.

Elsewhere

IMified
Build instant messaging applications. (My company)
SacStarts
The Sacramento technology startup community.
Pinewood Freak
Pinewood Derby tips and tricks

Contact

Adam Kalsey

Mobile: 916.600.2497

Email: adam AT kalsey.com

AIM or Skype: akalsey

Resume

PGP Key

©1999-2010 Adam Kalsey.
Content management by Movable Type.