1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
<!doctype html>
<html lang="en" id="top">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<title>Faking 3D Elements with CSS</title>
<link href="https://bt.ht/atom.xml" type="application/atom+xml" rel="alternate" title="Atom feed for blog posts" />
<style>*{box-sizing:border-box;}body{font-family:sans-serif;margin:0 auto;max-width:650px;padding:1rem;}img{max-width:100%;}pre{overflow:auto;}table{text-align:left;width:100%;}</style>
</head>
<nav>
<a href="#menu">Menu ↓</a>
</nav>
<main>
<h1>Faking 3D Elements with CSS</h1>
<p>2020-04-29</p>
<p>Although not always practical, creating the illusion that some of your web elements are 3D can be a fun experiment. I set out to see if I was able to create such an illusion with only 2 HTML elements and as little CSS as possible.</p>
<p>This is what I ended up creating:</p>
<p><img src="/public/images/css-orb.png" alt="Blue 3D orb made out pure CSS" /></p>
<p><a href="https://codepen.io/bradleytaunt/pen/VwvzKyb">Live CodePen Example</a></p>
<h2>The HTML</h2>
<p>Prepare for your mind to be blown:</p>
<pre><code><div class="main-orb">
<div class="inner-orb"></div>
</div>
</code></pre>
<p>That's it - I wasn't kidding when I said we would use only 2 HTML elements. The <code>.main-orb</code> is the core shape (set to 400x400) and the <code>.inner-orb</code> is placed on top of it's parent at a slightly smaller size (360x360) - but more on that below in the CSS portion.</p>
<h2>The CSS</h2>
<p>First we give the bigger orb element (<code>.main-orb</code>) the default styling needed to represent a 2D circle:</p>
<pre><code>.main-orb {
background: linear-gradient(#fff 0%, #eee 10%, #2E86FB 50%, #1A237E 100%);
border-radius: 9999px;
height: 400px;
margin: 4rem auto;
position: relative; /* This is important for the inner orb element later */
width: 400px;
}
</code></pre>
<p>Next, we include both <code>:before</code> and <code>:after</code> pseudo elements for our orb's drop shadow. You <em>could</em> do this with a simple <code>box-shadow</code> property on the <code>.main-orb</code> itself, but I've explained in a previous post why <a href="/blog/better-box-shadows.html">that's not the best approach</a>.</p>
<pre><code>/* Shared styling for both pseudo elements - Remember DRY */
.main-orb:before, .main-orb:after {
border-radius: 200px 200px 9999px 9999px;
bottom: -10px;
content:'';
filter: blur(20px);
height: 40px;
position: absolute;
z-index: -1;
}
</code></pre>
<pre><code>/* Bigger, lighter shadow */
.main-orb:before {
background: rgba(0,0,0,0.4);
left: 7.5%;
width: 85%;
}
</code></pre>
<pre><code>/* Smaller, darker shadow */
.main-orb:after {
background: rgba(0,0,0,0.7);
left: 20%;
width: 60%;
}
</code></pre>
<p>With our main orb complete we can move on to the <code>.inner-orb</code> element to help bring slightly more depth to our floating ball of CSS:</p>
<pre><code>.inner-orb {
background: linear-gradient(#fff 0%, #2E86FB 60%, #283593 100%);
border-radius: 9999px;
box-shadow: 0 8px 20px rgba(0,0,0,0.5);
height: 360px;
filter: blur(18px);
left: 20px;
position: absolute;
top: 15px;
width: 360px;
}
</code></pre>
<h2>Poor-man's 3D elements</h2>
<p>Clearly implementing something like this will never come close to generating true 3D renders on a website, but it is a fun exercise to see how much further we can push simple CSS. Feel free to fork the above CodePen to play around with different colors and shadow placements.</p>
<footer role="contentinfo">
<h2>Menu Navigation</h2>
<ul id="menu">
<li><a href="/">Home</a></li>
<li><a href="/projects">Projects</a></li>
<li><a href="/uses">Uses</a></li>
<li><a href="/wiki">Wiki</a></li>
<li><a href="/resume">Resume</a></li>
<li><a href="/colophon">Colophon</a></li>
<li><a href="/now">Now</a></li>
<li><a href="/donate">Donate</a></li>
<li><a href="/atom.xml">RSS</a></li>
<li><a href="#top">↑ Top of the page</a></li>
</ul>
<small>
Built with <a href="https://git.sr.ht/~bt/barf">barf</a>. <br>
Maintained with ♥ for the web. <br>
Proud supporter of <a href="https://usefathom.com/ref/DKHJVX">Fathom</a> & <a href="https://nextdns.io/?from=74d3p3h8">NextDNS</a>. <br>
The content for this site is <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>.<br> The <a href="https://git.sr.ht/~bt/bt.ht">code for this site</a> is <a href="https://git.sr.ht/~bt/bt.ht/tree/master/item/LICENSE">MIT</a>.
</small>
</footer>
|