0

I'm trying to use Css animation to create a location type animation in which the location div animates over a point of interest marked on the line div. My first point of interest is the centre of the line div. However, the following code doesn't seem to be doing what I want.

  top: 116px;
  left: 50%;
  transform: translate(-50%, -50%);

.location {
  position: absolute;
  top: 116px;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 120px;
  height: 120px;
  border-radius: 50%;
  border: 16px solid #f3f3f3;
  border-top: 16px solid #3498db;
  border-bottom: 16px solid #3498db;
  animation: Location 4s linear infinite;
}

@keyframes Location {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.line {
  position: absolute;
  top: 100px;
  left: 50%;
  transform: translateX(-50%);
  border: 8px solid #f3f3f3;
  width: 500px;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
</head>

<body>
  <div class="location"></div>
  <div class="line"></div>
</body>

</html>
Simple
  • 437
  • 7
  • 19
  • 1
    the rotation is overriding the translation for the centring – Temani Afif Feb 27 '19 at 21:44
  • I realised this when I saw Dogukan Cavus answer. How do you find all these similar questions? I wouldn't even think of searching for anything close to the heading title. – Simple Feb 27 '19 at 21:48
  • 1
    that's why we have duplicate closure ;) to close similar question and make it easy to search later. Someone may find your question and then will see the other one. – Temani Afif Feb 27 '19 at 21:50
  • Why not just put them in the same div and absolutely position that parent? – Pytth Feb 27 '19 at 21:56
  • If they were in the same div, wouldn't I still need to position at least one of them in order for them to overlap? – Simple Feb 27 '19 at 22:02

1 Answers1

1

Something like this? You should also add an translate(-50%,-50%) to the animation. Otherwise, it deletes translate(-50%,-50%) on and overwrites it.

.location {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 120px;
  height: 120px;
  border-radius: 50%;
  border: 16px solid #f3f3f3;
  border-top: 16px solid #3498db;
  border-bottom: 16px solid #3498db;
  animation: Location 4s linear infinite;
}

@keyframes Location {
  0% {
    transform:translate(-50%, -50%) rotate(0deg);
  }
  100% {
    transform:translate(-50%, -50%) rotate(360deg);
  }
}

.line {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: 8px solid #f3f3f3;
  width: 500px;
}
  <div class="location"></div>
  <div class="line"></div>
doğukan
  • 23,073
  • 13
  • 57
  • 69