How to retain responsive page layout with CSS background animations and flex?

Viewed 29

I am trying to have an animated background with content but the animated background keeps getting displaced. The only way the animated background is without content which I cant have for my site.

The animation is three vertical lines spaced evenly apart. In each iteration a portion of one vertical line will turn white and fall down the vertical line, for every vertical line. Ive included an image to show what I mean. How can I retain responsiveness and be able to add content placed on top of the animated background(ie an h1 tag and some nav elements)? Also is there a way to have this with css flexbox?


.lines {
  position: absolute;
  height: 100%;
  margin: auto;
  width: 90vw;
}

.line {
  position: absolute;
  width: 1px;
  height: 100%;
  top: 0;
  left: 50%;
  background: rgba(255, 255, 255, 0.1);
  overflow: hidden;
}

.line::after {
  content: "";
  display: block;
  position: absolute;
  height: 15vh;
  width: 100%;
  top: -50%;
  left: 0;
  background: linear-gradient(to bottom, rgba(255, 255, 255, 0) 0%, #ffffff 75%, #ffffff 100%);
  animation-name: backAnimation;
  animation-duration: 7s;
  animation-iteration-count: infinite;
  animation-fill-mode: forwards;
  animation-timing-function: cubic-bezier(0.4, 0.26, 0, 0.97);
}

.line:nth-child(1) {
  margin-left: -25%;
}

.line:nth-child(1)::after {
  animation-delay: 2s;
}

.line:nth-child(3) {
  margin-left: 25%;
}

.line:nth-child(3)::after {
  animation-delay: 2.5s;
}

animation image

1 Answers

CSS has position: fixed, also position: absolute can be used for that.

Fixed example:

html, body {margin: 0; padding: 0}
body{min-height: 100vh}

.background {
  background: linear-gradient(red, blue);
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-evenly;
  z-index: -1;
}

.background-line {
  background-color: white;
  min-width: 10px;
}

.content {
  margin: 100px auto;
  width: 300px;
  background-color: #00000044;
  padding: 20px;
  height: 2000px;
}
<div class="background">
  <div class="background-line"></div>
  <div class="background-line"></div>
  <div class="background-line"></div>
</div>
<div class="content">
  content
</div>

Absolute example:

html, body {margin: 0; padding: 0}
body{min-height: 100vh; position: relative; padding: 100px 0;}

.background {
  background: linear-gradient(red, blue);
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 100%;
  display: flex;
  justify-content: space-evenly;
  z-index: -1;
}

.background-line {
  background-color: white;
  min-width: 10px;
}

.content {
  margin: auto;
  width: 300px;
  background-color: #00000044;
  padding: 20px;
  height: 2000px;
}
<html>
<body>

<div class="background">
  <div class="background-line"></div>
  <div class="background-line"></div>
  <div class="background-line"></div>
</div>
<div class="content">
  content
</div>

</body>
</html>

Related