What is the best way to present progress bar numbers in Bootstrap beneath the bar?

I have successfully implemented a progress bar using Bootstrap.

Now, I am looking to display numerical values below the progress bar, similar to this:

https://i.sstatic.net/3QG8d.png

Is there a way to achieve this without utilizing JavaScript graphing libraries?

My main issue is figuring out how to show the numbers below the divs, without the need for color styling or manual calculation.

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f4969b9b808780869584b4c0dac2dac6">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">

<div class="container pt-4">
  <div class="progress">
    <div class="progress-bar" role="progressbar" style="width: 15%;">
      15
    </div>
    <div class="progress-bar bg-success" role="progressbar" style="width: 30%;">
      45
    </div>
    <div class="progress-bar bg-info" role="progressbar" style="width: 20%;">
      65
    </div>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b8dad7d7cccbcccad9c8f88c968e968a">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>

Answer №1

To create a solution, you can insert a new div element below yours and utilize JavaScript for the functionality:

var elements = document.getElementsByClassName('progress-bar');
var count = 0;
var values = document.getElementById('values');
var value = document.createElement('div');
value.innerHTML = "0";
value.style.width = (parseInt(elements[0].style.width.split('%')[0]) - 1.6) + "%"
values.appendChild(value);

for (var i = 0; i < elements.length; i++) {
  count += parseInt(elements[i].style.width.split('%')[0]);

  var value = document.createElement('div');
  value.innerHTML = count;

  if (i < elements.length - 1)
    value.style.width = parseInt(elements[i + 1].style.width.split('%')[0]) + "%"
  else
    value.style.width = (100 - count - 4) + "%";
  values.appendChild(value);
}
var value = document.createElement('div');
value.innerHTML = "100";
values.appendChild(value);
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="197b76766d6a6d6b7869592c372b">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous" />

<div class="container">
  <div class="progress">
    <div class="progress-bar" role="progressbar" style="width: 15%;"></div>
    <div class="progress-bar bg-success" role="progressbar" style="width: 30%;"></div>
    <div class="progress-bar bg-info" role="progressbar" style="width: 20%;"></div>
  </div>
  <div id="values" style="display: flex; flex-flow: row wrap;"></div>
</div>

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e3c31312a2d2a2c3f2e1e6b706c706d">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>

Answer №2

To add some visual flair to your CSS, consider incorporating pseudo-elements. I've enclosed the numerical values in spans for precise positioning. By assigning a custom class to the structure, it's possible to overwrite Bootstrap's styles without resorting to !important, simplifying future modifications and preventing the need to restyle all progress bars.

It's worth mentioning that Bootstrap offers classes for positioning, overflow, and border-radius, but I've opted to define these attributes in the CSS for better clarity. However, I have applied border classes to the outer element for consistency.

.progress.labels-out,
.progress.labels-out .progress-bar {
  position: relative; /* enables child element positioning */
  overflow: visible; /* ensures child elements are visible */
  border-radius: 0;
}

.progress.labels-out .progress-bar span,
.progress.labels-out::before,
.progress.labels-out::after {
  position: absolute;
  top: calc(100% + 5px); /* shifts downwards by parent height plus a margin */
  left: 100%;
  transform: translateX(-50%); /* centers by shifting left half of its width */
  color: #000;
  font-weight: bold;
}

.progress.labels-out::before {
  content: '100';
}

