Adjust the background color of a div dynamically depending on a specified percentage, similar to a progress bar

I have a div containing some text along with a percentage value (e.g. 78%). I would like to represent this percentage as the background color of the entire div, not just a portion of it.

<div class="my-div">
    <div class="percentage-colored" style="width: 78%"></div>
    My text
</div>

Answer №1

Here is the solution I came up with:

<div class="progressbar-wrapper">
    Some sample text
    <div class="progressbar" style="width: 78%"></div>
</div>

.progressbar-wrapper {
    position: relative;
    z-index: 1;
}

.progressbar {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    background-color: #dcf3dd;
    z-index: -1;
}

Answer №2

<!DOCTYPE html>
<html>
<head>
<title>Web Page Title</title>
</head>
<body>

 <div style="height:20px; ">
        Some content here
</div>
<div style="height:50px;width:500px;background-color:grey;">
    <div class="???" style="width: 78%;background-color: brown;height: inherit;">This is a loading bar</div>
</div>

</body>
</html>

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

Is there a way to determine if a line has been automatically wrapped within a textarea element?

Is it possible to detect when text is wrapped onto the next line in a textarea after becoming wider than the area? ...

An alternative to Socket.io tailored for up-to-date web browsers

Are there any specialized versions of Socket.io or similar libraries that cater to modern browsers exclusively, utilizing Websockets without the need for legacy browser support and outdated code? ...

Using Bootstrap to create a fixed footer in Laravel

I have a Laravel project with Bootstrap added as the CSS. I have tried everything to make my sticky footer work, but when the page is longer than the window, it remains at the bottom of the window when scrolled up. Here is my main: <html> <head& ...

Error encountered while trying to access "toast" element using "$(...)"

Currently, I'm in the process of developing a web app using Django Channels and WebSockets. My goal is to dynamically generate Bootstrap toasts (https://getbootstrap.com/docs/4.2/components/toasts/) when a WebSocket connection is established. However, ...

Is it possible to create a stylish CSS shadow effect?

As I work on converting a PSD design into HTML/CSS, I've encountered an intricate shadow effect created by the designer. I am struggling to replicate it using CSS. Is there a way to achieve this? If yes, then how can it be done? The shadow style is s ...

The Bootstrap Navbar appears hidden beneath other elements on mobile devices

While using bootstrap to style my header contents, I encountered a strange issue. The navbar that appears after clicking on the hamburger menu is displaying behind all the components. Even though I've set the z-index to the maximum value, it still doe ...

Issue with media screen css not working on the fixed menu

My website has a fixed menu. However, I noticed that if the browser size is small, the menu shifts left by -130px instead of -20px. Can anyone help me understand why this might be happening? #nav { left: -20px; list-style: none; margin: 0; ...

Optimizing images for various screen sizes with responsive design

As I delve into using responsive images, a question has arisen. On my website, there is an icon that comes in two sizes: a small version measuring 74 pixels wide and a larger version at 94 pixels wide. <img alt="Section Gino" style="background-color: ...

Having trouble decoding audio data in JavaScript Web Audio?

I'm attempting to utilize the Web Audio API in JavaScript to upload a sound into a buffer and play it. However, I am encountering an issue where it does not work and an error message is displayed: Uncaught TypeError: Failed to set the 'buffer&ap ...

Is the button inactive until an action is taken?

In my coding project, I am working with two buttons. I am trying to figure out a way to disable the second button until the first button is clicked. Does anyone have any suggestions on how to achieve this using a combination of JavaScript and CSS? ...

Analyzing - Dynamically Tagging Method - Implementing direct call regulations- Erase enduring variables

https://i.sstatic.net/elGJz.jpg Hello there, I am currently utilizing a Direct call rule within DTM. When I click on a href link that opens in a new window, I want to remove or clear the eVars and events associated with the click. I have implemented custo ...

The Ultimate Guide to HTML Scrolling Default Pages

I found a JSFiddle link to help me create a sliding page content: http://jsfiddle.net/sg3s/rs2QK/ Although I'm quite pleased with it, I am trying to make the first target show up by default. I attempted to replace the panel class of the first target ...

Unable to trigger .click event in Jquery after modifying CSS attributes

Looking for a solution with my HTML code snippet: <h2 class="more-button">Read More</h2> I want to change the position of another div when this button is clicked. I am trying to achieve this using: $(".more-button").click(function(){ $(" ...

What methods does a browser use to identify SCSS files?

While exploring this html template in Chrome's inspection tool, I was amazed to see that the browser was able to detect the scss files instead of the compiled css version. To investigate further, I pressed Ctrl+U to view the page source, hoping to fin ...

Preventing the automatic selection of the initial item in a PrimeNG dropdown menu

While utilizing the p-menu component from PrimeNG in popup mode ([popup]="true"), I encountered an unexpected issue where the first item in the menu is automatically selected and turns gray. Here is the code snippet that I am using: <p-menu #menu [popu ...

What is the best way to split two divs in this manner?

Is there a way to split two divs like this in such a manner using TailwindCSS? Alternatively, what would be the CSS code for dividing these two divs? I am struggling with this concept and cannot wrap my head around how it can possibly be achieved. ...

The bookdown feature of tufte_html_book, when combined with the csl style, struggles to properly position citations

bookdown::tufte_html_book does not place citations in the margin with csl notes. Here is an example: bookdown::render_book(input = "index.rmd", output_format = "bookdown::tufte_html_book") The csl used comes from: https://raw.githu ...

The Autoprefixer script crashes with the error message: TypeError - Patterns can only be a string or an array of strings

After successfully installing autoprefixer and postcss-cli, I embarked on setting up a simple build process with NPM scripts using my command prompt and VS code. As someone with only 2 months of coding experience, I was following a tutorial on CSS/SASS top ...

Angular 8: Master the art of HTML binding

I am facing an issue with displaying HTML content in Angular 8. **Note: The HTML template I receive from the database as JSON data needs to be displayed in Angular. I am fetching this template and saving it in a variable (e.g., plot), then passing that va ...

Customizing the appearance of a submenu header on a WordPress website

I need assistance in creating a sub-menu style. Please refer to the image linked below for guidance: https://i.sstatic.net/Cu1on.jpg Here is my website link: ...