The other day I was trying to style CSS3 border-radius to image element and I realized that Firefox doesn't display border-radius on images. Then I figured a way to work around it — wrap a span tag around with the original image as a background-image. Thanks to Darcy Clarke for the jQuery code which does the magic tag wrapping automatically.
Goal
My goal to use the CSS3 border-radius and box-shadow feature to make the image element look like the screenshot below.

Problem
The problem is none of the modern browsers display rounded corners image the way I want it. Webkit does display the rounded corners, but the inset box shadow is not supported. In Firefox, the border-radius doesn't even display at all.
The CSS Trick
The trick is very simple: wrap a span tag around the image element. Specify the original image as background-image. To hide the original image, specify opacity:0 or display:none. I find using the opacity method is a better approach because the image will remain available for copy or download.
Final Solution With jQuery
To make things easier, we can use jQuery to automatically wrap a span tag around the image.
The jQuery code below will find any element with ".rounded-img" or "rounded-img2" (in my case, it is the image element) and wrap it with a span tag. The script finds the src, width, height, and CSS class attribute of the original image and apply them as inline styling in the span tag. Then it specifies the opacity of the image to 0 to hide it.
It works with any image dimension (with or without the width and height attribute). It can also be combined with other CSS classes. No additional markup is required.

<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".rounded-img, .rounded-img2").load(function() {
$(this).wrap(function(){
return '<span class="' + $(this).attr('class') + '" style="background:url(' + $(this).attr('src') + ') no-repeat center center; width: ' + $(this).width() + 'px; height: ' + $(this).height() + 'px;" />';
});
$(this).css("opacity","0");
});
});
</script>
Sample Usage
I hope you will find this trick useful. For example, you can use it to style your blog's avatar or profile photos.
Credits
Thanks to Darcy Clarke for the jQuery code.



I think CSS3 need some more time to grow as we clearly know whats the problem is… we are still struggling and crumbling our code because of these browsers.
Anyways… Nice work and keep it coming :)
Nice Tip!!! Thanks for share!! =0)
That’s really useful ! nice and easy !
Excuse me if this has been covered in one of the comments already, but inset box-shadows do work in webkit browsers.
/* Safari & Chrome */
-webkit-box-shadow: inset 0px 1px 3px rgba(0, 0, 0, .5);
/* Firefox */
-moz-box-shadow: inset 0px 1px 3px rgba(0, 0, 0, .5);
/* Opera and W3C */
box-shadow: inset 0px 1px 3px rgba(0, 0, 0, .5);
Like I said… I haven’t read all the comments, but I thought it was worth noting. (all tested on Windows XP).
Greetz,
Wouter
Very nice script !
I was bored so I created a jQuery plugin for this lol… it’s very basic but it executes in about 0.005ms as opposed to your 0.020ms. Every little helps as they say ;)
You just need to include the plugin file and then execute $(yourSelector).roundImg(radius) on DOM ready.
E.g: http://jsfiddle.net/JJenZz/3gYqj/
I included the unminified code at that URL too so that you can see how it’s built but you don’t need to save that in your plugin file :)
This is cool, thanks.
I hope it works with IE9
Great tuts. Nowadays everything can be settle with a CSS and jQuery
Nice tutorial man.. thanks..
I actually did this a while back on a project for school http://www.cs.iupui.edu/~ar43/a455/zenGardenFinal/ – But the inset effect is very cool!
Oh, I just realized you used jQuery for this? My rounded images are all done with CSS on this page http://www.cs.iupui.edu/~ar43/a455/zenGardenFinal/
Wow! Exilent!
This is a quality article! I’ve been reading quite a few articles recently on CSS3 rounded corner implementations, and this is one of the best I’ve read. I’ll certainly be using this implementation in my work from now on.
Thanks for the great article.
Cheers,
Keith
jQuery is cool :)
Thank you, I have come across this several times lately. My fix have been similar but never with the use of jquery. Its a great idea.
Great solution, thanks as always.
it’s cool
yeah it would be such a waste not to show the rounded edges! great!
wonderful article very helpful indeed.
i already try that, and the result are nice