Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › [CSS] Draw Circles with CSS
New Posts  All Forums:Forum Nav:

[CSS] Draw Circles with CSS

post #1 of 8
Thread Starter 
Long gone is the day when you require images to create circles and items in circles. We will create a simple circle now.

Just like if we were making a square or rectangle using a div, we declare a width, height, and background color. Of course things like Borders and padding are optional still.

Lets create our first circle:
Code:
.circle
{
width: 200px;
height: 200px;
background-color: #eeeeee;
border-radius: 50%;
}

In the code above, it is the border-radius that turns the square into a nice circle.

However, by adding a few more properties we can turn it into quite a nice circle.
Code:
.circle
{
width: 200px;
height: 200px;
background-color: #eeeeee;
border-radius: 50%;
padding: 5px;
text-align: center;
line-height: 200px;
color: white;
font-family: arial, serif;
font-size: 30px;
}

.circle:hover
{
background-color: #575757;
mouse: pointer;
}

Now by adding some HTML and Javascript (onclick), we can make it into a link to OCN!
Code:
<!doctype html>
<html lang="en">
<head>
<title>Overclock.net link</title>
<style type="text/css">
.circle
{
width: 200px;
height: 200px;
background-color: #eeeeee;
border-radius: 50%;
padding: 5px;
text-align: center;
line-height: 200px;
color: white;
font-family: arial, serif;
font-size: 30px;
}

.circle:hover
{
background-color: #575757;
mouse: pointer;
}
</style>
</head>

<body>
<div class="circle" onclick="window.location='http://www.overclock.net';">
Overclock.net
</div>
</body>
</html>

TO SEE THE FINISHED ARTICLE ON JS FIDDLE: http://jsfiddle.net/rHCPa/

I hope these guides are helping some people.
Main Rig.
(14 items)
 
Battlefield 2 review.
Battlefield 2 PC Game EA
 
CPUMotherboardGraphicsRAM
Bulldozer FX4100 Gigabyte GA-M68MT-S2 MSI 7770 Kingston Hyper X | 2 x 2 GB 
Hard DriveOptical DriveCoolingOS
WD 2 TB  LiteON DVD/CD R-W Drive AMD Standard cooler Windows 7 Ultimate 
MonitorKeyboardPowerCase
32' Luxor Full HD TV Microsoft comfort curce Antec 450 Watt OcUK Value case 
MouseAudio
Microsoft wireless mouse Logitech speakers & Bass 
  hide details  
Reply
Main Rig.
(14 items)
 
Battlefield 2 review.
Battlefield 2 PC Game EA
 
CPUMotherboardGraphicsRAM
Bulldozer FX4100 Gigabyte GA-M68MT-S2 MSI 7770 Kingston Hyper X | 2 x 2 GB 
Hard DriveOptical DriveCoolingOS
WD 2 TB  LiteON DVD/CD R-W Drive AMD Standard cooler Windows 7 Ultimate 
MonitorKeyboardPowerCase
32' Luxor Full HD TV Microsoft comfort curce Antec 450 Watt OcUK Value case 
MouseAudio
Microsoft wireless mouse Logitech speakers & Bass 
  hide details  
Reply
post #2 of 8
Also, as a little note...

For maximum compatibility with older browsers, you should also include:
Code:
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
Alpha
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K ASUS P8Z68 Deluxe GTX 280 (1GB) 2x 4GB G.SKILL PC-16000 
Hard DriveMonitorKeyboardCase
2x Corsair Force GT 120GB (RAID-0), 3TB Hitachi DS Dell U2410 Logitec Wireless Illuminated Keyboard (K800) Fractal Design Define R3: Silver Arrow 
Mouse
Logitec Performance Mouse MX 
  hide details  
Reply
Alpha
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K ASUS P8Z68 Deluxe GTX 280 (1GB) 2x 4GB G.SKILL PC-16000 
Hard DriveMonitorKeyboardCase
2x Corsair Force GT 120GB (RAID-0), 3TB Hitachi DS Dell U2410 Logitec Wireless Illuminated Keyboard (K800) Fractal Design Define R3: Silver Arrow 
Mouse
Logitec Performance Mouse MX 
  hide details  
Reply
post #3 of 8
A bit of a mini-rant, but this is the point where I start to question if we're abusing CSS.

