How can I change text animations so that they just show up in their own space?

Viewed 44

I was adding some animations with CSS to the site I was working on. But the overflow: hidden attribute doesn't seem to work the way I expected it should. Here's my code.

.jumbotron {
    height: 100%;
    height: 100vh;
    align-items: center;
    display: flex;
    position: relative;
}

.sty {
    display: inline-block;
    position: relative;
}

.sty1 {
    position: relative;
    animation: firanim 1.5s ease-in-out;
    overflow: hidden;
}
.sty2 {
    position: relative;
    animation: secanim 1s ease-in-out;
    overflow: hidden;
}

@keyframes firanim {
    from {top:-50px;}
    to {top:0px;}
}
@keyframes secanim {
    from {left:-55vw;}
    to {left:0px;}
}
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <link rel="stylesheet" href="css/style.css" type="text/css">
  <!-- EndCSS -->

    <title>lorem ipsum</title>
  </head>
  <body>
    <nav class="navbar navbar-expand-sm navbar-dark bg-dark text-white sticky-top">
        <a class="navbar-brand" href="#">Under Development</a>
        <button class="navbar-toggler d-lg-none" type="button" data-toggle="collapse" data-target="#collapsibleNavId" aria-controls="collapsibleNavId"
            aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
        <div class="collapse navbar-collapse justify-content-end" id="collapsibleNavId">
          <ul class="navbar-nav">
            <li class="nav-item">
              <a class="nav-link" href="#">About</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Follow</a>
            </li>
          </ul>
        </div>
    </nav>


    
    <div class="jumbotron justify-content-center">
      <div class="text-monospace sty">
        <h1 class="display-3">welcome<span class="text-danger">!</span></h1>
        <div class="sty1">
        <span class="lead">lorem ipsum lorem <span class="text-warning">LOREM IPSUM.</span></span>
        </div>
        <div class="sty2">
        <span class="lead">lorem ipsum lorem ipsum lorem.</span>
        </div>
      </div>
    </div>



<!-- JS -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<!-- EndJS -->
  </body>
</html>

So is there any additional class missing in my code or anything wrong I am using? Because I was expecting the span element with .lead class to just show in his own space not like some text flying over to the place from somewhere. I am hoping you'll get what i wanted to say.

3 Answers

You are animating the whole box (.sty1 and .sty2), the text (.lead) its not outside the (.sty1 and .sty2) thats why its not getting hidden by overflow: hidden

you need to keep the (.sty1 and .sty2) in thier place and animate .lead

try this

.jumbotron {
  height: 100%;
  height: 100vh;
  align-items: center;
  display: flex;
  position: relative;
}

.sty {
  display: inline-block;
  position: relative;
}

.sty1 {
  overflow: hidden;
}

.sty2 {
  overflow: hidden;
}

.sty1 .lead {
  position: relative;
  animation: firanim 1.5s ease-in-out;
}

.sty2 .lead {
  position: relative;
  animation: secanim 1s ease-in-out;
}

@keyframes firanim {
  from {
    top: -50px;
  }

  to {
    top: 0px;
  }
}

@keyframes secanim {
  from {
    left: -55vw;
  }

  to {
    left: 0px;
  }
}

your animation it's set on the .leads container(.sty1 and .sty2), so that's the one that it's moving , your lead are moving along with it, to fix it you can wrap those in a div with a class that will have its overflow to hidden, like this

I added a animation-container and set overflow:hidden, see the updated html

.jumbotron {
  height: 100%;
  height: 100vh;
  align-items: center;
  display: flex;
  position: relative;
}

.sty {
  display: inline-block;
  position: relative;
}

.sty1 {
  position: relative;
  animation: firanim 1.5s ease-in-out;
  overflow: hidden;
}

.sty2 {
  position: relative;
  animation: secanim 1s ease-in-out;
  overflow: hidden;
}

.animation-container {
  overflow: hidden;
}

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

@keyframes secanim {
  from {
    left: -55vw;
  }
  to {
    left: 0px;
  }
}
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
  <link rel="stylesheet" href="css/style.css" type="text/css">
  <!-- EndCSS -->

  <title>Anurag Srivastava - Portfolio</title>
</head>

<body>
  <nav class="navbar navbar-expand-sm navbar-dark bg-dark text-white sticky-top">
    <a class="navbar-brand" href="#">Under Development</a>
    <button class="navbar-toggler d-lg-none" type="button" data-toggle="collapse" data-target="#collapsibleNavId" aria-controls="collapsibleNavId" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
    <div class="collapse navbar-collapse justify-content-end" id="collapsibleNavId">
      <ul class="navbar-nav">
        <li class="nav-item">
          <a class="nav-link" href="#">About</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Follow</a>
        </li>
      </ul>
    </div>
  </nav>



  <div class="jumbotron justify-content-center">
    <div class="text-monospace sty">
      <h1 class="display-3">welcome<span class="text-danger">!</span></h1>
      <div class="animation-container">
        <div class="sty1">
          <span class="lead">lorem ipsum lorem <span class="text-warning">LOREM IPSUM.</span></span>
        </div>
      </div>
      <div class="sty2">
        <span class="lead">lorem ipsum lorem ipsum lorem.</span>
      </div>
    </div>
  </div>



  <!-- JS -->
  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
  <!-- EndJS -->
</body>

</html>

Try the following HTML:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <link rel="stylesheet" href="css/style.css" type="text/css">
  <!-- EndCSS -->

    <title>Anurag Srivastava - Portfolio</title>
  </head>
  <body>
    <nav class="navbar navbar-expand-sm navbar-dark bg-dark text-white sticky-top">
        <a class="navbar-brand" href="#">Under Development</a>
        <button class="navbar-toggler d-lg-none" type="button" data-toggle="collapse" data-target="#collapsibleNavId" aria-controls="collapsibleNavId"
            aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
        <div class="collapse navbar-collapse justify-content-end" id="collapsibleNavId">
          <ul class="navbar-nav">
            <li class="nav-item">
              <a class="nav-link" href="#">About</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Follow</a>
            </li>
          </ul>
        </div>
    </nav>


    
    <div class="jumbotron justify-content-center">
      <div class="text-monospace sty">
        <h1 class="display-3">welcome<span class="text-danger">!</span></h1>
        <div class="sty1">
        <span class="lead_1">lorem ipsum lorem <span class="text-warning">LOREM IPSUM.</span></span>
        </div>
        <div class="sty2">
        <span class="lead_2">lorem ipsum lorem ipsum lorem.</span>
        </div>
      </div>
    </div>



<!-- JS -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<!-- EndJS -->
  </body>
</html>

And the following CSS

.jumbotron {
    height: 100%;
    height: 100vh;
    align-items: center;
    display: flex;
    position: relative;
}

.sty {
    display: inline-block;
    position: relative;
}

.sty1 {
    overflow: hidden;
}
.lead_1{
  animation: firanim 1.5s ease-in-out;
  position: relative;
}
.lead_2{
  position: relative;
  animation: secanim 1s ease-in-out;
}
.sty2 {
    overflow: hidden;
}

@keyframes firanim {
    from {top:-50px;}
    to {top:0px;}
}
@keyframes secanim {
    from {left:-55vw;}
    to {left:0px;}
}

Hope it helps! :)

Related