№ 168. Circling the square: round images with CSS only

Squaring the circle is a mathematical phantasm born in antiquity and proved to be impossible in the 19th century; it consists in attempting to draw a square with the same area as a circle.
Today we’ll turn the quadrature problem upside down and try to convert a square image into a round one.
Round images aren’t something new to the web (although they’re rather “fake-round”, using transparency), but they have to face some cross-browser compatibility issues (the famous PNG alpha transparency bug in IE6). Anyhow, I don’t intend to provide another fix to this bug, nor to deal with .png. I just want to share a little trick that will turn a common .jpg into a round image using CSS3 only.
Please note: the codes listed bellow will only work in Mozilla, Safari and Chrome.
First, we need a square image.

Kitty - square image

Kitty - square image


Second, we nest the image inside a div that we style; then, hocup pocus, we’ll see a nice round picture:

The code for the image above:

<div class="round-image" style="width:200px;height:200px;border:1px solid #fff;-moz-border-radius:200px;-webkit-border-radius:100px;background:url(http://wp-content-themes.com/wp-content/uploads/2009/04/kitty.jpg)"></div>

The trick here is to use the image as background for the empty div. If you actually place the picture inside the div, using the img tag, it will simply not work (at least using this particular style).
Using this tip with WordPress
If you want to have round images displayed on single posts, you only need to use a code like the one above.
On the other hand, if you want to automatically have such images displayed on the main page, you’ll need a little php. To make the things easier, we’ll borrow a little script from Narudin Jauhari (I first saw it in his Hamasaki theme*) and modify it:

<div style="float:left;margin:2px;">
<?php
$id =$post->ID;
$the_content =$wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id");
$pattern = ‘!<img.*?src="(.*?)"!’;
preg_match_all($pattern, $the_content, $matches);
$image_src = $matches[’1′][0];
?>
<div class="round-thumbnail" style=" width:200px;height:200px;border:1px solid #ccc;-moz-border-radius:200px;-webkit-border-radius:100px;background: url(<?php if($image_src != ”) { echo $image_src; } else { ?><?php bloginfo(‘template_directory’); ?>/images/no-image.jpg<?php } ?>) no-repeat;"></div>

Just drop this script inside of the loop and style it in a manner that suits your blog design.
*Please note that this piece of code was licenced under Creative Commons attribution – share alike (CC-BY-SA 2.0), which is incompatible with GNU-GPL.
The problem with this script is that it won’t resize the image, so you’ll have only a part of the actual picture displayed on the homepage; also, the image won’t be clickable.

Bonus 1: Clickable HTML round images with PHP and CSS

This tip is also automatic, working with WordPress, and using php. The final result isn’t spectacular, but but it can be tastefully styled.
First, we need a php script that will automatically call an image on the frontpage, for instance this beautiful function written by c.bavota.
Second, we need to nest the call in a div:

<div class="himg">
<?php getImage(’1′); ?>
</div>

Finally, we style the whole thing:

.himg{width:164px;height:134px;}
.himg img{width:60px;height:60px;margin:0;padding:15px;-moz-border-radius:150px;}

(It is important to set widths and heights for both div and img; otherwise, the image won’t be resized.)
The result will look like this:

round clickable image

round clickable image


Of course, you can change the style in any way you want.
This trick works best when the pictures background has the same color as the image’s background (yes, they’re different).

Bonus 2: Clickable HTML round images with CSS only

That’s right, with CSS only. For the beginning, let’s upload an image:

square image

square image


Now let’s make this image clickable and round:

clickable html round image

The script for the image above:

<div style="width:500px;height:500px;background:#f1eae8;"><a href="http://wp-content-themes.com"><img src="http://wp-content-themes.com/wp-content/uploads/2009/04/round.jpg" alt=clickable html round image" style="width:260px;height:260px;padding:80px;background:#fff;border:5px solid #ccc;-moz-border-radius:260px;-webkit-border-radius:200px;" /></a></div>

I really hope you enjoyed this post.
Cheers.

Tags: , , ,

Published on the 13th of April, 2009, in Tutorials · Print

5 Comments on “Circling the square: round images with CSS only”

  1. For some reason your examples aren’t working in Safari (I’m running Safari 4).

  2. Thanks, Colin, I had forgotten that WebKit interprets radius as radius while Gecko interprets it as diameter. I’ve updated the post (and the snipets).

  3. I have found pure css technique to make rounded curves. This works in IE6 also.
    http://www.htmlremix.com/css/curved-corner-border-radius-cross-browser

  4. Thanks for sharing :) A little remark, though – the technique isn’t pure css, it’s css + javascript.

  5. I could only get this to work when I put the CSS code in with the html, but not when I called it in the CSS file. Does it have to be in the HTML?

Some Pings and Trackbacks Are Welcome

  1. Redondear imagenes via CSS3 | Eliseos.Net
  2. The truth about the world | Fictions

Leave a Reply

Recent comments

Colophon

This website was made by Delicia, who is deeply in love with WordPress. It is built on a 30x20px module, with Georgia and Helvetica/Arial. The design is the result of many years she spent reading Wittgenstein.