CSS Tip: How to Make Circles Without Images

Sunday, May 3rd, 2009

About a month ago, Jeffery Way posted an article about creating shapes with pure CSS, no images added. I've been using the CSS3 property of border radius a lot in a design I've been working on and thought, "why can't I use this to make circles?" Today, I'll show you how to make your very own CSS circles. In addition, we will use our new knowledge to create a simple jQuery plugin!

View Demo

Structure, structure, structure!

How are we suppose to make a circle without any HTML to style? Because this is just a simple shape, I'll be using an "empty" span tab — I've just put   into it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>CSS Circle</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
body {
margin: 1in;
}
.circle {
/* Everything else goes here. */
}
</style>
</head>
<body>

<span class="circle">&amp;amp;nbsp;</span>

</body>
</html>

I'll be using inline CSS because this is just a demo, but if this were for a project you'd probably want to put it in an external style sheet. I've also reset the margins and padding, then gave the body a one inch margin to push our circle away from the sides of the page.

Basic Styles

For now, I'll give the circle a background of #333 just so we can see it. You'll also want to give it the same height and width, this is the length of the diameter. The size doesn't really matter just make sure it's en even number.

.circle {
display: block;
width: 80px;
height: 80px;
background: #333;
}

Note: If you're using an inline element (like a span tag), you'll want to give the element a display of "block". If you don't know if your element is a block level element or not, just load the file in a web browser and if it's height and width look larger than 1px it is.

Circles Are Really Made With CSS3

What really gives this circle its arcs is CSS3's -moz-border-radius and -webkit-border-radius properties. What we're going to have to do is give the border radius a value of half the height/width; this is the radius of the circle. For my example, I've used 80px for the height and width so I'll set the border radius to 40px.

.circle {
display: block;
display: block;
width: 80px;
height: 80px;
background: #333;
-moz-border-radius: 40px;
-webkit-border-radius: 40px;
}

Conclusion

CSS3 is a very powerful tool to know how to use. Just make sure you don't base your entire website around it because not every browser supports it yet. However, if you can get the effect in certain browsers and still have you're design looking okay in other browsers, I think it's fine to use browser specific styles.

Create a jQuery Plugin For Our Circles

You can also take this a step further by making it into a jQuery plugin.

(function($){

	$.fn.jCircle = function(options) {
		var defaults = {
			size: 40
		},
			settings = $.extend({}, defaults, options);

		settings.size = parseInt(settings.size) || 40;
		settings.radius = (settings.size/2) + 'px';

		return this.each(function(){
			var $this = $(this);

			$this.css({
				display: 'block',
				width: settings.size + 'px',
				height: settings.size + 'px'
			});

			$this.css('-moz-border-radius', settings.radius);
			$this.css('-webkit-border-radius', settings.radius);
		});
	}

})(jQuery);

View Demo


If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!

ABOUT THIS AUTHOR

Hey, I'm Vasili and I run duove.com. I love to code with PHP, MySQL, and jQuery, along with the basics like XHTML and CSS. At the moment, I just write (PHP) scripts for myself - like my CMS - but I hope that I can soon start freelancing for some people. Don't forget to follow me on Twitter for little tips and tricks I might tweet about. :)
  1. May 3, 2009 at 10:52 pm
  2. Gaurish
    May 4, 2009 at 12:26 am
  3. May 4, 2009 at 3:01 am
  4. May 4, 2009 at 3:53 am
  5. David
    May 4, 2009 at 6:43 pm
    • May 4, 2009 at 7:10 pm
  6. May 5, 2009 at 9:16 am
    • May 5, 2009 at 10:48 am
  7. May 8, 2009 at 2:21 am
    • May 9, 2009 at 12:04 am
  8. May 18, 2009 at 5:03 pm
  9. Meshach
    May 23, 2009 at 8:57 am
    • Meshach
      May 24, 2009 at 9:22 am
  10. July 10, 2009 at 4:37 am
  11. July 29, 2009 at 5:49 am
    • July 30, 2009 at 2:56 pm
    • Dominik
      September 21, 2009 at 1:54 am

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Popular Series

Output Buffering Articles
Build a Custom AJAX and PHP Contact Form
The Ultimate Image Gallery Manager.
ThemeForest Premium Site and WordPress Templates