We already have SVG for people that want to program vector graphics, CSS should just be styling the document rather than creating objects.

[edit]
I'm not trying to take anything away from the helpfulness of your guides though. Regardless of personal views, it's always good to see people contributing their experience to the wider community thumb.gif
post #4 of 8
Well, here's the thing. SVG graphics are what they are. However, rounded corners have two approaches. If you want the box to be able to expand horizontally and vertically, then you're going to have a lot of extra markup using the old way. To be precise, it'd be about 4 extra containers on each box that has rounded corners. The alternative now, using CSS, not only reduces the HTML markup but also reduces the number of HTTP requests performed by the browser as it no longer has to fetch 4 separate images.

Furthermore, the border-radius property can be used on images which allows us to dynamically give any image a rounded-corner. Before, you'd have to manually edit each image, have an overlay img, or do some server-side pre-processing and caching of a dynamically modified image using something such as the GD or ImageMagick library.

I mean, the stylesheets are what load the images anyway. So essentially, images and everything related to the aesthetics are to be handled by the CSS. Honestly, the part that took me by shock in CSS3 were the rotations, animations, and transitions. While I can see the relativity, I never expected it.

Regardless though, as a web developer, and doing a lot of the front-end stuff recently; I have to admit that coding HTML5/CSS3 is a breeze compared to XHTML 1.0 Strict/CSS2. I spend a lot less time in Photoshop trying to slice images and the workflow is a lot smoother.
Alpha
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K ASUS P8Z68 Deluxe GTX 280 (1GB) 2x 4GB G.SKILL PC-16000 
Hard DriveMonitorKeyboardCase
2x Corsair Force GT 120GB (RAID-0), 3TB Hitachi DS Dell U2410 Logitec Wireless Illuminated Keyboard (K800) Fractal Design Define R3: Silver Arrow 
Mouse
Logitec Performance Mouse MX 
  hide details  
Reply
Alpha
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K ASUS P8Z68 Deluxe GTX 280 (1GB) 2x 4GB G.SKILL PC-16000 
Hard DriveMonitorKeyboardCase
2x Corsair Force GT 120GB (RAID-0), 3TB Hitachi DS Dell U2410 Logitec Wireless Illuminated Keyboard (K800) Fractal Design Define R3: Silver Arrow 
Mouse
Logitec Performance Mouse MX 
  hide details  
Reply
post #5 of 8
Quote:
Originally Posted by Phaaze View Post

Well, here's the thing. SVG graphics are what they are. However, rounded corners have two approaches. If you want the box to be able to expand horizontally and vertically, then you're going to have a lot of extra markup using the old way. To be precise, it'd be about 4 extra containers on each box that has rounded corners. The alternative now, using CSS, not only reduces the HTML markup but also reduces the number of HTTP requests performed by the browser as it no longer has to fetch 4 separate images.
Furthermore, the border-radius property can be used on images which allows us to dynamically give any image a rounded-corner. Before, you'd have to manually edit each image, have an overlay img, or do some server-side pre-processing and caching of a dynamically modified image using something such as the GD or ImageMagick library.
I mean, the stylesheets are what load the images anyway. So essentially, images and everything related to the aesthetics are to be handled by the CSS. Honestly, the part that took me by shock in CSS3 were the rotations, animations, and transitions. While I can see the relativity, I never expected it.
Regardless though, as a web developer, and doing a lot of the front-end stuff recently; I have to admit that coding HTML5/CSS3 is a breeze compared to XHTML 1.0 Strict/CSS2. I spend a lot less time in Photoshop trying to slice images and the workflow is a lot smoother.

SVG supports circles though (has done since version 1.1): http://www.w3.org/TR/SVG/shapes.html#CircleElement
Code:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <circle cx="100" cy="50" r="40" stroke="black"
  stroke-width="2" fill="red"/>
