views:

61

answers:

4

What is the best way to make something like a mask with rounded corners for an image using CSS/JS/HTML? So, I need to add rounded corners to a rectangle image. I thought about adding 4 graphic elements like this one alt text above the image at its corners to hide some little parts of the image. Here red color is, for example, for using on the red background page, and the element is for right top corner. The problem with this solution is that I can't use it on complex backgrounds, like gradients or other non-flat fill background. I know there is a masking feature that can be used in FireFox but I need some more generic solution that will work in other browsers well too. Thanks.

+1  A: 

The best and simplest way is to use the CSS3 border-radius property:

.box {
    -moz-border-radius: 4px;
    -webkit-border-radius: 4px;
    border-radius: 4px;
}

It works in all modern browsers apart from IE8 (works in the new IE9 though).

DisgruntledGoat
I think this does not work on images...
elektronikLexikon
Actually, I don't think I can use CSS3 features for this project now as I need it to be supported by different browsers including ie6
Sergey Basharov
If IE 8 is not supporting this, how can you ever state that ALL modern browsers support it? IE8 was not release that long ago and can be seen as quite modern. Also, how is IE7 handling this?
Gertjan
I wonder what would happen if you gave a div rounded corners and made it slightly smaller then the contained image. Perhaps playing with the overflow property... It might produce the correct effect.
colithium
@Sergey, and if you use www.css3pie.com ? It enables css3 tricks for older IE's
Litso
Replies: (1) Actually it does work on images, in Chrome at least. (2) http://dowebsitesneedtolookexactlythesameineverybrowser.com - if some browsers get square corners then so be it :p (3) I said it works in all modern browsers *apart from IE8*.
DisgruntledGoat
Well, are rounded corners a requirement or a nicety? In the latter case, I would go for using CSS, and just leaving non-supporting browsers with square corners.
Dykam
Ok, I think the best 2 variants will be: 1. using CSS modern features and ignoring those ones that don't support it. 2. Using PHP to generate round corners. Thanks.
Sergey Basharov
+2  A: 

This is something that's difficult to get right in one browser, let alone all the common ones. I suggest you do your processing on the server side. If you're working with PHP, I know it has a built in image library that can work with semi-transparent png's. That's your best bet. Simply "crop" it once and save it on the server's file system. Look for an equivalent library if you're not using PHP.

(By "crop" I mean add the rounded corners with a nice alpha blending effect fading to a transparent background).

colithium
A: 

You can always look at using the nifty corners if you need it to work in older browsers also.

Or you can use the css border-radius as mentioned above, and just accept that in IE6/7/8 it will be square.

Ant Ali
+1  A: 

You should be using CSS border-radius for this (as described in another answer). It does work for images.

What the previous answer missed is that you can support it in CSS in all browsers, including IE6/7/8 using a wonderful little hack called CSS3Pie.

Spudley