Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

html - CSS Skill Bar Animation

I am trying to create a skill bar where the background color of the progress slides in but the label for the Skill bar stays in the same place.

.skill-container * {
  box-sizing: border-box;
}

.skill-container {
  width: 100%;
  border-radius: 5px;
  background-color: lightgray;
  margin: 20px 0;
  overflow: hidden;
}

.skill-container .skill {
  display: inline-block;
  text-align: left;
  color: white;
  padding: 10px 0 10px 4px;
  border-radius: inherit;
  background-color: #312E81;
  white-space: nowrap;
  position: relative;
  animation: animateLeft 1s ease-out;
}

@keyframes animateLeft {
  from {
    left: -100%
  }
  to {
    left: 0
  }
}
.skill-container .skill.skill-level-70 {
  width: 70%
}
<div class="skill-container">

  <span class="skill skill-level-70">CSS</span>

</div>
question from:https://stackoverflow.com/questions/65837078/css-skill-bar-animation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use a pseudo element for the background:

.skill-container .skill {
  color: white;
  background-color: lightgray;
  margin: 5px 0;
  padding: 10px 0 10px 4px;
  overflow: hidden;
  border-radius: 5px;
  white-space: nowrap;
  position: relative;
  z-index:0;
}

.skill-container .skill::before {
  content: "";
  position: absolute;
  background-color: #312E81;
  border-radius: inherit;
  z-index: -1;
  top: 0;
  bottom: 0;
  left: 0;
  animation: animateLeft 1s ease-out;
}

@keyframes animateLeft {
  from {
    transform: translateX(-100%);
  }
}

.skill-container .skill.skill-level-70::before {
  width: 70%
}
.skill-container .skill.skill-level-40::before {
  width: 40%
}
.skill-container .skill.skill-level-100::before {
  width: 100%
}
<div class="skill-container">
  <div class="skill skill-level-100">CSS</div>
  <div class="skill skill-level-70">HTML</div>
  <div class="skill skill-level-40">PHP</div>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...