Display a progress bar in Bootstrap with a labeled indicator positioned above the bar and background to the

Is there a way to create a progress bar with a label positioned to the right using Bootstrap v5.2 Progress Bar?

https://i.sstatic.net/gOWie.png

I attempted to use negative margins to achieve this layout but faced issues when the label expanded due to longer text:

https://i.sstatic.net/u0vvo.png

Here is the code I used:

For a single row (ProgressBarWithName.cshtml):

@{
    var valuePercentage = (double)Model.Value / Model.MaxValue * 100;
}

<div class="row m-2" style="height: 50px;">
    <div class="col-3 text-white bg-dark h-100 d-flex justify-content-end">
        <div class="font-allan align-self-center"><b>@Model.Name</b></div>
    </div>
    <div class="col-9 h-100">
        <div class="row gx-0 progress-container h-100">
            <div class="col-11">
                <div class="progress h-100">
                    <div class="progress-bar"
                         role="progressbar" aria-label="Example with label"
                         aria-valuenow="@valuePercentage" aria-valuemin="0" aria-valuemax="@Model.MaxValue"
                         style="width: @valuePercentage%;">
                    </div>
                </div>
            </div>
            <div class="col-1 progress-label align-self-center">
                <b class="@if (valuePercentage > 85){ <text>text-white</text> }">@Model.Value</b>
            </div>
        </div>

    </div>
</div>

For all rows (CustomBarChart.cshtml):

<div>
    @foreach (var item in Model.Items)
    {
        await Html.RenderPartialAsync("ProgressBarWithName", item);
    }
</div>

For testing purposes, I created some hardcoded models:

@{
    var stat = new Stat("Some Stat", 40, 100);
    var stat1 = new Stat("Some Stat1", 84, 100);
    var stat2 = new Stat("Some Stat2", 86, 100);
    var stat3 = new Stat("Some Stat3", 10, 100);
    var stat4 = new Stat("Some Stat4", 100, 100);
    var stats = new Stats(new List<Stat> { stat, stat1, stat2, stat3, stat4 });
    await Html.RenderPartialAsync("CustomBarChart", stats);
}

CSS:

.progress-container{
  margin-right: -60px;
}

.progress-label{
  margin-left: -35px;
  width: 0;
}

The issue I encountered was that the default style of the bootstrap progress bar positions the label in the filled part of the bar. I aim to have the label on the right side of the entire bar, whether in the filled portion or the background. I experimented with using float, but due to the row/col structure I'm using for the grid style, I faced challenges.

Answer №1

Understood:

<div class="row m-2" style="height: 50px;">
    <div class="col-4 text-white bg-gray h-100 d-flex justify-content-end rounded-2 p-2" title="@Model.Name">
        <b class="font-sans align-self-center mh-100 w-100 overflow-hidden"
           style="text-overflow: ellipsis; white-space: nowrap;">
            @Model.Name
        </b>
    </div>
    <div class="col-8 h-100 progress-label-container">
        <div class="progress h-100">
            <div class="progress-bar"
                 role="progressbar" aria-label="Example with label"
                 aria-valuenow="@valuePercentage" aria-valuemin="0" aria-valuemax="@Model.MaxValue"
                 style="width: @valuePercentage%;">
            </div>
        </div>
        <div class="progress-label">
            <b>@Model.Value @Model.Units</b>
        </div>
    </div>
</div>
.progress-label-container{
  position: relative;
}

.progress-label{
  position: absolute;
  right: 7.5%;
  top: 25%;
}

https://i.sstatic.net/7nb3F.png

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

Dynamically Allocating Page Heights

I am tasked with creating an ASP.NET MVC page that will display a template of controls. The controls will be retrieved from an XML file. One issue I am facing is that the number of controls is not fixed, so the page height needs to be dynamic. Our site has ...

What are the pros and cons of embedding background image data into CSS as Base64?

