Animating width using CSS3 with percentage values

https://i.sstatic.net/SGEQZ.gif

I've implemented CSS3 animations to expand the bars, but I'm facing an issue where the percentage needs to be embedded in the @keyframes-code. The custom width is currently inserted as inline CSS in the HTML.

Is there a method to animate based on the amount specified in the inline CSS width, instead of what's included in the @keyframe?

#dataanalysis td figure {
  animation: aw 2s normal;
}

@keyframes aw {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}
#dataanalysis {
  width: 100%;
  margin-top: 3rem;
  background: #26272b;
  color: white;
  border-collapse: collapse;
  font-size: .7rem;
  font-weight: 300;
  -webkit-font-smoothing: antialiased;
}
#dataanalysis td {
  border: solid 1px black;
  padding: .5rem;
  word-wrap: break-word;
  height: 25px;
}
#dataanalysis td:first-child {
  width: 140px;
  padding-left: .5rem;
}
#dataanalysis td:nth-child(2n) {
  border-right: none;
}
#dataanalysis td:last-child {
  text-align: right;
  width: 25px;
  border-left: none;
}
#dataanalysis td figure {
  background: linear-gradient(to right, #f24f37 0%, #cd2134 100%);
  height: .8rem;
  margin: 0 0 0 .5rem;
  border-radius: 0.3rem;
  border: 1px solid black;
}
#dataanalysis td img {
  width: .8rem;
  padding-left: .8rem;
}
<table id="dataanalysis">
            <tbody>
              <tr>
                <td>Acousticness
                  <a href="#" data-tooltip="A confidence measure from 0.0 to 1.0 of whether the track is acoustic. 1.0 represents high confidence the track is acoustic."><img src="/assets/img/info.svg" alt="tip"></a>
                </td>
                <td><figure style="width:87%"></figure></td><td>87%</td>
              </tr>
              <tr>
                <td>Danceability
                  <a href="#" data-tooltip="Danceability describes how suitable a track is for dancing based on a combination of musical elements including tempo, rhythm stability, beat strength, and overall regularity. A value of 0.0 is least danceable and 1.0 is most danceable."><img src="/assets/img/info.svg" alt="tip"></a>
                </td>
                <td><figure style="width:15%"></figure></td><td>15%</td>
              </tr>
              <tr>
                <td>Energy
                  <a href="#" data-tooltip="Energy is a measure from 0.0 to 1.0 and represents a perceptual measure of intensity and activity. Typically, energetic tracks feel fast, loud, and noisy. For example, death metal has high energy, while a Bach prelude scores low on the scale. Perceptual features contributing to this attribute include dynamic range, perceived loudness, timbre, onset rate, and general entropy."><img src="/assets/img/info.svg" alt="tip"></a>
                </td>
                <td><figure style="width:49%"></figure></td><td>49%</td>
              </tr>
              <tr>
                <td>Instrumentalness
                  <a href="#" data-tooltip="Predicts whether a track contains no vocals. "Ooh" and "aah" sounds are treated as instrumental in this context. Rap or spoken word tracks are clearly "vocal". The closer the instrumentalness value is to 1.0, the greater likelihood the track contains no vocal content. Values above 0.5 are intended to represent instrumental tracks, but confidence is higher as the value approaches 1.0."><img src="/assets/img/info.svg" alt="tip"></a>
                </td>
                <td><figure style="width:32%"></figure></td><td>32%</td>
              </tr>
              <tr>
                <td>Liveness
                  <a href="#" data-tooltip="Detects the presence of an audience in the recording. Higher liveness values represent an increased probability that the track was performed live. A value above 0.8 provides strong likelihood that the track is live."><img src="/assets/img/info.svg" alt="tip"></a>
                </td>
                <td><figure style="width:65%"></figure></td><td>65%</td>
              </tr>
              <tr>
                <td>speechiness
                  <a href="#" data-tooltip="Speechiness detects the presence of spoken words in a track. The more exclusively speech-like the recording (e.g. talk show, audio book, poetry), the closer to 1.0 the attribute value. Values above 0.66 describe tracks that are probably made entirely of spoken words. Values between 0.33 and 0.66 describe tracks that may contain both music and speech, either in sections or layered, including such cases as rap music. Values below 0.33 most likely represent music and other non-speech-like tracks."><img src="/assets/img/info.svg" alt="tip"></a>
                </td>
                <td><figure style="width:11%"></figure></td><td>11%</td>
              </tr>
              <tr>
                <td>Valence / Happiness
                  <a href="#" data-tooltip="A measure from 0.0 to 1.0 describing the musical positiveness conveyed by a track. Tracks with high valence sound more positive (e.g. happy, cheerful, euphoric), while tracks with low valence sound more negative (e.g. sad, depressed, angry)."><img src="/assets/img/info.svg" alt="tip"></a>
                </td>
                <td><figure style="width:91%"></figure></td><td>91%</td>
            </tbody>
          </table>

Answer №1

To modify the animation, simply eliminate the 100% segment from the @keyframes. The final phase of the animation will now be based on the width specified in the inline style attribute:

