diff options
author | Bradley Taunt <bt@btxx.org> | 2024-06-10 09:41:25 -0400 |
---|---|---|
committer | Bradley Taunt <bt@btxx.org> | 2024-06-10 09:41:25 -0400 |
commit | 07e4a2dafe248280b5610f8c7d09b0f30b530f54 (patch) | |
tree | 8a145d1d4d07e1278a837ff15dadccc322d27515 /build/posts/fake-3d-elements-with-css/index.html | |
parent | 16d28628aca9b2d356de31c319f5e7bc0f5b2b02 (diff) |
Initial modifications to rebuilt only changed files based on mod date, performance updates
Diffstat (limited to 'build/posts/fake-3d-elements-with-css/index.html')
-rw-r--r-- | build/posts/fake-3d-elements-with-css/index.html | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/build/posts/fake-3d-elements-with-css/index.html b/build/posts/fake-3d-elements-with-css/index.html new file mode 100644 index 0000000..a3e8319 --- /dev/null +++ b/build/posts/fake-3d-elements-with-css/index.html @@ -0,0 +1,104 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="color-scheme" content="dark light"> + <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;}blockquote{background:rgba(0,0,0,0.1);border-left:4px solid;padding-left:5px;}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> +</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><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 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 */ + 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; +} + +/* Bigger, lighter shadow */ +.main-orb:before { + background: rgba(0,0,0,0.4); + left: 7.5%; + width: 85%; +} + +/* 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 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> + <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>
\ No newline at end of file |