{"id":649,"date":"2013-01-09T10:12:51","date_gmt":"2013-01-09T16:12:51","guid":{"rendered":"http:\/\/cssnewbie.com\/?p=649"},"modified":"2013-01-09T10:12:51","modified_gmt":"2013-01-09T16:12:51","slug":"making-a-sphere-in-css","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/making-a-sphere-in-css\/","title":{"rendered":"Making a Sphere in CSS"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2013\/01\/css-sphere-4.png\" alt=\"CSS Sphere\" width=\"500\" height=\"220\" class=\"alignnone size-full wp-image-653\" \/><\/p>\n<p>Traditionally, the web has been a static, two dimensional space. CSS allowed us to modify the width and height of elements on our websites, but it has been difficult to create a sense of depth without using lots of extra images, and creating motion has always been the realm of Flash or JavaScript. Happily, this is changing.<\/p>\n<p>CSS3 provides us with a slew of new tools that allow us to do some fantastic things simply &mdash; things that would have required extra images at the least, or an entirely different language more commonly, to accomplish. And while browser support isn\u2019t always the best for these new tools yet, sometimes it\u2019s fun to play with what the web of tomorrow offers instead of dealing with the web of today or yesteryear.<\/p>\n<p>Today, let\u2019s start with something simple: we\u2019ll turn a regular, blocky old element into a sphere.<!--more--><\/p>\n<h3>The HTML<\/h3>\n<p>There is practically no HTML required here. We can use pretty much any tag. I chose the bold tag. B is for Ball.<\/p>\n<pre lang=\"html\"> <b class=\"ball\"><\/b><\/pre>\n<h3>The CSS<\/h3>\n<p>Now for the fun stuff! A bold tag is an inline element, but we can turn it into a block element easy enough. Then we can give it a width, height and color (and margin, just for spacing). That gets us our square to work with.<\/p>\n<pre lang=\"css\">\nb.ball {\n  display: block;\n  width: 200px;\n  height: 200px;\n  margin: 30px auto 0;\n  background-color: #3b4ba3;\n}<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2013\/01\/css-sphere-1.png\" alt=\"CSS Square\" width=\"500\" height=\"220\" class=\"alignnone size-full wp-image-650\" \/><\/p>\n<p>Next up, let\u2019s make our square a circle. It sounds WAY harder than it is. All it takes is one line of border-radius:<\/p>\n<pre lang=\"css\">border-radius: 50%;<\/pre>\n<p><a href=\"http:\/\/cssnewbie.com\/easy-rounded-corners-with-border-radius\/\">Border-radius applies a radius to the corners of our borders<\/a>, rounding them. If we set a border-radius of 50%, the corners are automatically rounded to 50% of the width\/height of the element. Instant circle.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2013\/01\/css-sphere-2.png\" alt=\"css-sphere-2\" width=\"500\" height=\"220\" class=\"alignnone size-full wp-image-651\" \/><\/p>\n<p>So now we have a circle, but it doesn\u2019t look very three-dimensional. For that, we\u2019ll turn to the realm of design, and play with light and shadow. (Note: as I\u2019ve mentioned many times, I am NOT a designer. Take any design advice you get from me with a big grain of salt. Maybe a salt lick.)<\/p>\n<p>Shadow is an easy way to create a sense of depth in a two-dimensional space. If we use an inset box-shadow (so the shadow is on our element, not behind it, we can shade our sphere.<\/p>\n<pre lang=\"css\">box-shadow: inset -25px -25px 40px rgba(0,0,0,.5);<\/pre>\n<p>A few things to note about how I\u2019m using the box-shadow here. The first item, inset, means the shadow is inside the element. Then I set the distance the shadow extends from the left side (-25px means it will extend 25px from the RIGHT side), the distance from the top (again, negative puts it on the bottom), and the amount of blur to add to the shadow (zero means a sharp line, bigger numbers mean more blur). Then I\u2019m setting the color of the shadow using rgba, which allows me to use transparency in my color. I\u2019m setting it to a 50% transparent black, which allows the blue to show through and makes the shadow look more like true shading.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2013\/01\/css-sphere-3.png\" alt=\"css-sphere-3\" width=\"500\" height=\"220\" class=\"alignnone size-full wp-image-652\" \/><\/p>\n<p>Now honestly, in my opinion this is pretty close already. But we can go one step further to make it more realistic. Where there are shadows, there is light. So let\u2019s add some light to our sphere.<\/p>\n<p>Because the shadow is on the lower-right quadrant, the light must be coming from the upper-left side. I could technically use another inset drop-shadow here, but as you can see from the image above, drop shadows curve along the edge of the object. While I think that looks okay for this one shadow, adding the light in the same way starts to look strange. Instead, we\u2019ll use a linear gradient to apply our light.<\/p>\n<pre lang=\"css\">\nbackground-image: -webkit-linear-gradient(-45deg, rgba(255,255,220,.3) 0%, transparent 100%);\nbackground-image: -moz-linear-gradient(-45deg, rgba(255,255,220,.3) 0%, transparent 100%);  \nbackground-image: -o-linear-gradient(-45deg, rgba(255,255,220,.3) 0%, transparent 100%);\nbackground-image: -ms-linear-gradient(-45deg, rgba(255,255,220,.3) 0%, transparent 100%);<\/pre>\n<p>Gradients in CSS are applied as background images. I\u2019m using browser prefixes to apply my gradient in each of the modern browsers (I\u2019m not using the non-prefixed W3C version because Firefox seems to be misinterpreting it). <\/p>\n<p>We\u2019re using an rgba color again (30% opaque white) to create a feeling of light on that side. The -45deg part rotates the gradient from its default top-to-bottom 45 degrees counter-clockwise, so that it starts from the top-left corner. We\u2019re fading the gradient to transparent over the length of the entire circle. We could stop the light at 50% (like it would in a real sphere), but I felt that created too hard of a line for my tastes.<\/p>\n<p>And now our sphere is complete! You can see a demo below, and click through to play around with the code.<\/p>\n<pre class=\"codepen\" data-height=\"300\" data-type=\"result\" data-href=\"patLD\" data-user=\"rglazebrook\" data-safe=\"true\"><code><\/code><\/pre>\n<p><script async src=\"http:\/\/codepen.io\/assets\/embed\/ei.js\"><\/script><\/p>\n<p>Please note, because this is a modern CSS technique, you\u2019ll need a modern browser.  Safari, Firefox, Chrome, and presumably Opera should be fine. IE9 understands the drop shadow but not the linear gradient.<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Traditionally, the web has been a static, two dimensional space. Depth and motion have been the realm of Flash or JavaScript. Happily, this is changing. Today, we\u2019ll turn a regular, blocky old element into a sphere. [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/making-a-sphere-in-css\/\" title=\"Click to read 'Making a Sphere in CSS'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":653,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[174],"tags":[47,48,296],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/649"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=649"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/649\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media\/653"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}