← 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:
css
.btn {
position: relative;
}
The vertical line represents the `:before` selector which is positioned absolutely:
css
.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:
css
.btn::before {
left: 50%;
transform: translate(-50%, 0);
}
In the same way, the `:after` selector can be used to represent the horizontal line:
css
.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:
html
<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:
css
.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:
css
.btn--rotate {
transform: rotate(45deg);
}
To animate the button, we can add a transition for the `transform` property:
css
.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:
js
buttonEle.addEventListener('click', () => {
buttonEle.classList.toggle('btn--rotate');
});
If you found this post helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

Questions? 🙋

Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
Ask me questions

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