</svg>
( example taken from http://www.w3schools.com/svg/svg_circle.asp )

And as SVGs are handled like images, you'd just dump them into the <img> tags, just like any other JPEG or PNG (or inside an embedded object). So there would be no extra mark up, only one extra HTTP request (hopefully cached) and your separating HTML (the page content), CSS (page design) and the graphical objects (images, embedded objects) like they should be.

CSS is pretty powerful these days, but I don't agree with it being used for rendering.
Edited by Plan9 - 11/29/12 at 11:34pm
post #6 of 8
If you're that desperate, you can base64 encode an image into the file too. However, that's not the point of SVG's... They're better suited for drawing dynamic graphs and such. They're not very practical for UI design elements as they contain a lot of overhead and meta data, therefore being very costly in terms of bandwidth and causing slower page loads...

CSS's are already used to color the DOM objects, why not make use of it's own engine to simply act as a mask for itself and its child elements? It's much more efficient than using XML to produce an SVG's, loading static images with extra markup, or even drawing onto a canvas element using JavaScript...
Edited by Phaaze - 11/30/12 at 5:29pm
Alpha
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K ASUS P8Z68 Deluxe GTX 280 (1GB) 2x 4GB G.SKILL PC-16000 
Hard DriveMonitorKeyboardCase
2x Corsair Force GT 120GB (RAID-0), 3TB Hitachi DS Dell U2410 Logitec Wireless Illuminated Keyboard (K800) Fractal Design Define R3: Silver Arrow 
Mouse
Logitec Performance Mouse MX 
  hide details  
Reply
Alpha
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K ASUS P8Z68 Deluxe GTX 280 (1GB) 2x 4GB G.SKILL PC-16000 
Hard DriveMonitorKeyboardCase
2x Corsair Force GT 120GB (RAID-0), 3TB Hitachi DS Dell U2410 Logitec Wireless Illuminated Keyboard (K800) Fractal Design Define R3: Silver Arrow 
Mouse
Logitec Performance Mouse MX 
  hide details  
Reply
post #7 of 8
Quote:
Originally Posted by Phaaze View Post

If you're that desperate, you can base64 encode an image into the file too.
You could do that direct into the img src if you really wanted to, but base64 adds ~30% to file sizes so isn't advisable.
Quote:
Originally Posted by Phaaze View Post

However, that's not the point of SVG's... They're better suited for drawing dynamic graphs and such. They're not very practical for UI design elements as they contain a lot of overhead and meta data, therefore being very costly in terms of bandwidth and causing slower page loads...
I don't think SVG is any less suited for this than CSS is. There was quite a lot of data in the CSS there as well. Though I will grant you that XML does have more redundancy than (pseudo)JSON.
Quote:
Originally Posted by Phaaze View Post

CSS's are already used to color the DOM objects, why not make use of it's own engine to simply act as a mask for itself and its child elements? It's much more efficient than using XML to produce an SVG's, loading static images with extra markup, or even drawing onto a canvas element using JavaScript...
I don't agree with using JS for this either.

It's all to do with having clear separation between different programming blocks. Like how MVC is used on the server side to separate HTML from server side code, on the client side I believe content, design, objects and client side executable code should all be separated into well defined blocks:
  • Content: HTML
  • Design: CSS
  • Objects: SVG, PNG, Flash, etc
  • client side code: Javascript

I think too many people abuse tools like CSS and Javascript simple because they've morphed into such powerful tools that you can extend their usefulness to roles in which they were not originally intended nor best suited for. Much like how people use MS Word for DTP and Excel as a database. But that doesn't mean that Word or Excel are any good at those roles. In fact it make scalability and maintainability much more difficult and creates a right nightmare for the sorry individual who has to join that project after the original developers have parted.

I think the key to being a good developer is understanding how to properly modularise your code and the key to that is understanding the distinct roles for each facet of your application and not trying to merge paradigms with them. In C++, that means having classes and grouping related functions with by source and header files. In web development, that means separating the different elements of the page into logical building blocks.

disclaimer: I'm all for taking a number of short cuts (eg embedding snippets of CSS / Javascript into HTML) at the final streamlining stage when trying to reduce the footprint of your site. But the streamlining stage is -in my opinion at least- best done when you have a working site and then trying to compress it, rather than taking short cuts when building a site. The former means you still have a clean foundation to revert to when core changes need to be made. The latter means you quickly descend into spaghetti code as your project matures.

That's just my opinion anyway. Every programmer has their own approach smile.gif
Edited by Plan9 - 12/1/12 at 2:50am
post #8 of 8
I hope you don't mind, but this is quite an interesting discussion and I'm really enjoying hearing your opinion on this so I started a new thread dedicated to this topic: http://www.overclock.net/t/1333205/what-programming-design-principles-to-you-follow-for-web-development
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › [CSS] Draw Circles with CSS