Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Rounded Corners with CSS


:P
On this page:

In an earlier post I posted some issue with consistently creating rounded corners and was lamenting that there’s no easy way to do this using an all CSS solution. Instead I used some fairly ugly HTML with hardcoded links:

<table width="100%" border="0" cellpadding="0" cellspacing="0">

<tr>

    <td><img src="images/Corner_ul.gif" style="height:30px;width:20px;"/></td>

    <td class="menuband" style="padding:0px;width:200px;text-align:center;">Shopping</td>

    <td><img src="images/Corner_ur.gif" style="height:30px;width:20px;" /></td>

</tr>

</table>

 

Two problems with this : Obviously this stuff is hardcoded and it doesn’t lend itself to theming. Specifying images in a CSS file makes the layout easier to deal with and follow a more adaptable theming/templating approach.

 

After some more thought on this: It can be done with CSS only although it still requires some configuration.

 

First a couple of assumption. I need to use images rather than one of the auto-rounding routines mentioned in the previous post, because I need to be able to overlay the rounded corners on image backgrounds. The auto-solutions that I’ve seen can’t deal with that as they sniff the background color and work with that.

 

Assuming I want to use images for the corners so that I can properly handle transparency I can create three style tags for the corners and the center:

 

.roundedcorner_ul

{

      float:left;

      display:inline;

      height:25px;

      width:20px;

      background: url(images/corner_ul.gif);

}

.roundedcorner_ur

{

      float:right;

      display:inline;

      height:25px;

      width:20px;

      background: url(images/corner_ur.gif);

}

.roundedcorner_center

{

      height:20px;

      display:block;

      text-align:center;

      padding-top:5px;

      font-weight: bold;

      margin-left:20px;

      margin-right:20px;

      background-color: #003399;

}

 

Note that I specify the two images and float them both to the left and right respectively. The size of the tag should match the image size exactly. The center tag then sets the margins to start rendering where the images leave off – this is so that the background color of the center div doesn’t show behind the images, so you can still get transparency.

 

Now to put this on the page:

 

<div>

    <div class="roundedcorner_ul"></div>

    <div class="roundedcorner_ur"></div>

    <div class="roundedcorner_center">Shopping</div>

</div>

 

While still verbose all the ugly style stuff is out of it. More importantly and maybe the main reason I want to do this is that want my images to be style sheet relative rather than application relative so the images can be picked up from the ASP.NET Theming directory rather than from an application relative location.

 

Using images is a bit of a pain because if you change colors of a theme you’ll need to change the image colors along with it. But in this scenario I suppose this works out Ok. 

 

Maybe creating a rounded corner generator HttpHandler is the way to go, generate then cache the images... left for another day <g>...

 


The Voices of Reason


 

Ron
June 13, 2006

# re: Rounded Corners with CSS

FYI - Mozilla based browsers support rounded corners directly in CSS with something like: moz-border-radius

Last I heard IE7 was going to implement something similar as well.

In all reality, cutting some images and using smart CSS isnt all that bad.

Marc Brooks
June 13, 2006

# re: Rounded Corners with CSS

You're fighting too hard, Rick. Use CSS more completely and specify MULTIPLE classes to get the behavior you want:

.roundedcorner
{
float:left;
display:inline;
height:25px;
width:20px;
}

.ul
{
background: url(images/corner_ul.gif);
}

.ur
{
float:right;
background: url(images/corner_ur.gif);
}

.center
{
display:block;
text-align:center;
padding-top:5px;
font-weight: bold;
margin-left:20px;
margin-right:20px;
background-color: #003399;
}

<div>
<div class="roundedcorner ul"></div>
<div class="roundedcorner ur"></div>
<div class="roundedcorner center">Shopping</div>
</div>

Note the blanks in class... the right-most trumps the left class, so you get the same effect but you can now change one class to set heights and such...

Of course the canonical way is using a UL tag with to set the heights and then doing the LI styles for the corners.


bdiaz
June 13, 2006

# re: Rounded Corners with CSS

Here is a link to some examples from the Rico library:

http://www.openrico.org/rico/demos.page?demo=rico_corner

May be a bit much, but I really like their javascript library, which is also built on top of the prototype library:

http://prototype.conio.net/