.progress.labels-out::after {
  content: '0';
  left: 0;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9aba6a6bdbabdbba8b989fce7fbe7fa">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">

<div class="container pt-4">
  <div class="progress labels-out border border-2">
    <div class="progress-bar" role="progressbar" style="width: 15%;">
      <span>15</span>
    </div>
    <div class="progress-bar bg-success" role="progressbar" style="width: 30%;">
      <span>45</span>
    </div>
    <div class="progress-bar bg-info" role="progressbar" style="width: 20%;">
      <span>65</span>
    </div>
  </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

Sending formdata containing a file and variables via $.ajax

I've been working on a jquery file upload project based on a tutorial I found here: . The tutorial is great and the functionality works perfectly. However, I'm looking to add more control and security measures for user image uploads, such as inco ...

To use the ModuleWithProviders<T> in the angular-autofocus-fix package, you must provide 1 type argument

Upon successful installation of angular-autofocus-fix I have imported the AutofocusModule However, upon running the Angular project, I encountered the following error: ERROR in node_modules/angular-autofocus-fix/index.d.ts:4:23 - error TS2314: Generic ty ...

Incorporating a new row in JQuery Datatable using an mdata array

I am currently using a datatable that retrieves its data through mData. var processURL="path" $.ajax( { type : "GET", url : processURL, cache : false, dataType : "json", success ...

The response parser in Angular 7 is failing to function correctly

Hey, I recently updated my Angular from version 4.4 to the latest 7 and after encountering several errors, I was able to get my service up and running. However, I'm facing an issue with my output parser function which is supposed to parse the login re ...

Steps for updating images and text on a live webpage after launching the web application

I added an image to a jsp file. The image, named kid.jpg, is downloaded and stored in the /resources/images folder as shown in the code snippet below. <li style="background-image: url('resources/images/kid.jpg');"> Now that my web app is ...

React function failing to utilize the latest state

I'm facing an issue with my handleKeyUp function where it doesn't seem to recognize the updated state value for playingTrackInd. Even though I update the state using setPlayingTrackInd, the function always reads playingTrackInd as -1. It's p ...

React - Page Loads with Empty Query Parameters

Seeking assistance with navigation logic in React, I'm encountering an issue that requires some guidance. I have developed a front-end web app using TypeScript, React, and Ionic Framework V5. The app features a standard search box that redirects use ...

storage location for data in JSON format

When using content type : "application/x-www-form-urlencoded", the result is stored in Request.Form in Asp MVC. However, for "application/json", the storage location cannot be found. Below is the code that is being used: AJAX part // reading form da ...

Building a loading spinner component in a react-native project

I have successfully implemented a loading spinner that is currently being used in various components of my React project. However, as I am now working on a react-native version of the application, I am faced with the challenge of recreating this loading sp ...

The template in AngularJS route fails to display

I am currently facing an issue with loading multiple pages using AngularJS $routeProvider and two controllers. Whenever I click on a link from the first partial to go to the second one, the second partial fails to render properly, displaying only template ...

Using focusout and clicking buttons do not effectively interact with child elements

I have a series of div elements, each containing a button. When I click on a button, text is displayed, and when I click away, the information hides with a focus-out function. If I have one button open and then want to click on another parent button, it wo ...

Stylist in Visual Studio Code for React applications

Whenever I try to save or format my React code using Ctrl + Shift + F, the formatting of the code below seems unusual. Is there a way to fix this issue? The original code is as follows: import logo from './logo.svg'; import './App.css&apos ...

React component fails to re-render after state change

For the past two days, I've been struggling with this error and can't seem to fix it! I'm currently working on creating a weather app in React which utilizes the API. The app features a Bootstrap Navbar with a search option that allows user ...

The scroll bar is visible on the scroll box, but unfortunately it is not functional in Internet Explorer 8

http://example.com/code <div style="width:625px;height:220px;overflow-y:hidden;overflow-x:scroll;z-index:10;"> <table width="1000" height="114" border="1"> <tr> <td width ...

No data found on Angular TypeScript page with an empty array

Incorporated a function called 'getQuestionsWithInterviewId' to populate my 'Questions' string, but it appears empty when I attempt to call it within the ngOnInit and ngAfterContentInit methods and log the result in the console. import ...

Creating a dynamic table with columns of fixed width in Angular on the fly

I am struggling to create a data table with fixed column widths (20% each). I have incorporated div elements in my table structure to enable dynamic row creation using Angular, but this has caused some design issues. My goal is for all rows to occupy 100% ...

Tips for handling arguments in functional components within React applications

Is it possible to pass arguments to a function in a functional component without creating the function directly in JSX? I've heard that creating functions in JSX is not recommended, so what's a better way to achieve this? function MyComponent(pr ...

What could be causing the child view to not display the AJAX result?

An AJAX call is being made in the following manner: @Ajax.ActionLink("My Schedule", "GetSchedule", "Schedule", new { selectedDate = strToday}, new AjaxOptions { UpdateTargetId = "theTimes", InsertionMode = InsertionMode.Replace, HttpMethod = "GET" }) Th ...

my initial attempt at using Firebase cloud functions

I'm currently attempting to create my first Firebase Cloud Function. My goal is to take the value of the 'name' field from the 'amr' document and add it to the 'ahmed' document under a new field called 'newName' ...

Using various Content-Types within a single path with Serverless Next JS

Is it possible to achieve the following scenario using serverless on Vercel with Next JS? I have a route /product/[id].tsx. When a request is sent with the header Accept: text/html, I want it to follow the normal Next JS flow and display a React page. How ...