Quick and dirty rounded corners on an image

Written by That Web Guy on 22nd May 2009. 15 comments

Quick and dirty rounded corners on an image
  • Avant Innovations
  • Advertise Here

I got to a certain stage during the development of this blog when I realised I was going to want rounded corners on the article images. I was first reminded of a complex div/gif arrangement, and then my mind shifted to just simply Photoshopping them when needed. Neither are desirable so I came up with this quick and dirty solution.

Before you get too excited bear in mind this method is only good if the image you want to apply rounded corners too has consistent dimensions, that way you can swap the image out without any extra work.

So for this tutorial we will assume the image is going to be 150 x 150px.

Part 01: Prepare a transparent PNG

Open your preferred image editor and create an image with a blank canvas and add some rounded corners. The package I've linked at the end of this article contains one I prepared earlier.

Part 02: The HTML

In your document create a DIV with a class called rounded and place the transparent rounded cornered image inside it.

<div class="rounded">
<img src="corners.png" alt="" />
</div>

In your CSS file create the style for the rounded class.

.rounded {
width:150px;
height:150px;
}

Now the quick bit is done, onto the dirty. Place an image into the opening DIV using inline CSS:

<div class="rounded" style="background-image:url(leaves.jpg)">
<img src="corners.png" alt="" />
</div>

But what about the ALT attribute? If you want to validate it will need to go on the PNG image.

<div class="rounded" style="background-image:url(leaves.jpg)">
<img src="corners.png" alt="Leaves" />
</div>

And if you need to add a link:

<div class="rounded" style="background-image:url(leaves.jpg)">
<a href="http://www.leaves.com"><img src="corners.png" alt="Leaves" /></a>
</div>

That's it - you're done. I did say it was quick and dirty. It validates so I'm happy to use it when needed.

Tested in Firefox 2, Firefox 3, IE7, IE8, Chrome, Safari and Opera. IE6 should be fine but you will need a script to handle the transparent PNG otherwise you'll just see a light blue box.

An accessibility consideration

As the PNG is technically a decorative image without meaning, some could argue that you should leave the ALT attribute empty. But as the ALT attribute is intended to help people with screen readers get a better idea of what's on your page, I reckon it's appropriate to put something inside the ALT in this case. The picture (the one visible under the PNG) still needs explaining for the visually impaired, where it actually is or how it's implemented doesn't matter.

twgb_rounded_corners.zip (13k)

Is this worth sharing?

That Web Guy

About That Web Guy

That Web Guy (Mikey to his friends) is a veteran web designer based in Perth, Western Australia, and currently Design Director at Perth Web Design. When he's not XHTML'ing or messing around in Photoshop, Mikey can usually be found preaching web standards evangelism onto unsuspecting victims.

Feel free to send That Web Guy a message some time, follow him on Twitter, or make a donation.

Comments

Not a Member

Alberto Vera

Thanxs, that is a great dirty way (i will used it quite a lot) nice!!!!

Saturday 23rd May 2009 | 02:18 AM Reply Comment URL Back to top

Not a Member

Jose

Not as dirty as you think. Love it thanks.

Saturday 23rd May 2009 | 06:15 AM Reply Comment URL Back to top

Kim OJ

Kim OJ

That's clever, though it is a little dirty. I might just have to use that trick for rounded corners, and maybe other little tweaks. Thanks.

Saturday 23rd May 2009 | 06:41 AM Reply Comment URL Profile Back to top

CSS Babe

CSS Babe

Its one of those little tips you wonder why no one thought of it before.

Saturday 23rd May 2009 | 07:28 AM Reply Comment URL Profile Back to top

Not a Member

Kimmo

This was a good read but it got me thinking: there's quite a lot of markup for such a simple thing. You know, the background image could be assigned straight to the img element. That would also be quite a simple thing to automate using JS without losing the accessibility focus. And while we're at it, we could throw in the magnificent DD_belated_png script to fix PNG alpha issues with IE6 but in this case it's not needed as IE6 is (rightfully) discarded.

Saturday 23rd May 2009 | 03:46 PM Reply Comment URL Back to top

Not a Member

Marty

Responding to this comment by Kimmo

That would work but you are proposing is invalid in XHTML strict unless it's surrounded by a p or a h1 or something else. You can't put a p or a h1 (or any other heading) around it unless you want to disregard semantics because it's neither a paragraph or a heading, so that leaves you with a div as your only option which makes it almost the same as what's shown here anyway.

Though if you don't care for semantic mark-up then your method is fine Kimmo.

Saturday 23rd May 2009 | 05:10 PM Reply Comment URL Back to top

That Web Guy

That Web Guy

Responding to this comment by Kimmo

Thanks Kimmo. I don't see any problems with it working your way except for the semantic issue Marty already raised. But if it's HTML Transitional it will at least validate without having to surround the img tag with anything. I only ever do XHTML Strict and try to keep my mark-up semantically correct when possible though I admit to being lazy sometimes :-P

Saturday 23rd May 2009 | 05:53 PM Reply Comment URL Profile Back to top

Not a Member

Carerra Davis

Too much emphasis is placed on semantics. I like Kimmos way for my transitional pages.

This is a lovely design you've got going on. Hope you don't mind if I steal some ideas from it!

Saturday 23rd May 2009 | 05:57 PM Reply Comment URL Back to top

Not a Member

Harun Smrkovic

Nice one !
This is good when using a CMS solution. You could create a few presets for this, and then link it to selected mode for image in database, so when you add bigger images, it re-sizes them to your preset and set it as wanted.
I don't know how much you know about php and mysql, but man, I LOVE your design style.

Sunday 24th May 2009 | 01:20 AM Reply Comment URL Back to top

That Web Guy

That Web Guy

Responding to this comment by Harun Smrkovic

That's pretty much what I have happening here. When I create a new article I browse for an image which is resized and cropped automatically and then echoed out on the page in that div. My php and mysql skills could be much better than they currently are - they're a 3rd language to me :-)

Thanks for the design compliment!

Sunday 24th May 2009 | 06:25 AM Reply Comment URL Profile Back to top

Not a Member

Antone

Responding to this comment by Carerra Davis

I can't believe you said that if you're a web designer or developer. Awesome design here Web Guy.

Sunday 24th May 2009 | 06:32 AM Reply Comment URL Back to top

Storm

Storm

Neat!

Monday 25th May 2009 | 04:15 PM Reply Comment URL Profile Back to top

Not a Member

Beth

That was simple. Thanks for the download too!

Tuesday 26th May 2009 | 08:18 AM Reply Comment URL Back to top

owen

owen

Hmm I have better ideas for this one.. Thanks for the tip again.

Saturday 13th June 2009 | 12:04 AM Reply Comment URL Profile Back to top

Not a Member

Carlo

This Could also work to stop people from downloading your images. Unless they find the images' url or get the image out of the browser cache somehow.

Tuesday 21st July 2009 | 06:03 AM Reply Comment URL Back to top

FYI: You are currently not logged in. It's cool though - you can still comment. But only members get a profile page, access to the download section and they can pimp their own web sites. Feel free to register or Login now!

Your name:

Your comments:

Note: HTML tags are automatically stripped from comments.

Are you human?
Turing

Sorry, I have to ask. So what sort of animal is this? (Hint: you don't have to be perfectly specific)

Back to top

Login to That Web Guy Blog

Login

Not registered? | Forgot your Password? Cancel Login