aboutsummaryrefslogtreecommitdiff
path: root/build/fake-3d-elements-with-css/index.html
blob: d04f852a12c8eeb450d192068065bab1d6464dab (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!doctype html>
<html lang="en">
<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="/atom.xml" type="application/atom+xml" rel="alternate" title="Atom feed for blog posts" />
	<link href="/rss.xml" type="application/rss+xml" rel="alternate" title="RSS feed for blog posts" />
<style>*{box-sizing:border-box;}body{font-family:sans-serif;line-height:1.33;margin:0 auto;max-width:650px;padding:1rem;}img{max-width:100%;}pre{border:1px solid;overflow:auto;padding:5px;}table{text-align:left;width:100%;}.footnotes{font-size:90%;}</style>
</head>

<nav>
	<a href="#menu">Menu &darr;</a>
</nav>

<main>
<h1 id="faking-3d-elements-with-css">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 id="the-html">The HTML</h2>

<p>Prepare for your mind to be blown:</p>

<pre><code>&#60;div class="main-orb"&#62;
    &#60;div class="inner-orb"&#62;&#60;&#47;div&#62;
&#60;&#47;div&#62;
</code></pre>

<p>That&#8217;s it - I wasn&#8217;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&#8217;s parent at a slightly smaller size (360x360) - but more on that below in the CSS portion.</p>

<h2 id="the-css">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; &#47;* This is important for the inner orb element later *&#47;
    width: 400px;
}
</code></pre>

<p>Next, we include both <code>:before</code> and <code>:after</code> pseudo elements for our orb&#8217;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&#8217;ve explained in a previous post why <a href="/blog/better-box-shadows.html">that&#8217;s not the best approach</a>.</p>

<pre><code>&#47;* Shared styling for both pseudo elements - Remember DRY *&#47;
.main-orb:before, .main-orb:after {
    border-radius: 200px 200px 9999px 9999px;
    bottom: -10px;
    content:&#39;&#39;;
    filter: blur(20px);
    height: 40px;
    position: absolute;
    z-index: -1;
}

&#47;* Bigger, lighter shadow *&#47;
.main-orb:before {
    background: rgba(0,0,0,0.4);
    left: 7.5%;
    width: 85%;
}

&#47;* Smaller, darker shadow *&#47;
.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 id="poor-mans-3d-elements">Poor-man&#8217;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">&uarr; 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> &amp; <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>