Animating CSS Skill Bars

My goal is to create a skill bar that smoothly slides in the background color of the progress while keeping the label for the skill in a fixed position.

.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>

I have successfully implemented the animation, but I am working on finding a way to ensure that the text (CSS) stays on the left while the background color dynamically shows the progress.

Answer №1

Try using a pseudo element to create the background effect:

.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>

You can enhance this with CSS variables for optimization:

.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;
  width:var(--l,0%);
  animation: animateLeft 1s ease-out;
}

@keyframes animateLeft {
  from {
    transform: translateX(-100%);
  }
}
<div class="skill-container">
  <div class="skill" style="--l:100%">CSS</div>
  <div class="skill" style="--l:70%">HTML</div>
  <div class="skill" style="--l:40%">PHP</div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When the font size is set below 16px on an iPhone, iOS will automatically zoom in to compensate

Currently, I am running tests on my app using an iPhone. Everything seems to be working correctly on the iPad, but when I try clicking on a text field on the iPhone, the view automatically zooms in. It appears that setting the font size to 16px or larger r ...

I am having trouble understanding why dropdown menus with the same content have varying widths

My issue is illustrated in the screenshots below: first-dropdown: second-dropdown: The second dropdown appears slightly wider on the right side. Despite Google Chrome's claim that the widths are the same. HTML code: <div class="row menu"> ...

Using Jquery to run a function once innerHTML has been loaded

After creating an InnerHTML, I'm attempting to execute a simple click function but it's not working. No errors are showing in the console. $(document).ready(function() { var content = "Lorem ipsum dolor sit amet, leo nulla ipsum vivamus, aug ...

What is the best jQuery library to add to my website?

I have included several jQuery scripts on my website for various functionalities such as sticky header, anchors, and animations. I am wondering if it is necessary to include all of them or if I can just include one or two? Here are the jQuery scripts I ha ...

In the event that the identifier changes, initiate a fresh iteration of the loop

My query is returning several lines with the same id. Here's an example of the output: Id Amount Owed Description 552 50.00 Diapers 552 35.00 Lotion 545 23.00 Pen 545 15.00 Notebook Currently, I'm looping in Javascript to display t ...

HTML row with interactive functionality that sends data via POST method to another PHP file

I am currently working on an HTML table where I am displaying data from a database. My goal is to make the entire row clickable so that I can trigger a PHP script using the data from that specific row as variables. However, I am facing a challenge in figur ...

Resolved z-index problem in modal dialog box

My fixed modal has a z-index set to 100, but it seems to be positioned below an element with a z-index of 2 and a position set to relative. I'm not sure why this is happening. You can view the code in this fiddle. Any insights or assistance would be g ...

Prevent Android WebView from attempting to fetch or capture resources such as CSS when using loadData() method

Context To many, this situation might appear to be repetitive. However, I assure you that it is not. My objective is to import html data into a WebView, while being able to intercept user hyperlink requests. During this process, I came across this helpfu ...

Modifying an image with jQuery

Why won't this image change? Below is the relevant HTML, CSS, and jQuery code. HTML <nav id="desktop-nav"> <div class="logo"> <a href="#"></a> </div> <div> ... </div> ... CSS nav#desktop-nav . ...

displaying columns side by side on mobile using bootstrap

I am currently using a horizontal form to display the label on the left side in a row. For mobile devices, I would like the label to take up one row and have the three textboxes remain on the same row. How can I achieve this layout? The textboxes will st ...

Responsive input form causes floating label to be positioned incorrectly

I have crafted a form using Django, Crispy Forms, and Bootstrap. When the screen width is above 575px, everything looks great with fields of appropriate width and floating labels in their correct position. However, when the screen width drops below 575px, ...

iOS iframe remains unscrollable only when switching from portrait to landscape orientation

I have a scrollable iframe set up on iOS like this: <div style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; overflow: scroll; -webkit-overflow-scroll: touch; ..."> <iframe style="height: 600px, ..."> </iframe> </d ...

Make a div with absolute positioning overflow outside of a div with relative positioning that is scrollable

I am facing an issue with two columns positioned side by side. The right column contains a tooltip that overflows to the left on hover. Both columns are scrollable and relatively positioned to allow the tooltip to follow the scroll. However, the tooltip is ...

The HTML body is given a right margin for proper spacing

Within the page, I noticed some margin to the right that extends beyond the body itself, causing a scroll bar to appear at the bottom. However, I'm unable to determine the root cause of this issue. I have provided links to both Codepen and Netlify be ...

Make the child element's height 100% when the parent's width is not set

Having trouble implementing the height:100; property on hover in my portfolio section. I've tried several solutions from similar questions without success. Although it works with media queries, I believe there's a better way to achieve this but I ...

Solely apparent division

Similar Question: How do I disable interaction with content inside a div? Can you display a div without allowing any interaction with the buttons, selects, and information inside? Essentially creating a "blocked" div where everything is visible but no ...

Using JavaScript to display content on the screen

As a newcomer to Javascript, I'm looking for a way to display my variable on the webpage without utilizing <span> or innerHTML. var pProductCode = $('[name=pProductCode]').val(); $('input[id*="_IP/PA_N_"]').each(function(i){ ...

Automatically expand all PrimeNG Accordion panels for easy printing purposes

I've implemented the PrimeNG library's accordion component in my angular project. You can find more information here. In my template, I have some custom css styling for printing the page that looks like this: @media print { .profile-progress ...

Infinite scrolling feature on Kendo UI mobile listview showcasing a single item at a time

Currently, I am utilizing the kendo ui mobile listview and encountering an issue when setting endlessScroll or loadMore to true. The problem arises as the listview only displays the first item in such instances. Upon inspecting with Chrome inspector, I ob ...

Guide to connecting an image in react.js by utilizing the source

I am currently building a website with react.js and I am trying to incorporate an image (logo) as a link to the site. Here is my folder structure: https://i.sstatic.net/VPZo6.png This is the code for my footer logo image: <img className="img-responsi ...