aboutsummaryrefslogtreecommitdiff
path: root/build/posts/better-box-shadows/index.html
blob: e98f56c5bc17b3f359069dca8c79e7fe62439d9a (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
122
123
124
125
126
<!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>Better Box Shadows</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 &darr;</a>
</nav>

<main>
<h1 id="better-box-shadows">Better Box Shadows</h1>
<p>2019-01-08</p>
<p>Box shadow on HTML elements has been widely supported across most browsers for a while now, but I find the default options don&#8217;t allow for much visual manipulation of the shadows in general.</p>
<p>Let&#8217;s take a look at a default configuration of <code>box-shadow</code>:</p>
<pre><code>.box-container {
    box-shadow: 0 4px 8px rgba(0,0,0,0.3);
}
</code></pre>
<p>In the example above the first property number is the origin of the <em>x-axis</em>, the second number is the origin of the <em>y-axis</em> and the third is the amount of <em>blur</em>.</p>
<p>We should also add some minimal styling to cleanup the <code>.box-container</code> a little bit for our example:</p>
<pre><code>&#60;div class="box-container"&#62;&#60;&#47;div&#62;


.box-container {
    box-shadow: 0 4px 8px rgba(0,0,0,0.3);
    &#47;* Styles to make it less ugly *&#47;
    background: white;
    border-radius: 10px;
    border: 1px solid #eee;
    height: 200px;
    padding: 10px;
    position: relative;
    width: 250px;
}
</code></pre>
<p>Which would render as this:</p>
<p>Not bad - but we can do a lot better than this.</p>
<h2 id="please-sir-i-want-some-more-depth">Please sir, I want some more (depth)</h2>
<p>We just need to add a simple child <code>div</code> (or use a <code>pseudo</code> element if you prefer) inside our main element we want to apply the shadow to:</p>
<pre><code>&#60;div class="box-container"&#62;
    &#60;div class="box-container-inner"&#62;&#60;&#47;div&#62;
&#60;&#47;div&#62;
</code></pre>
<p>Now we make our inner child element <code>absolute</code> and set it&#8217;s <code>height</code> and <code>width</code> dynamically to be slightly smaller than it&#8217;s parent (percentages work best for this). </p>
<p>Remember to set this child element behind it&#8217;s parent by adding <code>z-index: -1</code>.</p>
<pre><code>.box-container {
    &#47;* No box-shadow needed on this element anymore *&#47;
    &#47;* Styles to make it less ugly *&#47;
    background: white;
    border-radius: 10px;
    border: 1px solid #eee;
    height: 200px;
    padding: 10px;
    position: relative;
    width: 250px;
}
</code></pre>
<h2 id="inner-containers">Inner Containers</h2>
<p>We also need to target the <code>box-container-inner</code> element set inside the current parent to reflect our custom shadow styling:</p>
<pre><code>.box-container-inner {
    bottom: 0;
    &#47;* The box-shadow is added here now *&#47;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    height: 94%;
    left: 3%;
    position: absolute;
    width: 94%;
    z-index: -1;
}
</code></pre>
<p>Which will make the drop-shadow render with a little more realistic depth:</p>
<h2 id="but-wait---theres-more">But wait - there&#8217;s more!</h2>
<p>We could stop now and have a decent drop-shadow that is certainly easier on the eyes - but we can make this even better with one extra property - <code>filter:blur();</code>. </p>
<p>So your final code would look like this:</p>
<pre><code>.box-container {
    &#47;* Styles to make it less ugly *&#47;
    background: white;
    border-radius: 10px;
    border: 1px solid #eee;
    height: 200px;
    padding: 10px;
    position: relative;
    width: 250px;
}

.box-container-inner {
    bottom: 0;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    filter: blur(6px);
    height: 94%;
    left: 3%;
    position: absolute;
    width: 94%;
    z-index: -1;
}
</code></pre>
<p>Which renders out into a much smoother blend of a drop-shadow, creating a more realistic illusion of depth:</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>