Display a gradient-colored Bootstrap progress bar that visually represents the active width

I am trying to create a Bootstrap progress bar with a gradient color fill (such as red to green). Currently, my CSS code looks like this:

.progress-lf {
    position: relative;
    height: 31px;
    background-color: rgba(51, 51, 51, 0.4)
}

.progress-lf span {
    position: absolute;
    display: block;
    font-weight: bold;
    color: #d2d2d2;
    width: 100%;
    top:6px;
}

.progress-lf .gradient {
    background-color: transparent;
    background-image: -ms-linear-gradient(left, #E34747 0%, #5FB365 100%);
    background-image: -moz-linear-gradient(left, #E34747 0%, #5FB365 100%);
    background-image: -o-linear-gradient(left, #E34747 0%, #5FB365 100%);
    background-image: -webkit-gradient(linear, left top, right top, color-stop(0, #E34747), color-stop(100, #5FB365));
    background-image: -webkit-linear-gradient(left, #E34747 0%, #5FB365 100%);
    background-image: linear-gradient(to right, #E34747 0%, #5FB365 100%);
}

Here is the corresponding HTML:

<div class="progress progress-lf">
            <div class="progress-bar gradient" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo mt_rand(0,100);?>%;">
                    <span>60% Complete</span>
            </div>
    </div>

While this displays the gradient, it shows the full color spectrum for the entire length of the progress bar. I am looking for a way to only show the relevant percentage of the color spectrum (e.g., only 60% of the gradient for 60%). Any suggestions on how to achieve this? I would prefer a solution using pure CSS, but jQuery is also an option.

Answer №1

To dynamically adjust the 'amount' and update the progress bar, utilizing jQuery (or vanilla js) is recommended.

By using the data attribute for setting the value of the progress bar and text in one place, you can easily make changes.


All you need to do is modify the

data-amount 

attribute to a value ranging from 0 to 100%.


Example

 $(document).ready(function () {
    var dataval = parseInt($('.progress').attr("data-amount"));
    if (dataval < 100) {
        $('.progress .amount').css("width", 100 - dataval + "%");
    }

  /*DEMO PURPOSES ONLY*/
    $('#increase').click(function () {
        modifyProgressVal(1);
    });
    $('#decrease').click(function () {
        modifyProgressVal(-1);
    });
    function modifyProgressVal(type) {
        dataval = parseInt($('.progress').attr("data-amount"));
        if (type == 1) dataval = Math.min(100,dataval + 10)
        else if (type == -1) dataval = Math.max(0,dataval - 10);
        $('.progress .amount').css("width", 100 - dataval + "%");
        $('.progress').attr("data-amount", dataval);
    }
});
.progress {
  position: relative;
  height: 31px;
  background: rgb(255, 0, 0);
  background: -moz-linear-gradient(left, rgba(255, 0, 0, 1) 0%, rgba(0, 255, 0, 1) 100%);
  background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 0, 0, 1)), color-stop(100%, rgba(0, 255, 0, 1)));
  background: -webkit-linear-gradient(left, rgba(255, 0, 0, 1) 0%, rgba(0, 255, 0, 1) 100%);
  background: -o-linear-gradient(left, rgba(255, 0, 0, 1) 0%, rgba(0, 255, 0, 1) 100%);
  background: -ms-linear-gradient(left, rgba(255, 0, 0, 1) 0%, rgba(0, 255, 0, 1) 100%);
  background: linear-gradient(to right, rgba(255, 0, 0, 1) 0%, rgba(0, 255, 0, 1) 100%);
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#ff0000', endColorstr='#00ff00', GradientType=1);
}
.amount {
  position: absolute;
  top: 0;
  right: 0;
  height: 100%;
  transition: all 0.8s;
  background: gray;
  width: 0;
}
.progress:before {
  content: attr(data-amount)"% Complete";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  text-align: center;
  line-height: 31px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="progress" data-amount="80">
  <div class="amount"></div>
</div>


<!--FOR DEMO PURPOSES ONLY-->

<button id="increase">Increase by 10</button>
<button id="decrease">Decrease by 10</button>

This setup utilizes only two elements for better performance.

NOTE

The extensive use of jQuery in this example is primarily for demonstration purposes and not essential for actual implementation.

Answer №2

Update the element containing a gradient from progress-bar to progress.

To hide it, apply a white box-shadow to progress-bar.

Adjustments needed in the style:

.progress {
  background-image: linear-gradient(to right, #FFF, #00F);
}

.progress-bar {
  box-shadow: 0px 0px 0px 2000px white;   /* Use white or any other color you prefer */
  background-image: none !important;
  background-color: transparent !important;
}

The gradient within .progress will show through the transparent background of progress-bar.

However, outside the progress-bar area, it will be concealed by the shadow effect.

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

Odd behavior observed when clicking on the link

On the initial click with an anchor tag, I am experiencing different behavior compared to subsequent clicks. When the href value is set to "", no HTTP GET request is made on the first click. I cannot determine why the first click would behave differently t ...

Can data sent to php via ajax be manipulated?

To restrict the number of characters a user can input in a textarea, I have implemented a character counter. // part of tinymce setup: function (ed) { ed.on('keyup', function (e) { var maxchars = <?php echo $max_chars_allowed; ?>; ...

Automatically save user input in form fields with the help of jQuery and AJAX technology

I'm working on a form that has various input fields, and I need the data entered by the user to be automatically stored in the database every minute. After the user submits the request, it will be processed in a struts file where database interactions ...

What is the best way to display the JQuery ThickBox plugin content on a webpage?

After setting up the ThickBox Plugin and making an AJAX request to retrieve content, I noticed that when I click the print link, it prints the entire page visible through the Thickbox. How can I modify this to only print the specific content being displaye ...

concise stylus CSS selector

Is there a way to condense these stylus selectors for a cleaner look? #map > div.mapboxgl-control-container > div.mapboxgl-ctrl-top-left > div:nth-child(1) > button #map > div.mapboxgl-control-container > div.mapboxgl-ctrl-top-left > ...

Encountering an issue with the message "SyntaxError: Unexpected token < in django-jquery-file

I am currently working on implementing django-jquery-fileupload into my project. https://github.com/sigurdga/django-jquery-file-upload However, I encounter an "Error SyntaxError: Unexpected token < " when attempting to click the "start" upload button. ...

Switching between different sections of a webpage

Seeking assistance with implementing a transition effect between sections on a single-page application. All sections are located on the same page, but only one section is displayed at a time. When an event occurs, the display property of the requested sect ...

Improving loading time for pages requiring jQuery to render

Currently, I am utilizing the Google PageSpeed service to enhance the loading time of my website. It is hosted on GAE/P, but that detail may not be crucial in this context. There are several resources my page relies on: Bootstrap (css and js) jQuery (js ...

Getting an undefined result when trying to load an external script using getScript

I attempted to include an external JavaScript file in my HTML using the $.getScript method, but it is returning as undefined. Even when trying with the URL https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js, I encountered the same issue of ...

The usage of $('').switchClass in IE8 may cause an error when the switched class includes a color property

I have the following unique css classes .swap-format{ background-color: green; } .swap-format1{ background-color: orange; } .swap-format2{ color: purple; } Using these classes, I want to create an animation on the given div <div id="swap-clas ...

Determining parent height through the positioning of children

When it comes to CSS, it's truly fascinating until you hit a roadblock and the "aha moment" seems elusive. Today, I'm struggling with adjusting the height of my parent div that contains a relatively positioned element. The issue arises from the ...

The _destroy method of the jQuery widget factory is designed to efficiently

I need help creating a timer widget that can start with a specific number of seconds and be reset at any time by initializing it again with a new set of seconds. I have managed to create the timer and make it count down, but now I am facing an issue. My g ...

What steps do I need to take in order to accomplish this specific CSS

Hey there, I have a question that may seem silly, but I am struggling to find a solution on my own. Unfortunately, my CSS skills are not the best :( I'm trying to achieve the following layout where the text should wrap at the end of the container (di ...

Is there a way to make a div appear on the following line without relying on the clear:both property?

I am currently tackling a small portion of a complicated webpage where there are divs placed on the right side that I do not have direct control over. Therefore, when I attempt to use clear:both (as advised here) to push my div to the next line, it ends up ...

Attempting to design a unique CSS ribbon but hitting a roadblock with getting the perfect curve just right

I have attempted to create a CSS shape (ribbon) using border radius, but I am struggling to achieve the desired curve. The curve I manage to create does not exactly match the curve of the div in the image. background: #008712; width: 89px; height: 22p ...

Place the image in the middle of a div

Struggling to horizontally center an image in a div, but for some reason it's just not working. I've centered images many times before, so why is it different this time? Here's what I've attempted... CSS #cabeca{ position: relative; f ...

The functionality of sliding an HTML form using jQuery does not function properly when specific controls are defined

I currently have an html form that includes a <nav> tag. Here is the structure of the form: <section class="dropinput"> <form id="add_task_form" class="addTaskForm" action=""> <input type="text" id="add_task_name" class= ...

Issues arise when trying to integrate Bootstrap modal with bootcards

I am currently implementing a modal with bootstrap: <div class="modal" id="contact-update-view"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-head ...

Prevent the colorpicker feature for the <input type="color"> element on Google Chrome

When using Google Chrome, the <input type="color"> element creates an input field with a color bar inside it. By default, it opens a colorpicker that may vary in appearance depending on the operating system (mine has a Windows theme). I have implemen ...

Toggle the visibility of a table row depending on the selected value of a Radio button

I have a jQuery script that I am working on, which toggles the visibility of table rows based on the selection of radio buttons. The current code functions properly when a radio button is clicked, but I would like to enhance it to automatically show or hid ...