<div class="buddywrapper">
<input name="emotions" class="excited" id="excited" type="radio" checked>
<label class="excited" for="excited">Excited</label>
<input name="emotions" class="happy" id="happy" type="radio">
<label class="happy" for="happy">Happy</label>
<input name="emotions" class="sad" id="sad" type="radio">
<label class="sad" for="sad">Sad</label>
<button type="button" class="buddy">
<span class="face">
<svg viewBox="0 0 100 60">
<ellipse cx="15" cy="12" rx="8" ry="9" class="eye right-eye" fill="currentColor" />
<ellipse cx="80" cy="12" rx="8" ry="9" class="eye left-eye" fill="currentColor" />
<path stroke-linecap="round" d="M30 40 c0 20, 40 20, 40 0" fill="currentColor" stroke="currentColor" />
<!--
Uncomment to visualize the Bezier control points
<circle cx="30" cy="40" r="2.5" fill="red"/>
<circle cx="40" cy="60" r="2.5" fill="red"/>
<circle cx="60" cy="60" r="2.5" fill="red"/>
<circle cx="70" cy="40" r="2.5" fill="red"/>
-->
</svg>
</span>
Buddy
</button>
</div>
<a href="https://buttonbuddy.dev">Visit ButtonBuddy.dev to create accessible button colors</a>
/*
* Visit ButtonBuddy.dev to create new colors for Buddy!
*
* Tutorial on animatating the SVG Face:
* https://dev.to/5t3ph/how-to-create-an-animated-svg-face-with-css-5djd
*/
:root {
--surface: #27272c;
--background: #00a4b6;
--color: #222222;
--focus-background: #ffffff;
--focus-color: #222222;
}
* {
box-sizing: border-box;
}
/* Learn about button styling in my in-depth guide:
* https://moderncss.dev/css-button-styling-guide/
*/
.buddy {
/* Reset */
border: none;
background-color: transparent;
font-family: inherit;
font-size: 1.125rem;
padding: 0;
cursor: pointer;
@media screen and (-ms-high-contrast: active) {
border: 2px solid currentColor;
}
/* Core styles */
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
background-color: var(--background);
color: var(--color);
border-radius: 0.15em;
padding: 0.35em 0.75em 0.25em;
font-size: clamp(3rem, 5vw, 4rem);
text-align: center;
line-height: 1;
font-weight: 500;
transition: 180ms all ease-in-out;
grid-area: buddy;
justify-self: center;
&:focus {
outline-style: solid;
outline-color: transparent;
background-color: var(--focus-background);
color: var(--focus-color);
}
.face {
width: 1em;
height: 1em;
margin-right: 0.25em;
transform: translateY(-0.05em);
svg {
width: 100%;
height: 100%;
transform: rotate(-10deg);
path {
transition: all 180ms ease-in;
}
}
}
}
svg.wink .left-eye {
transform: scale(1);
transform-origin: 90% 20%;
animation: wink 480ms ease-in-out 1;
}
/* Curious how this is working?
* Check out my guide to advanced CSS selectors:
* https://moderncss.dev/guide-to-advanced-css-selectors-part-one/
*/
#happy:checked ~ button svg path,
#sad:checked ~ button svg path {
fill: transparent;
stroke-width: clamp(6px, 1vw, min(0.1em, 6px));
}
#sad:checked ~ button svg path {
transform: scaleY(-1);
transform-origin: 50% 80%;
}
@keyframes wink {
0%,
20%,
70% {
transform: scale(1.5, 0.25);
}
30%,
50%,
90%,
100% {
transform: scale(1);
}
}
body {
background-color: var(--surface);
font-family: "Baloo 2", sans-serif;
font-weight: 500;
display: grid;
place-content: center;
justify-items: center;
grid-gap: 2rem;
min-height: 100vh;
margin: 0;
padding: 1rem;
color: #fff;
a {
color: inherit;
}
}
.buddywrapper {
display: grid;
grid-template-areas: "excited happy sad" "buddy buddy buddy";
grid-gap: 1rem;
place-content: center;
align-items: center;
}
.excited {
grid-area: excited;
}
.happy {
grid-area: happy;
}
.sad {
grid-area: sad;
}
label {
margin-left: 1.5rem;
margin-top: 0.15rem;
font-size: 1.5rem;
}
a {
max-width: 30ch;
text-align: center;
visibility: hidden;
}
@media (prefers-reduced-motion: reduce) {
* {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
View Compiled
const buddy = document.querySelector(".buddy svg");
// Only add the class if Buddy isn't sad
setInterval(() => {
if (!document.querySelector("#sad:checked")) buddy.classList.add("wink");
}, 5000);
// Remove the wink class to reset the animation after it ends
buddy.addEventListener("animationend", () => {
buddy.classList.remove("wink");
});
View Compiled
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.