Your Ad Here

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.

 

Kurt
May 27, 2008 12:44 AM

Thanks! Great explaining of the code, will help my css design skills greatly!

Sam
June 11, 2008 5:32 PM

Thats cool way, but i hope CSS nerds like you can figure out a way for us to do rounded corners without creating the images.

Thanks!

Stuart
June 28, 2008 9:20 PM

I have read through a lot of feedback where people say they are getting a bar a few pixels high at the bottom of thier box (in IE). I haven’t found a straight answer on how to fix this. Would you please save me some time and let me know?

Thanks!

dean
July 1, 2008 4:10 AM

for a way without images try googling “niftycorners”. this is a very good way also though.

Midnight Umbreon
July 14, 2008 6:16 PM

Okay. So. Doing this layout: http://www.midnight-umbreon.net/other/layout.png and. I only need one rounded corner. The top right is done by the header image. I only need the bottom right. So, I created three other images that are just squares. Would this work?

Sarah
July 15, 2008 2:42 PM

Have to agree with Stuart — what can we do about the bar at the bottom?

Umar Bukhari
July 19, 2008 3:17 AM

It is not just good way. It is so simple you are saying about but I want to never use any kind of image. Only use colors and then make it round corners. it is no matter that what do you use, table or div. but i need you to use colors and and make round corners.

John
July 20, 2008 6:53 AM

try giving 0 margin and 0 padding of each div. I understand different browsers have different default values and this may be causing the bar at the bottom.

twoja stara
July 21, 2008 12:11 PM

OMG. You say DIVs are so great.

Then create a good 3x3 box code in few minutes, just like when using tables. Tip: the code above is not a GOOD one. In fact it sucks hard: it will break the transparent (gif, png) corners and borders.

Is it so hard to create a REAL 3x3 layout with DIVs?!

DIVs are theoretically nice, but they do suck like hell in the real life.

davidoff
July 24, 2008 7:36 AM

twoja stara, why so negative?

divs eat tables for breakfast, you just need to get to know them…

thanks for the excellent code Adam

Nav
July 29, 2008 2:25 AM

Hi Adam, nice example its great but i have an question is it possible we can do it same with border corner .imeans to say we have a rounded box with border how to make tht in html

ed
July 29, 2008 11:19 AM

Well, it worked even in safari, in addition to all other browsers… I reallized that I can do this css corners using a background image (I mean the entire thing using image). it may be bad since it is going to take more time for download.. ur solution is better, but if u lazy, u can create 1 image using adobe, and place it as background.

alan
August 8, 2008 2:48 PM

Great work, worked on all browsers.. gonna use it on my website :)

Thanks, Alan

sa
August 17, 2008 8:48 AM

Great css, it worked on ie, and firefox for me.

Thanks

a
September 4, 2008 2:12 PM

i dont gett itt :-| everyone says its really easyy omg im soo thick! pleasee explain.

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


Your comments:

Text only, no HTML. URLs will automatically be converted to links. Your email address is required, but it will not be displayed on the site.

Name:

Email: (not displayed)

If you don't feel comfortable giving me your real email address, don't expect me to feel comfortable publishing your comment.

Website (optional):

Lijit Search

Best Of

  • 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.
  • Where do the RSS ad startups fit in? Yahoo's RSS advertising service could spell trouble for pure-play RSS advertising services unless they adapt their business model.
  • Let it go Netscape 4 is six years old.
  • The importance of being good Starbucks is pulling CD burning stations from their stores. That says something interesting about their brand.
  • The mouse and me Not only is the mouse very destructive, but it's evaded all attempts to capture or kill it so far.
  • More of the best »

Recently Read

Get More

Subscribe | Archives

Recently

Cloud Reliability (Aug 12)
Would you like to take bets as to whether Amazon or Google have better reliability and safety than your local network service providers?
George Carlin (Jun 22)
"I'm always relieved when someone is delivering a eulogy and I realize I'm listening to it."
Business lessons from the Kitchen (Jun 9)
The Gordon Ramsay School of Business
Under The Radar twittering (Jun 3)
My live stream from Under the Radar
Measuring a CEO's mind (May 29)
Not everything that's important can be measured. Not everything that can be measured is important.
Golden 1: breaking customer expectations (May 25)
Take a potential new user and give them a poor signup experience, then call them a liar.
Sprout Test (May 7)
A test post for Sprout widgets.

Subscribe to this site's feed.

Elsewhere

Feed Crier
Get alerted by IM when your favorite web sites and feeds are updated.
SacStarts
The Sacramento technology startup community.
Pinewood Freak
Pinewood Derby tips and tricks
Del.icio.us
My tagstream at del.icio.us.
Waddlespot
My son's Club Penguin community. News, blogs, tips, and tricks.

Contact

Adam Kalsey

Mobile: 916.600.2497

Email: adam AT kalsey.com

AIM or Skype: akalsey

Resume

PGP Key

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