Hope that helps!
Bobby

Rainer
June 14, 2006

# re: Rounded Corners with CSS

Rick, have you heard the sliding doors-technique? It means creating a flexible box, that
can expand horizontally as well as vertically. Four images are required for this technique to work,
so you will have to add a couple of extra, nonsemantic elements to your markup.

Like::

<div class="box">
<div class="box-outer">
<div class="box-inner">
<h3>Header</h3>
<p>Content</p>
</div>
</div>
</div>


The top two images make up the top curve, and the bottom images make up the bottom curve and
the body of the box. The bottom images need to be as tall as the max. height of the box.

-------------------- ---
| top-left.gif | | | top-right.gif
-------------------- ---
-------------------- ---
| | | |
| bottom-left.gif | | | bottom-
| | | | right.gif
| | | |
| | | |
| | | |
-------------------- ---

First you add the bottom-left.gif to the main box div and bottom-right.gif to the outer div.
Next you add top-left.gif to the inner div and finally top-right.gif to the header. Lastly,
it's a good idea to add some padding to space-out the contents of the box a little.

Like::

.box {
width: 20em;
background: #effce7 url(images/bottom-left.gif) no-repeat left bottom;
}

.box-outer {
background: url(images/bottom-right.gif) no-repeat right bottom;
padding-bottom: 5%;
}

.box-inner {
background: url(images/top-left.gif) no-repeat left top;
}

.box h3 {
background: url(images/top-right.gif) no-repeat right top;
padding-top: 5%;
}

.box h3, .box p {
padding-left: 5%;
padding-right: 5%;
}

Rainer
June 14, 2006

# re: Rounded Corners with CSS

Sorry, tried to draw a box of the pics, but it rendered not so good <g>

Russell Garner
June 14, 2006

# re: Rounded Corners with CSS

I think you might be after something like the ThrashBox:

http://www.vertexwerks.com/tests/sidebox/

Incidentally, re: the HttpHandler. I've already written it, though I haven't put it live anywhere. It supports generating the four images that the semantically correct ThrashBox above requires. It uses GDI+ to render JPG/PNG/GIF as required for each of the images and supports gradients and anti-aliasing. It doesn't cache yet, but that would be an easy addition. It also supports generating glass buttons and so on and I'd be interested in your opinion on where next to go with it :)

Drop me a mail at rgarner (at) zephyros-systems.co.uk if you'd like a copy.

Rick Strahl
June 14, 2006

# re: Rounded Corners with CSS

Thanks for the pointers guys.

Marc - Ok, but that's not a huge improvement <g>. Yeah, I should be better about CSS reuse, but it also makes it a little harder to find the right tags as you have to find multiples.

Rainer - Looked at Sliding Box and Onion Layer model a few times and it's a bit complex to look at after you've been away from it for a 5 minutes <g>. At the moment I only need single borders and I think the above is easier. For full boxes the above might be a better choice.

Russell - I wish I'd saw your entry earlier, because is spent some time building a rudimentary corner Handler that spits out just the end pieces. Works but there are a few problems - mainly image transparency for and render quality for gif images (and output with PNG files - see next entry <g>).

James Conley
June 14, 2006

# re: Rounded Corners with CSS

Have a look at Spiffy Corners.
http://spiffycorners.com/

Rick Strahl's Web Log
October 15, 2006

# Graphically challenged - Rick Strahl's Web Log

I've been spending a bit of time over the last week cleaning up some of the 'design' of a couple of applications. I'm no graphics designer and I often feel graphically challenged. Here are few thoughts, hey even a question and a few links for things that I found useful.

PeteL's Blog
July 04, 2007

# PeteL's Blog : Rounded Corners in CSS

I was talking with a friend last night, and he was lamenting the problems that he was having with CSS. Not so much the problems, but more the fact he was just starting to learn it and wasn't really enjoying it so much. While we were talking, I tried to

Mathews
December 29, 2007

# re: Rounded Corners with CSS

Guys,

You all are speaking in tonques! I have just started on CSS and great, I have been able to understand the <div> and create boxes without tables. But I love the 'rounded corner' effects.

If someone could help in plain, simple english that would be great!

Cheers

# fat burner cream weight loss

thanks so much. Good writing

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024