← Back toCSS Animation

More actions

Written byPhuoc Nguyen
Created
29 Sep, 2022
The animation is often used when we show additional actions inside a menu or popover.

Layout

You can use a SVG element to create the plus icon inside the button. Another approach seen in the demo above uses two different elements representing the `:before` and `:after` selectors of the button.
To archive that, we use the relative position of the button:
.btn {
position: relative;
}
The vertical line represents the `:before` selector which is positioned absolutely:
.btn::before {
background: #4b5563;

/* Absolute position */
content: '';
position: absolute;
bottom: 0.25rem;
top: 0.25rem;

/* Size */
width: 1px;
}
The vertical line is displayed at the center of the button exactly via two CSS declarations:
.btn::before {
left: 50%;
transform: translate(-50%, 0);
}
In the same way, the `:after` selector can be used to represent the horizontal line:
.btn::after {
background: #4b5563;

/* Absolute position */
content: '';
position: absolute;
left: 0.25rem;
right: 0.25rem;

/* Display at the center horizontally */
top: 50%;
transform: translate(0, -50%);

/* Size */
height: 1px;
}
To display the popover or menu right under the button, you can organize the layout as follows:
<div class="container">
<!-- The button -->
<button class="btn"></button>

<!-- The additional contents -->
<div class="popover">...</div>
</div>
The popover should be positioned absolutely to its container:
.container {
position: relative;
}

.popover {
position: absolute;
left: 0;
top: 100%;
margin-top: 0.5rem;
}
You can take a look at the popover arrow pattern if you want to include an arrow at the top left corner of the popover

Animation

When users click the button, it will be rotated by 45 degrees. The target variant has the following styles:
.btn--rotate {
transform: rotate(45deg);
}
To animate the button, we can add a transition for the `transform` property:
.btn {
transform-origin: center center;
transition: transform 100ms;
}
Clicking the button needs to toggle the `btn--rotate` class, assuming that the `buttonEle` variable represents the button element:
buttonEle.addEventListener('click', () => {
buttonEle.classList.toggle('btn--rotate');
});

Questions? 🙋

Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.

Recent posts ⚡

Newsletter 🔔

If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn