I would like to achieve the following animation with CSS:
- From the center the circle scales to 100%, so the entire circle is filled with a color
- Then again from the center the fill deformed into a stroke around the circle
I thought the best way is to use the before or after pseudo, so you can animate to circles. The problem I think I run into is that the animation on the parent is also used within the pseudo class.
If there is a better way to achieve this, I am open for suggestions 🙂
https://codepen.io/Caspert/pen/JjaMgKe
HTML:
<div class="circle"></div>
SCSS:
@keyframes parentFromCenter {
0% {
transform: scale(0);
}
100% {
transform: scale(1);
}
}
@keyframes childFromCenter {
0% {
transform: scale(0);
}
100% {
transform: scale(1);
}
}
.circle {
height: 200px;
width: 200px;
box-sizing: border-box;
border-radius: 50vh;
overflow: hidden;
position: relative;
background: black;
animation: parentFromCenter 1s ease-in;
&::before {
content: "";
height: 90%;
width: 90%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
border-radius: inherit;
animation: childFromCenter 1s ease-in;
}
}
4
Answers
You don’t need to use pseudo-elements. You can just add another div inside the circle and animate each of them like so:
Also, add the
forwards
keyword to the animation so the elements keep the styling from the animation.Here is an example: https://codepen.io/raul-rogojan/pen/dyqJxVB
If you want the inner circle to be a cutout and see the content behind u can achieve the same effect using
box-shadow
This is the updateI have also updated the code pen: https://codepen.io/raul-rogojan/pen/dyqJxVB
I’ll throw in my two cents…
Using pseudo elements for styling seems like a good idea – but I’d go further and completely separate out the animation styling from the actual element.
This snippet keeps the element itself at its full dimensions and animates the before an after pseudo elements to grow from scale 0 to scale 1.
The before pseudo element is black and starts to grow immediately. The after pseudo element is white and waits 1 second before starting to grow.