Rounded Images using CSS and some optional JavaScript
Regardless of what some people say, there is one area of rounded corners that hasn’t been dealt with much and that is rounding the corners of images. Before, if you wanted to round an image’s corners, you’d have to open it up in photoshop (or whatever image editor you use) and fix it up there. But no more…
See the demo
Download Rounded Images
Just add water
If you wish to just use Rounded Images right out of the box, extract the zip file and you will find scripts.js, index.html (a sample page), four .gifs, and a .PSD.
The four .gifs are made for a white background, but if you’re going to be including the image over a background other than white you can edit the .PSD to your own liking. To do so, first change the color of Color Fill 2 to match the background of your page, then rotate and save the image until you have all four corners. To rotate the image, simple go to Image –> Rotate Canvas –> 90° CW. You can show Color Fill 1 to help you see the curve so you know what way it is facing, but make sure it’s invisible before you save. Save the corners as topleft.gif, topright.gif, bottomleft.gif, and bottomright.gif.
Now upload the entire roundedimages folder into your root directory (so that it can be accessed from /roundedimages).
On the page where you wish to round an image’s corners, include the following in your header:
<script type="text/javascript" src="/roundedimages/scripts.js"></script>
<link rel="stylesheet" href="/roundedimages/style.css" type="text/css" />
Now wrap the image you wish to round with a div having the id of imagewrapper.
That’s it, you’re done. If you want more than one image rounded on the same page, read on.
Under the hood
So here’s how it works. First, we start with our XHTML:
<div id="imagewrapper">
<img src="bla.jpg" alt="An image of something." />
<span class="curve1"></span><span class="curve2"></span><span class="curve3"></span><span class="curve4"></span>
</div>
We need the four spans as style hooks for our four corners, and the imagewrapper div will be needed to help position these.
So first we need to get our imagewrapper ready to position the other spans inside it.
#imagewrapper {
position: relative;
float: left;
}
Since it’s postioned relatively, we can then absolutely position the corners around it.
#imagewrapper span {
position: absolute;
z-index: 2;
height: 12px;
width: 12px;
}
.curve1 {
background: transparent url(/roundedimages/topright.gif) top right no-repeat;
top: 0;
right: 0;
}
.curve2 {
background: transparent url(/roundedimages/topleft.gif) top left no-repeat;
top: 0;
left: 0;
}
.curve3 {
background: transparent url(/roundedimages/bottomright.gif) bottom right no-repeat;
bottom: 0;
right: 0;
}
.curve4 {
background: transparent url(/roundedimages/bottomleft.gif) bottom left no-repeat;
bottom: 0;
left: 0;
}
That’s all the CSS required. However, the 4 extra spans in our XHTML look a little messy, so let’s clean it up with some JavaScript:
window.onload = function() { ES.Round.create('imagewrapper'); }
var ES = new Object();
ES.Round = {
create : function(wrapper) {
for(var i = 1; 4 >= i; i++) {
var curve = document.createElement('span');
curve.className = 'curve'+i;
var target = document.getElementById(wrapper);
target.appendChild(curve);
}
}
}
That’s it. If you want more than one image rounded, you can just call ES.Round.create(wrapper) where wrapper is the name of the div surrounding the image to be rounded (just remember to edit your CSS file accordingly).
So download the package, have fun, and if you actually use this I’d love to see it. 












