Let's start off with the simplest of buttons:
<button class="btn">Click Anywhere</button>
We will give the button some basic CSS for resetting its appearance across browsers, center the text and give it some life.
We will use a fixed width and height and center the text horizontically and vertically using Flexbox.
I would normally not hard-code the width and just give it some padding, to conform to dynamic text, but the reason will be explained later on.
Just trust me for now. 😇
.btn {
/* Reset styles */
appearance: none;
outline: none;
background: none;
border: none;
width: 12em;
height: 3em;
/* Center text */
display: flex;
justify-content: center;
align-items: center;
padding: 0.5em 1em;
font-size: 20px;
border-radius: 0.3em;
background: #42a5f5;
color: #e3f2fd;
}
Let's add some shadow for a more material design-y look.
I usually go for a big soft shadow, stacked with a tighter more profound shadow. We will add a
transition for a nice animation.Note that animatingbox-shadowis not GPU-accelerated (likeopacityandtransform) so it may cause some UI jank. Use with caution!
.btn {
/* Reset styles */
appearance: none;
outline: none;
background: none;
border: none;
width: 12em;
height: 3em;
/* Center text */
display: flex;
justify-content: center;
align-items: center;
padding: 0.5em 1em;
font-size: 20px;
border-radius: 0.3em;
background: #42a5f5;
color: #e3f2fd;
transition: box-shadow 250ms ease;
}
.btn:hover {
box-shadow:
2px 5px 5px 0px rgba(2, 119, 11, 0.2),
/* soft shadow */ 1px 2px 3px 0 rgba(2, 119, 11, 0.1); /* harsh shadow */
}
