Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

  1. -------------
  2. ------t------
  3. -----sti-----
  4. ----estin----
  5. ---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.

like image 426
JBis Avatar asked Dec 24 '22 02:12

JBis


2 Answers

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>
like image 172
Temani Afif Avatar answered Jan 05 '23 01:01

Temani Afif


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.

like image 28
JBis Avatar answered Jan 05 '23 00:01

JBis