#dataanalysis td figure {
  animation: aw 2s normal;
}
@keyframes aw {
  0% {
    width: 0%;
  }
}
#dataanalysis {
  width: 100%;
  margin-top: 3rem;
  background: #26272b;
  color: white;
  border-collapse: collapse;
  font-size: .7rem;
  font-weight: 300;
  -webkit-font-smoothing: antialiased;
}
#dataanalysis td {
  border: solid 1px black;
  padding: .5rem;
  word-wrap: break-word;
  height: 25px;
}
#dataanalysis td:first-child {
  width: 140px;
  padding-left: .5rem;
}
/* The following rule is used to hide broken images and is not relevant to the answer or requirement */

#dataanalysis td a img {
  display: none;
}
#dataanalysis td:nth-child(2n) {
  border-right: none;
}
#dataanalysis td:last-child {
  text-align: right;
  width: 25px;
  border-left: none;
}
#dataanalysis td figure {
  background: linear-gradient(to right, #f24f37 0%, #cd2134 100%);
  height: .8rem;
  margin: 0 0 0 .5rem;
  border-radius: 0.3rem;
  border: 1px solid black;
}
#dataanalysis td img {
  width: .8rem;
  padding-left: .8rem;
}
<table id="dataanalysis">
  <tbody>
    <tr>
      <td>Acousticness
        <a href="#" data-tooltip="A confidence measure from 0.0 to 1.0 of whether the track is acoustic. 1.0 represents high confidence the track is acoustic.">
          <img src="/assets/img/info.svg" alt="tip">
        </a>
      </td>
      <td>
        <figure style="width:87%"></figure>
      </td>
      <td>87%</td>
    </tr>
    <tr>
      <td>Danceability
        <a href="#" data-tooltip="Danceability describes how suitable a track is for dancing based on a combination of musical elements including tempo, rhythm stability, beat strength, and overall regularity. A value of 0.0 is least danceable and 1.0 is most danceable.">
          <img src="/assets/img/info.svg" alt="tip">
        </a>
      </td>
      <td>
        <figure style="width:15%"></figure>
      </td>
      <td>15%</td>
    </tr>
    <tr>
      <td>Energy
        <a href="#" data-tooltip="Energy is a measure from 0.0 to 1.0 and represents a perceptual measure of intensity and activity. Typically, energetic tracks feel fast, loud, and noisy. For example, death metal has high energy, while a Bach prelude scores low on the scale. Perceptual features contributing to this attribute include dynamic range, perceived loudness, timbre, onset rate, and general entropy.">
          <img src="/assets/img/info.svg" alt="tip">
        </a>
      </td>
      <td>
        <figure style="width:49%"></figure>
      </td>
      <td>49%</td>
    </tr>
    <tr>
      <td>Instrumentalness
        <a href="#" data-tooltip="Predicts whether a track contains no vocals. " Ooh " and "aah " sounds are treated as instrumental in this context. Rap or spoken word tracks are clearly "vocal
        ". The closer the instrumentalness value is to 1.0, the greater likelihood the track contains no vocal content. Values above 0.5 are intended to represent instrumental tracks, but confidence is higher as the value approaches 1.0.">
          <img src="/assets/img/info.svg" alt="tip">
        </a>
      </td>
      <td>
        <figure style="width:32%"></figure>
      </td>
      <td>32%</td>
    </tr>
    <tr>
      <td>Liveness
        <a href="#" data-tooltip="Detects the presence of an audience in the recording. Higher liveness values represent an increased probability that the track was performed live. A value above 0.8 provides strong likelihood that the track is live.">
          <img src="/assets/img/info.svg" alt="tip">
        </a>
      </td>
      <td>
        <figure style="width:65%"></figure>
      </td>
      <td>65%</td>
    </tr>
    <tr>
      <td>speechiness
        <a href="#" data-tooltip="Speechiness detects the presence of spoken words in a track. The more exclusively speech-like the recording (e.g. talk show, audio book, poetry), the closer to 1.0 the attribute value. Values above 0.66 describe tracks that are probably made entirely of spoken words. Values between 0.33 and 0.66 describe tracks that may contain both music and speech, either in sections or layered, including such cases as rap music. Values below 0.33 most likely represent music and other non-speech-like tracks.">
          <img src="/assets/img/info.svg" alt="tip">
        </a>
      </td>
      <td>
        <figure style="width:11%"></figure>
      </td>
      <td>11%</td>
    </tr>
    <tr>
      <td>Valence / Happiness
        <a href="#" data-tooltip="A measure from 0.0 to 1.0 describing the musical positiveness conveyed by a track. Tracks with high valence sound more positive (e.g. happy, cheerful, euphoric), while tracks with low valence sound more negative (e.g. sad, depressed, angry).">
          <img src="/assets/img/info.svg" alt="tip">
        </a>
      </td>
      <td>
        <figure style="width:91%"></figure>
      </td>
      <td>91%</td>
  </tbody>
</table>

JS Fiddle demo.

Answer №2

It is recommended to opt for the transition property rather than a keyframe. Here is an example:

#dataanalysis td figure {
    transition: width 2s normal;
}

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