While examining the source code of a greasemonkey userscript, I came across some interesting CSS: .even { background: #fff url(data:image/gif;base64,R0lGODlhBgASALMAAOfn5+rq6uvr6+zs7O7u7vHx8fPz8/b29vj4+P39/f///wAAAAAAAAAAAAAAAAAAACwAAAAABgASAAAIMAAVCBxIsK ...

Arranging data into columns using HTML and CSS

I'm currently working on a CSS solution to organize various elements like buttons and text in a tabular column format. My main challenge lies in controlling the layout effectively, especially since CSS columns are not compatible with IE9 and earlier v ...

Encountering 'Error 405 - Method not Supported' while making an Ajax POST request in ASP.NET Web API

I am currently in the process of developing a straightforward Web API service that captures input data from an HTML form, converts it into JSON format, and forwards it to a Web API. My coding platform is ASP.NET Web API on Visual Studio 2017. To further el ...

Personalize the pagination or other elements of Splide in a Svelte component to suit your needs

I am currently integrating Splide into my Svelte application and facing an issue with the pagination styling. The pagination is appearing on top of my images and is too transparent for my liking. I'm utilizing Svelte Splide to incorporate this library ...

If the progress bar in AngularJS reaches 100%, the "active" class will be automatically removed

How can I remove the bootstrap 'active' class from the progress bar once it reaches 100% to stop the stripes from moving? I tried this, but it's not working: <div class="progress progress-striped active" style="margin-bottom: 0;" n ...

jquery allows you to toggle the visibility of content

There are two divs with a button positioned above them. Initially, only one div, the catalogusOrderBox, is visible. When the button named manualButton is clicked, it should display the manualOrderBox div while hiding the catalogusOrderBox div. The text on ...

An Ajax tabContainer featuring panels and a single button located outside of the tabContainer

Seeking your expertise in developing an Admin Panel using an Ajax tab container with five tapPanels. Each tapPanel consists of a Panel from the standard tool and within each Panel, there is a GridView to display added data. Additionally, there is one Add b ...

Customize your Jquery UI Calendar with Changing Background Images for Each Month!

How can I change the background of jQuery UI datepicker based on the selected month? I have created 12 classes, each representing a month, and the selected month already has the background. I tried using "onChangeMonthYear: function(year, month, inst) { ...

Define the total in a CSS attribute

I am working with multiple divs that have a specific CSS class applied to them. My goal is to manually increase the pixel value in the top property: .class { top: 100px; } div class="class" style="top:+=50px" Is it possible to achieve this functiona ...

What is the best way to verify the font-family using JavaScript?

To verify if the user has installed the font family, we can run a JavaScript function with AngularJS. I am using a Typekit font and have only loaded this service for users who do not have this specific font. ...

Navigating Parent Menus While Submenus are Expanded in React Using Material-UI

My React application includes a dynamic menu component created with Material-UI (@mui) that supports nested menus and submenus. I'm aiming to achieve a specific behavior where users can access other menus (such as parent menus) while keeping a submenu ...

The slider thumb is not showing up properly on Microsoft Edge

Hey there! I'm experiencing an issue with the range slider's thumb display in Microsoft Edge. It appears to be positioned inside the track rather than outside as intended. Take a look at this snapshot for clarification: https://i.stack.imgur.co ...

Update the appearance of a webpage using a custom JavaScript function

My goal is to modify a CSS style whenever a button is clicked. In order to achieve this, I have implemented a JavaScript function that creates a new class for CSS along with several other functionalities. Here's how the JS function currently appears: ...

Enhancing the appearance of the active menu item with HTML/CSS/JS/PHP

Here's the HTML code I have: <ul class="navigation"> <li><a href="index.php">Home</a></li> <li><a href="index.php?content=about">About us</a></li> </ul> As you can see, the content of the " ...

Tips for implementing bslib package pop up window within a table displayed using rhandsontable in R Shiny

I am trying to implement the template from Laurent's answer on R Shiny - Popup window when hovering over icon in my code below, which involves integrating a popup window into a rhandsontable table. My goal is to display the popup window as depicted in ...

What methods can I use to create a navigation bar similar to this design?

Lately, I've noticed a growing trend in navigation bars that are designed to resemble waves. Wavey Navbar I'm eager to incorporate a similar navbar into my website, but I'm struggling to figure out how to do it. I'm working with react ...

Expand the object by clicking on it, causing the surrounding objects to move outside the viewport instead of appearing below

I'm facing a challenge with 3 squares inside one container, each occupying 33.33% of the viewport and placed next to each other. When I click on one square, it should expand to full viewport width and reveal hidden content, while the other two remain ...

Using C# to Deserialize JSON String into Class Properties and Beyond

I am attempting to process JSON results retrieved from an API call in order to make it easier for me to manipulate it for a WPF UI. The [] brackets at the start and end of the JSON data are highlighted because that is how the data returns after the initia ...

Troubleshooting text-rendering CSS issues in Netbeans

body, html{ font-family: 'Lato', sans-serif; text-rendering: optimizeLegibility !important; -webkit-font-smoothing: antialiased !important; color: #5a5a5a; } there was a CSS parsing error due to an unrecognized property ...