Reece Reece - 1 year ago 42
CSS Question

Animating characters of text separately

I'm trying to animate the characters of text in a line of text separately. What I want to achieve is having them fly off and one the screen one at a time. I know I can do this using transitions and transform: translateX. But theres a couple problems that I'm having. Firstly the outer div for the text isn't wrapping all the characters and secondly applying transform: translateX to one of the spans doesn't work. I have created a little demo for you to inspect. Thanks.



#burger_container {
background-color: #404041;
display: block;
position: fixed;
top: 0;
left: 0;
bottom: 0;
width: 60px;
z-index: 101;
}

svg {
margin: 20px auto 0 auto;
display: block;
}

#logo_text {
transform: rotate(-90deg);
margin-top: 350px;
font-size: 40px;
color: #ffffff;
font-weight: 700;
}

#logo_text span {
font-weight: 400;
}

#logo_text span:nth-child(1){
transform: translatex(-50px);
}

<div id="burger_container">
<div>
<svg id="button" style="height: 26px; width: 26px;">
<g style="" fill="#f04d43">
<rect class="line1" x="0" y="1" rx="2" ry="2" width="26px" height="4px" />
<rect class="line2" x="0" y="11" rx="2" ry="2" width="26px" height="4px" />
<rect class="line3" x="0" y="21" rx="2" ry="2" width="26px" height="4px" />
</g>
</svg>

<div id="logo_text"><span>C</span><span>o</span><span>m</span><span>p</span><span>a</span><span>n</span><span>y</span>&nbsp;<span>W</span><span>o</span><span>r</span><span>k</span><span>f</span><span>o</span><span>r</span><span>c</span><span>e</span></div>
</div>





https://jsfiddle.net/a3x4ykza/

Answer Source

You have used span tag which by default is inline element, now if I change it display then it breaks the formation of how it's aligned, instead you could change it position to relative and then using CSS animation you could take-off each text one by one using top property as below,

Select each span tag and then as said use CSS animation.

#logo_text span:nth-of-type(8) {
  animation: mv 1s ease forwards;
}

#logo_text span:nth-of-type(9) {
  animation: mv 1s ease forwards 1s;
}

#logo_text span:nth-of-type(10) {
  animation: mv 1s ease forwards 2s;
}

#logo_text span:nth-of-type(11) {
  animation: mv 1s ease forwards 3s;
}

#logo_text span:nth-of-type(12) {
  animation: mv 1s ease forwards 4s;
}

#logo_text span:nth-of-type(13) {
  animation: mv 1s ease forwards 5s;
}

#logo_text span:nth-of-type(14) {
  animation: mv 1s ease forwards 6s;
}

#logo_text span:nth-of-type(15) {
  animation: mv 1s ease forwards 7s;
}

#logo_text span:nth-of-type(16) {
  animation: mv 1s ease forwards 8s;
}

@keyframes mv {
  from {
    top: 0;
  }
  to {
    top: -50px;
  }
}

#burger_container {
  background-color: #404041;
  display: block;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 60px;
  z-index: 101;
}

svg {
  margin: 20px auto 0 auto;
  display: block;
}

.line1,
.line2,
.line3 {
  transition: all 0.3s ease;
}

.open1 {
  transform-origin: top left;
  transform: translatex(3px) translatey(-1px) rotate(45deg);
  width: 33px;
}

.open2 {
  opacity: 0;
}

.open3 {
  transform-origin: bottom left;
  transform: translatex(3px) translatey(1px) rotate(-45deg);
  width: 33px;
}

#trans_overlay {
  display: none;
}

#logo_two {
  display: none;
}

#logo_text {
  transform: rotate(-90deg);
  margin-top: 300px;   /*Change this back to 350px, just to see output I have change it to 300px*/
  font-size: 40px;
  color: #ffffff;
  font-weight: 700;
}

#logo_text span {
  font-weight: 400;
  position: relative;
}

#logo_text span:nth-of-type(8) {
  animation: mv 1s ease forwards;
}

#logo_text span:nth-of-type(9) {
  animation: mv 1s ease forwards 1s;
}

#logo_text span:nth-of-type(10) {
  animation: mv 1s ease forwards 2s;
}

#logo_text span:nth-of-type(11) {
  animation: mv 1s ease forwards 3s;
}

#logo_text span:nth-of-type(12) {
  animation: mv 1s ease forwards 4s;
}

#logo_text span:nth-of-type(13) {
  animation: mv 1s ease forwards 5s;
}

#logo_text span:nth-of-type(14) {
  animation: mv 1s ease forwards 6s;
}

#logo_text span:nth-of-type(15) {
  animation: mv 1s ease forwards 7s;
}

#logo_text span:nth-of-type(16) {
  animation: mv 1s ease forwards 8s;
}

@keyframes mv {
  from {
    top: 0;
  }
  to {
    top: -50px;
  }
}
<div id="burger_container">
  <div>
    <svg id="button" style="height: 26px; width: 26px;">
      <g style="" fill="#f04d43">
        <rect class="line1" x="0" y="1" rx="2" ry="2" width="26px" height="4px" />
        <rect class="line2" x="0" y="11" rx="2" ry="2" width="26px" height="4px" />
        <rect class="line3" x="0" y="21" rx="2" ry="2" width="26px" height="4px" />
      </g>
    </svg>
  </div>

  <div id="logo_text"><span>C</span><span>o</span><span>m</span><span>p</span><span>a</span><span>n</span><span>y</span>&nbsp;<span>W</span><span>o</span><span>r</span><span>k</span><span>f</span><span>o</span><span>r</span><span>c</span><span>e</span></div>
</div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download