Displaying cards horizontally on desktop and vertically on mobile using CSS

Context and Issue I am currently working on this specific page, with a focus on the "Top Artists" section. Desired Outcome: When viewed from a laptop, I aim for the cards to be displayed as vertical rectangles, where the ranking is at the top, followe ...

Highlighted Navigation Options

When visiting , take note of the top navigation bar. The background color on this page is white, but as you navigate to other pages, the tab changes to a different color. How can you modify this behavior and change the class? Is PHP or jQuery necessary f ...

Centering multiple floated divs using bootstrap

Looking for some help with a bootstrap panel design. I am trying to align a heading title to the left and a select element along with a date range input to the right of the panel heading, vertically aligned in the middle. While I have made progress on this ...

Botched HTML and CSS layout

Looking at someone else's project and trying to improve the design without modifying the CSS. Struggling to figure out how to rearrange it in HTML. Any suggestions? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

CSS3 family tree tutorial: Incorporating a wife into the design

I came across a fascinating tutorial about creating a family tree using CSS3 only. However, I'm struggling to understand how to depict a marriage. To explain further: What the code currently accomplishes is this: what i aim to include is this: Alth ...

What is the reason for not being able to align breadcrumb text to the right

This section contains HTML code and CSS for a breadcrumb container. The issue is that the breadcrumb is currently displayed on the left side. How can the breadcrumb be right-aligned? .breadcrumb-container { font-family: "Work Sans", sans-serif; ...

I am having trouble getting the border-radius to display properly in my email table

I'm in the process of designing a responsive email template. For the white content area, I've applied border-radius: 5px;. While it's working fine on the bottom corners of the table, the top corners don't seem to display the border-rad ...

Tips for adjusting the font size within the MUI TextField Label

I'm looking to adjust the label fontSize in the material ui TextField component. I've discovered the InputLabelProps property which has been helpful, but as shown in the image below, the label appears too cramped when focused. https://i.sstatic. ...

Increase the font size of a div using CSS to make it appear larger

Utilizing a WYSIWYG-editor, I am creating content that I want to display with double font-size, without directly altering the HTML code. Here is an example of the HTML structure: <div id="contents"> <p style="text-align:center"> <spa ...

Looking to turn off animation for a WordPress theme element?

Is there a code that I can insert into the style.css file to deactivate the theme animations on my WordPress page? As users scroll down, each element either drops in or slides in. I recently purchased the WP Alchemy theme from , but the animation speed is ...

Error encountered while generating PDF using xhtml2pdf Pisa.CreatePDF() due to CSS parsing issue

Currently, I am referencing the xhtml2pdf instructions. I opted to utilize one of the provided sample html files and saved it as test.html: <html> <head> <style> @page { size: a4 portrait; @frame header_frame { ...

In React development, a div (button) is visible, but in the production build, it becomes hidden from

I have been working on a website for my job, and I have added a "Gallery" button on top of two slideshows. Everything works perfectly fine on the development build, but when it comes to the production build, the button doesn't show up for some reason. ...

The panel element is being encroached upon by a sidebar

I'm currently developing a basic chat application and I'm in need of a collapsible sidebar for users to select different chat rooms. Unfortunately, the current fixed sidebar is overlapping the panel element where the chat conversations are suppos ...

Right-align the span and vertically align the header to the left

Seeking guidance on aligning a span to the right of a div, where the span is nested within a button. Although I have successfully achieved the above, I am encountering difficulties in vertically aligning the header while also keeping it to the left. .s ...

Minor Chrome compatibility problems with CSS alignment

As someone who is new to stackoverflow, I've always found it to be a valuable resource for answers. I've had success building HTML 5 banner ads using GSAP (Greensock Animation Platform) in the past, but now I'm facing a CSS alignment issue t ...

Troubleshooting CSS Animation Failure in Internet Explorer 11

My mouse over / mouse out animation works perfectly in Firefox and Chrome, but it's not functioning in IE. Can anyone suggest why this might be happening when it was working fine before? var actual = 1; var over = 0; var over2 = 0; function scrol ...

Tips on transitioning from Modal1 to Modal2 in Ionic after a successful submit button press?

My survey app requires users to fill out a form and click the Submit Feedback button. Upon successful submission, a message saying "Feedback submitted successfully" should be displayed in Modal2, which is inside another modal named (Modal1). However, being ...

Mobile devices consistently experiencing issues with JavaScript generated elements overlapping

I'm currently in the process of creating a quiz based on a tutorial I found at https://www.sitepoint.com/simple-javascript-quiz/. While I followed the tutorial closely and integrated Bootstrap, I am encountering an issue specifically with mobile devic ...

I must remove the thumb from the input range control

I am looking for a way to remove the thumb from the progress bar in my music player. I have tried various methods without success, and I simply want the progress bar to move forward with color change as it advances based on the Progress variable. For refe ...

Display a fresh <p> tag when the condition in the if-statement is met

If you are looking for a questionnaire, check out this one. Now, I am interested in creating if-statements using HTML/CSS/jQuery to achieve the following scenario: Initially, only Question 1 will be visible. When the input matches a certain value like X, ...