'CSS Animation to expand div from center outwards
I am trying to make the following expand outwards from the center (almost like curtains opening).
It should kind of look like this, but instead of -
it's empty space.
-------------
------t------
-----sti-----
----estin----
---Testing---
.animation{
width: 100%;
text-align: center;
font-size: 50px;
padding: 25px;
background: black;
color: white;
}
<div class="animation">
Testing
</div>
I'd also like to make it without using JavaScript if possible.
Solution 1:[1]
here is an idea way without clip-path
and relying on flexbox
.animation {
width: 0%;
margin: auto;
display: flex;
justify-content: center;
white-space: nowrap;
font-size: 50px;
padding: 25px 0;
background: black;
color: white;
overflow: hidden;
transition: 0.5s all;
}
body:hover .animation {
width: 100%;
}
<div class="animation">
Testing
</div>
You can also play with pseudo element and background in case you don't need transparency:
.animation{
width: 100%;
text-align: center;
font-size: 50px;
padding: 25px 0;
background: black;
color: white;
position:relative;
}
.animation:before{
content:"";
position:absolute;
inset:0;
background:
linear-gradient(#fff 0 0) left ,
linear-gradient(#fff 0 0) right;
background-size: 50.5% 100%;
background-repeat: no-repeat;
transition: 0.5s all;
}
.animation:hover::before {
background-size: 0% 100%;
}
<div class="animation">
Testing
</div>
Another idea using box-shadow
:
.animation{
width: 100%;
text-align: center;
font-size: 50px;
padding: 25px 0;
background: black;
color: white;
position:relative;
box-shadow:
50vw 0 0 #fff inset,
-50vw 0 0 #fff inset;
transition:0.5s all;
}
.animation:hover {
box-shadow:
0 0 0 #fff inset,
0 0 0 #fff inset;
}
<div class="animation">
Testing
</div>
You can also use mask:
.animation{
width: 100%;
text-align: center;
font-size: 50px;
padding: 25px 0;
background: black;
color: white;
--m: linear-gradient(#fff 0 0) center/var(--p,0%) 100% no-repeat;
-webkit-mask: var(--m);
mask: var(--m);
transition: 0.5s all;
}
.animation:hover {
--p: 100%;
}
<div class="animation">
Testing
</div>
Solution 2:[2]
Use clip-path
and CSS animations.
Note: On Safari and other browsers the prefix -webkit
is needed
The first keyframe has the 4 points at the center on the X axis but in their final (maximum) place's on the Y axis. This allows us to only animate the X axis.
The second keyframe moves the 4 points outwards to their maximum positions along the X axis, with the Y portion remaining the same.
A bit confusing but see below:
.animation{
width: 100%;
text-align: center;
font-size: 50px;
padding: 25px;
background: black;
color: white;
animation: expand_center 5000ms;
animation-fill-mode: forwards;
}
@keyframes expand_center {
0% {
clip-path: polygon(50% 100%,50% 0,50% 0,50% 100%);
-webkit-clip-path: polygon(50% 100%,50% 0,50% 0,50% 100%);
}
100%{
clip-path: polygon(0 100%, 0 0, 100% 0, 100% 100%);
-webkit-clip-path: polygon(0 100%, 0 0, 100% 0, 100% 100%);
}
}
<div class="animation">
Testing
</div>
Credit to @KendallFrey for the clip-path idea.
Solution 3:[3]
You could try transitioning the width
and use flex
to align the text
.animation {
width: 100%;
font-size: 50px;
padding: 25px 0;
background: black;
color: white;
margin: 0 auto;
display: flex;
justify-content: center;
animation: grow 2s;
}
@keyframes grow {
0% {
width: 0;
}
100% {
width: 100%;
}
}
<div class="animation">
Testing
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | JBis |
Solution 3 | itodd |