Sitepoint had an article about this a while back, but I can’t find it now. Obviously there is a limitation inherent in the styling of your #imagewrapper DIV that means the technique will only work on images that you want floated left (Sitepoint had the same problem).
I’ve been trying to create a JS method that copies the CSS styling of the image and applies it to the container DIV, but there are some cross-browser issues that make it pretty difficult.
Yeah, that is one limitation. Though, it can be floated to the right as well, just as long as it’s floated somehow.
I like the rounded corners, but there’s no anti-aliasing! But I still like the rounded corners
@Josh: That’s ’cause I used gifs. You could probably use .PNGs if you wanted, I just didn’t do that in the example for simplicity’s sake.
Elliot, I love the new site. It’s fabulous. I like the challenge you’re taking on here. Can’t wait to see where you take it. I’d love rounded image corners. The variety alone would be fun.
In IE6, the bottom curves don’t always show up in the right spot. In your demo, they are too low, when I do it on my test page, they show up too high. What is even worse, if I use a different image, they DO show up in the right spot. Very frustrating.
Oops sorry about that, I clicked the say it… anyways I like your rounded corners… great code. In IE though, I also found out that as the other user said that the corners on the bottom are too low. My temporary solution was to just set the position of curve 3 and 4 manually since I know the height of my picture. Either than that everything’s great. Great work
Exellent technique, I thank you sir!
I found the one on sitepoint you were talking about but couldn’t get it working. The technique you use works for me but only in Firefox (I’m on a Mac at the moment) and not Safari (yet I viewed the example in Safari which looked fine?!).
Just discovered your site through a link in sitepoint and am impressed, off to dig a little deeper into it now so many thanks for spreading the knowledge.
Here is the site I was working on where you can see the differences: http://www.firstdescents.com/gallery.cfm – look first in Firefox and all the rounded corners show up fine. However if you view in IE, most of the bottom corners are too high. MOST – because 2-3 of them show up OK. At time of this comment, 5th-7th pictures are OK, rest are not (they can change the pictures at any time). Very strange, but I think it has something to do with the pixel height of the image. (Just a guess!)
Hi,
this is a nice trick
Just wanted to say that with a little more trickstery (and a little more bloated generated html
) you can add whole borders to images. You can see how I’ve done it (which I’m afraid may be not the best way, but I’m still relatively a beginner) in this site I made for an italian women’s association.
Basically I’ve added a div for each side of the border, applying each side of the border as repeating backgrounds so it will fit any image, and finally letting the border show through using the appropriate padding values on the image (i.e. if you have a 10px border you’ll use 10px padding).
Last note: I’m using Joomla on that site and Joomla sticks to any image the width, height and an align attributes, so in my javascript I’m reading and using them, applying them back to the outermost div to have the right size and to float it correctly.
All in all this saved me a lot of work
Bye, Enrico
Nice idea with this site its better than most of the rubbish I come across.
Write a comment here.
it’s so useful code.. but how can i roud more images for beginer just like me…
pls refer this sample site… http://www.kjenglish.com/v2007/photo.php
pls email if you can… thank you
from Henry
South Korea
widekorea@naver.com
Hi! The code works great, but have you any idea how both IE6 and IE7 can show the effect as good as Firefox?
It seems like the bottom misses some pixels…
Thanks in advance!
[...] Rounded Images: questo script vi permette di creare al volo immagini arrotondate. [...]
[...] Rounded Images: questo script vi permette di creare al volo immagini arrotondate. [...]
hi there, wow, got live comment preview here. I just see the demo, look nice. It should works for any browsers, didn’t it. I think for IE we should has some CSS hack to view it perfectly as in Firefox. Anyway, the best alternate option to do this, i think we must resampled it back using PHP GD. Right? Thanks, great share.
I can’t see a demo, however are you limited to the site of the rounded corner? I’m looking for large rounded corners.
[...] | Elliot Swan (No Ratings Yet) Loading … Written by admin in: CSS, HTML, Javascript | Tags: CSS, [...]
This should use a class not a div. That way iy can be applied to as many images as necessary.
I have downloaded your demo, but dont work. Edited, modified, tried to debug… nothing.
Trashed.
Hi Elliot, I just found your site and I love the tutorial, excellent style , I think everyone that posts ideas should follow your lead, brilliant flow.
The corners worked wonderful and it is just a matter to play with it to apply it to your needs, I was looking for a while for it.
I will post the site where I am applying ot later
Thank you very much,
Javier
hi Eliot, this is really useful information for beginner web developer
Thanks for the info, Eliot. It’s simple and easy!
In IE7, there is one issue when the images are in a DIV that has a horizontal scrollbar (DIV has a “width” set and “overflow” set to “auto”). The images seem to appear fast the width of the DIV and scrolling left/right does not move any of the images.
Any ideas on how to get around that?
Thanks,
-Leslie
works great. thanks for taking the time.