Ravi Singh
Ravi Singh

Reputation: 315

How to make this design responsive?

I have tried to make an animation using CSS keyframes, but the design is breaking. How can I make it responsive without writing media queries, as it's just a simple animation code?

div {
  font-family: "Comic Sans MS", sans-serif;
  margin-left: 200px;
  margin-top: 200px;
}

span {
  color: white;
  font-size: 35px;
  padding: 14px 25px;
  margin: 5px;
  border-radius: 10px;
  display: inline-block;
  animation: move 0.8s infinite linear;
}

@keyframes move {
  0% {
    transform: translateY(0px);
    opacity: 1.0;
  }
  50% {
    transform: translateY(-50px);
    opacity: 0.5;
  }
  100% {
    transform: translateY(0px);
    opacity: 1.0;
  }
}

span:nth-child(1) {
  animation-delay: 0.1s;
  background-color: red;
  box-shadow: 5px 5px 0 black;
}

span:nth-child(2) {
  animation-delay: 0.2s;
  background-color: blue;
  box-shadow: 5px 5px 0 black;
}

span:nth-child(3) {
  animation-delay: 0.3s;
  background-color: green;
  box-shadow: 5px 5px 0 black;
}

span:nth-child(4) {
  animation-delay: 0.4s;
  background-color: orangered;
  box-shadow: 5px 5px 0 black;
}

span:nth-child(5) {
  animation-delay: 0.5s;
  background-color: springgreen;
  box-shadow: 5px 5px 0 black;
}

span:nth-child(6) {
  animation-delay: 0.6s;
  background-color: blueviolet;
  box-shadow: 5px 5px 0 black;
}

span:nth-child(7) {
  animation-delay: 0.7s;
  background-color: purple;
  box-shadow: 5px 5px 0 black;
}
<div>
  <span>W</span>
  <span>E</span>
  <span>L</span>
  <span>C</span>
  <span>O</span>
  <span>M</span>
  <span>E</span>
</div>

Upvotes: 0

Views: 44

Answers (1)

disinfor
disinfor

Reputation: 11533

Without using a media query, you are limited to what you can do, but in this example, I've removed your set margin-left value and used a percentage instead and made the containing div a percentage width. I've also made the parent div a flex container - this will make sure things have a harder time wrapping on smaller screens.

I then used vw units for the font-size, so it will scale with the window width. Also the padding on the boxes needs to be set to percentages. You can play with it here:

https://jsfiddle.net/disinfor/s5hkyr2f/3/

div {
  font-family: "Comic Sans MS", sans-serif;
  margin: 200px auto 0;
  width: 90%;
  display: flex;
  justify-content: center;
}

span {
  color: white;
  font-size: 4vw;
  padding: 2% 3%;
  margin: 5px;
  border-radius: 10px;
  display: inline-block;
  animation: move 0.8s infinite linear;
}

@keyframes move {
  0% {
    transform: translateY(0px);
    opacity: 1.0;
  }
  50% {
    transform: translateY(-50px);
    opacity: 0.5;
  }
  100% {
    transform: translateY(0px);
    opacity: 1.0;
  }
}

span:nth-child(1) {
  animation-delay: 0.1s;
  background-color: red;
  box-shadow: 5px 5px 0 black;
}

span:nth-child(2) {
  animation-delay: 0.2s;
  background-color: blue;
  box-shadow: 5px 5px 0 black;
}

span:nth-child(3) {
  animation-delay: 0.3s;
  background-color: green;
  box-shadow: 5px 5px 0 black;
}

span:nth-child(4) {
  animation-delay: 0.4s;
  background-color: orangered;
  box-shadow: 5px 5px 0 black;
}

span:nth-child(5) {
  animation-delay: 0.5s;
  background-color: springgreen;
  box-shadow: 5px 5px 0 black;
}

span:nth-child(6) {
  animation-delay: 0.6s;
  background-color: blueviolet;
  box-shadow: 5px 5px 0 black;
}

span:nth-child(7) {
  animation-delay: 0.7s;
  background-color: purple;
  box-shadow: 5px 5px 0 black;
}
<div>
  <span>W</span>
  <span>E</span>
  <span>L</span>
  <span>C</span>
  <span>O</span>
  <span>M</span>
  <span>E</span>
</div>

Upvotes: 2

Related Questions