146

Is it possible to draw circle using css only which can work on most of the browsers (IE,Mozilla,Safari) ?

web-tiki
  • 99,765
  • 32
  • 217
  • 249
Hector Barbossa
  • 5,506
  • 13
  • 48
  • 70

6 Answers6

242

Yep, draw a box and give it a border radius that is half the width of the box:

#circle {
    background: #f00;
    width: 200px;
    height: 200px;
    border-radius: 50%;
}

Working demo:

http://jsfiddle.net/DsW9h/1/

#circle {
    background: #f00;
    width: 200px;
    height: 200px;
    border-radius: 50%;
}
<div id="circle"></div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
  • 6
    I am using IE8, and this demo does not work. It shows a red square. – adam May 22 '12 at 19:23
  • 3
    IE8 does not support border-radius, so that makes sense... Great idea for modern browsers though, very cool... – jcreamer898 Feb 08 '13 at 14:37
  • 39
    Instead of supporting IE8 please ask your clients if you can show a message telling the user to upgrade their browser. It will benefit everyone, and Microsoft recommends it. Google even discontinued IE8 support in their web apps (Gmail, Calendar, Drive, Docs, etc.) at the end of 2012. It's ridiculous to support a 5 year old browser. – Gavin Sep 12 '13 at 10:08
  • 2
    use a polyfille for ie8 http://css3pie.com/ and use border-radius:100%; for responsive circle use padding-bottom:40%; width:40%; height:0; overflow:visible; – fearis Jun 29 '15 at 17:20
  • 2
    this doesn't work well if the circle is really really small – Atav32 Aug 28 '15 at 00:14
  • there is a much simpler way now using aspect-ratio: .circle { inline-size: 25ch; aspect-ratio: 1; border-radius: 50%; } – Felix Haeberle Mar 23 '23 at 07:14
177

You could use a .before with a content with a unicode symbol for a circle (25CF).

.circle:before {
  content: ' \25CF';
  font-size: 200px;
}
<span class="circle"></span>

I suggest this as border-radius won't work in IE8 and below (I recognize the fact that the suggestion is a bit mental).

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Tom
  • 43,583
  • 4
  • 41
  • 61
  • 4
    `:before` doesn't work in IE7 and below, so this method only gains support for IE8 but makes it very hard to position the circle correcly. For example, font-size of 200px doesn't equate to a circle with a diameter of 200px and you lose antialiasing on some systems. – Tatu Ulmanen Aug 04 '11 at 08:02
  • Maybe also we can set `margin: -0.5em -0.3em -0.3em -0.1em` – Pierre de LESPINAY Nov 30 '12 at 12:41
  • 1
    Given that at this point, IE 8 is nearly 10 years old, this should no longer be the accepted answer. It is reasonable to drop support for it, as according to https://caniuse.com/usage-table, IE 8 currently has a 0.18% share of usage, and most modern websites have done so. The border-radius property is now supported pretty much across the board (https://caniuse.com/#search=border-radius), so should be the accepted answer. – bjg222 Aug 24 '18 at 22:19
  • is there a way to appease the SEO gods, even if i use an image of circular text, instead of rotating each letter? – oldboy Oct 19 '20 at 08:21
52
  • Create a div with a set height and width (so, for a circle, use the same height and width), forming a square
  • add a border-radius of 50% which will make it circular in shape. (note: no prefix has been required for a long time)
  • You can then play around with background-color / gradients / (even pseudo elements) to create something like this:

.red {
  background-color: red;
}
.green {
  background-color: green;
}
.blue {
  background-color: blue;
}
.yellow {
  background-color: yellow;
}
.sphere {
  height: 200px;
  width: 200px;
  border-radius: 50%;
  text-align: center;
  vertical-align: middle;
  font-size: 500%;
  position: relative;
  box-shadow: inset -10px -10px 100px #000, 10px 10px 20px black, inset 0px 0px 10px black;
  display: inline-block;
  margin: 5%;
}
.sphere::after {
  background-color: rgba(255, 255, 255, 0.3);
  content: '';
  height: 45%;
  width: 12%;
  position: absolute;
  top: 4%;
  left: 15%;
  border-radius: 50%;
  transform: rotate(40deg);
}
<div class="sphere red"></div>
<div class="sphere green"></div>
<div class="sphere blue"></div>
<div class="sphere yellow"></div>
<div class="sphere"></div>
web-tiki
  • 99,765
  • 32
  • 217
  • 249
jbutler483
  • 24,074
  • 9
  • 92
  • 145
15

border radius is good option, if struggling with old IE versions then try HTML codes

&#8226;

and use css to change color. Output:

Salman A
  • 262,204
  • 82
  • 430
  • 521
Sunit
  • 403
  • 1
  • 5
  • 6
11

This will work in all browsers

#circle {
    background: #f00;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    -moz-border-radius: 50%;
    -webkit-border-radius: 50%;
}
AeJey
  • 1,447
  • 20
  • 40
9

yup.. here's my code:

<style>
  .circle{
     width: 100px;
     height: 100px;
     border-radius: 50%;
     background-color: blue
  }
</style>
<div class="circle">
</div>
Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81