diff options
Diffstat (limited to 'build/fake-3d-elements-with-css')
-rw-r--r-- | build/fake-3d-elements-with-css/index.html | 59 |
1 files changed, 39 insertions, 20 deletions
diff --git a/build/fake-3d-elements-with-css/index.html b/build/fake-3d-elements-with-css/index.html index 3eb02c5..d04f852 100644 --- a/build/fake-3d-elements-with-css/index.html +++ b/build/fake-3d-elements-with-css/index.html @@ -1,70 +1,87 @@ <!doctype html> -<html lang="en" id="top"> +<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="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> + <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 ↓</a> + <a href="#menu">Menu ↓</a> </nav> <main> -<h1>Faking 3D Elements with CSS</h1> +<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>The HTML</h2> + +<h2 id="the-html">The HTML</h2> + <p>Prepare for your mind to be blown:</p> -<pre><code><div class="main-orb"> - <div class="inner-orb"></div> -</div> + +<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>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 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; /* This is important for the inner orb element later */ + 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 */ + +<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:''; + content:''; filter: blur(20px); height: 40px; position: absolute; z-index: -1; } -</code></pre> -<pre><code>/* Bigger, lighter shadow */ + +/* 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 */ + +/* 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; @@ -77,7 +94,9 @@ width: 360px; } </code></pre> -<h2>Poor-man's 3D elements</h2> + +<h2 id="poor-mans-3d-elements">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> |