aboutsummaryrefslogtreecommitdiff
path: root/build/shiny-css-buttons/index.html
blob: be58ddb870826276d8cd6f503b4d719f0759b5b6 (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
127
128
129
<!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>Shiny, Animated CSS Buttons</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="shiny-animated-css-buttons">Shiny, Animated CSS Buttons</h1>

<p>2021-04-27</p>

<p>Everyone can appreciate fancy, animated buttons - but often times they come with a performance cost: <em>JavaScript</em>. Luckily for us, we can create our very own shiny, animated buttons with pure CSS.</p>

<h2 id="the-demo">The Demo</h2>

<p><img src="/public/images/shiny-buttons.png" alt="Four buttons that shine when hovered" /></p>

<p><a href="https://codepen.io/bradleytaunt/pen/oNBQevj">Live CodePen Example</a></p>

<h2 id="the-html">The HTML</h2>

<p>Nothing fancy going on here, just a set of <code>ahref</code> elements with specific <code>button</code> classes added:</p>

<pre><code>&#60;a href="#" class="button green"&#62;&#60;span&#62;Green Button&#60;&#47;span&#62;&#60;&#47;a&#62;
&#60;a href="#" class="button blue"&#62;&#60;span&#62;Blue Button&#60;&#47;span&#62;&#60;&#47;a&#62;
&#60;a href="#" class="button orange"&#62;&#60;span&#62;Orange Button&#60;&#47;span&#62;&#60;&#47;a&#62;
&#60;a href="#" class="button purple"&#62;&#60;span&#62;Purple Button&#60;&#47;span&#62;&#60;&#47;a&#62;
</code></pre>

<h2 id="the-css">The CSS</h2>

<p>First we set the default base styling for all the buttons. We also place the inner text into <code>span</code> elements (I will explain why in a little bit):</p>

<pre><code>.button {
    background: white;
    border: 1px solid #a5b1c2;
    border-radius: 6px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    color: #111111;
    display: inline-block;
    margin: 1rem auto;
    min-width: 180px;
    overflow: hidden;
    padding: 15px 30px;
    position: relative;
    text-align: center;
    text-decoration: none;
    transition: .3s ease-in-out all;
}
.button span {
    position: relative;
    z-index: 2;
}
</code></pre>

<p>Now we need to create our <em>shiny</em> element that will pass across the button on <code>hover</code> or <code>focus</code>. For this object we will use the <code>before</code> pseudo element:</p>

<pre><code>.button:before {
    background: linear-gradient(transparent 0%, rgba(255,255,255,0.6) 50%, transparent 100%);
    content:&#39;&#39;;
    height: 200%;
    position: absolute;
    right: calc(100% + 20px);
    top: -55%;
    transform: rotate(-70deg);
    transition: .6s ease-in-out right;
    width: 80px;
    z-index: 0;
}
</code></pre>

<p>Next, we tell the <code>before</code> element to swipe across the main <code>.button</code> parent element when the user hovers or focuses on it. Remember placing our inner content into a <code>span</code> element? That insures that our shiny&#47;swipe element doesn&#8217;t position itself <em>over</em> the text, but instead flows under it:</p>

<pre><code>.button:hover:before {
    right: -100%;
}
&#47;* Extra visual styling for buttons on hover - optional *&#47;
.button:hover, button:focus {
    box-shadow: 0 8px 12px rgba(0,0,0,0.1), inset 0 10px 30px rgba(255,255,255,0.3), inset 0 2px 2px rgba(255,255,255,0.2);
    color: white;
}
</code></pre>

<p>All that&#8217;s left is adding some visual flare to each individual button - in this case background-color and border-color:</p>

<pre><code>.button.green:hover, button.green:focus { background: #20bf6b; border-color: #20bf6b; }
.button.blue:hover, button.blue:focus { background: #0984e3; border-color: #0984e3; }
.button.orange:hover, button.orange:focus { background: #ff793f; border-color: #ff793f; }
.button.purple:hover, button.purple:focus { background: #6c5ce7; border-color: #6c5ce7; }
</code></pre>

<h2 id="browser-support">Browser Support</h2>

<p>These buttons work across all browsers flawlessly. See the details <a href="https://caniuse.com/css-gencontent">on the caniuse report itself</a>.</p>

<h2 id="the-live-codepen">The Live CodePen</h2>

<p>You can find the live demo embedded at the top of this post, or <a href="https://codepen.io/bradleytaunt/pen/oNBQevj">directly on CodePen here</a>.</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>