Steps for creating a dynamic progress bar with inverted text style using only CSS

I am working on creating a dynamic progress bar with inverted text color using only CSS.

To see examples of what I am trying to achieve, you can refer to the following links.

Inverted Colors CSS progress bar

CSS Progress Bars

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

The issue I am facing is that the progress bars in the examples provided have fixed widths. I want the progress bar to be of a width of 100% or some other relative length. It's proving difficult to modify the existing examples to fit my requirements without using JavaScript.

You can see my unsuccessful attempt here. Can you help adjust the implementation so that the progress bar supports relative widths like 50%?

Here is the code snippet. Thanks to @TheThirdMan.

body {
  background-color: #000;
}

h1 {
  text-align: center;
  font-family: "Trebuchet MS", Helvetica, sans-serif;
  font-weight: normal;
  margin: 50px 0 0;
  color: #fff;
}

.progress {
  position: relative;
  border: 0;
  width: 500px;
  height: 40px;
  line-height: 40px;
  margin: 100px auto;
  font-family: arial, sans-serif;
  font-weight: bold;
  font-size: 30px;
  background-color: #07A4DD;
  border-radius: 40px;
  overflow: hidden;
}
.progress .progress-text {
  position: absolute;
  top: 0;
  width: 500px;
  text-align: center;
  color: #fff;
  font-variant: small-caps;
}
.progress .progress-bar {
  height: 100%;
  overflow: hidden;
  width: 54%;
  background-color: #fff;
  border-radius: inherit;
  overflow: hidden;
  position: relative;
  -webkit-animation: animate 4s;
          animation: animate 4s;
}
.progress .progress-bar .progress-text {
  color: #07A4DD;
}

@-webkit-keyframes animate {
  0% {
    width: 0%;
  }
}

@keyframes animate {
  0% {
    width: 0%;
  }
}
<h1>Flexible CSS progress bar with inverted colors</h1>
<div class="progress">
  <div class="progress-text">Progress</div>
  <div class="progress-bar">
    <div class="progress-text">Progress</div>
  </div>
</div>
  
     


Thank you to @AndrewBone for the suggestion, but I have reasons for not using `vw` in this project:

  1. We previously used `vw` for other projects, but it was not supported on some Android devices so we had to abandon it.
  2. In many cases, the parent of `.progress` may not be proportionate to the viewport's width.

Here is my attempt. It is similar to the concept of CSS Progress bars.

The idea behind it is as follows:

  • Both `.progress-text` elements are absolutely positioned and have the same width as `.progress`.
  • The first `.progress-text` is white and acts as a background within `.progress`.
  • The second `.progress-text` is blue and is clipped by `.progress-bar`.

This leads to:

  • `.progress-bar` needs to be relatively positioned to clip `.progress-text`.
  • `.progress-text` cannot obtain the width of `.progress` as it is absolutely positioned to `.progress-bar` and not `.progress`.

Therefore:

  • If `.progress` has a fixed width (e.g. `$bar-width: 500px`), I can center `.progres-text` by setting its width to match that of `.progress`.
  • If `.progress` has a relative width (e.g. `$bar-width: 50%`), what steps should I take to make it work?

Answer №1

Hey everyone, I wanted to share an interesting concept with you. Please correct me if I'm mistaken. vw (REF) is a cool CSS3 feature that represents viewport width. In the example below, 75vw is used, indicating 75% of the viewport width. It's responsive and adjusts size as you resize the page.

Additionally, I've incorporated a CSS variable to streamline my testing process. While CSS variables are cutting edge, not all browsers support them yet (REF)

:root {
  --progress-width: 75vw;
}
body {
  background-color: #000;
}
h1 {
  text-align: center;
  font-family: "Trebuchet MS", Helvetica, sans-serif;
  font-weight: normal;
  margin: 50px 0 0;
  color: #fff;
}
.progress {
  position: relative;
  border: 0;
  width: var(--progress-width, 500px);
  height: 40px;
  line-height: 40px;
  margin: 100px auto;
  font-family: arial, sans-serif;
  font-weight: bold;
  font-size: 30px;
  background-color: #07A4DD;
  border-radius: 40px;
  overflow: hidden;
}
.progress .progress-text {
  position: absolute;
  top: 0;
  width: var(--progress-width, 500px);
  text-align: center;
  color: #fff;
  font-variant: small-caps;
}
.progress .progress-bar {
  height: 100%;
  overflow: hidden;
  width: 54%;
  background-color: #fff;
  border-radius: inherit;
  overflow: hidden;
  position: relative;
  -webkit-animation: animate 4s;
  animation: animate 4s;
}
.progress .progress-bar .progress-text {
  color: #07A4DD;
}
@-webkit-keyframes animate {
  0% {
    width: 0%;
  }
}
@keyframes animate {
  0% {
    width: 0%;
  }
}
<div class="progress">
  <div class="progress-text">Progress</div>
  <div class="progress-bar">
    <div class="progress-text">Progress</div>
  </div>
</div>

I hope you find this information useful.

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

Learn how to incorporate a newly created page URL into a customized version of django-oscar

Just starting out with django-oscar and trying to set up a new view on the page. I've successfully created two pages using django-oscar dashboard, https://ibb.co/cM9r0v and added new buttons in the templates: Lib/site-packages/oscar/templates/osca ...

Is it possible to customize the appearance of HTML input fields pre-populated by the browser

My ASP.NET MVC application has a login page: <form id="account" method="post"> <h2>Use email account to log in.</h2> <hr /> <div asp-validation-summary="ModelOnly" class=& ...

Developing a timetable feature using PHP

I am working on creating a schedule page that involves an SQL table named 'appointments' with columns for id, name, date, and time. My goal is to organize the data from this table into an HTML/CSS based table where all records with the same date ...

The XmlUtil.serialize(root) function is converting empty tags into self-closing tags

Check out this cool code snippet featuring an empty div tag (<div></div>): import groovy.xml.DOMBuilder import groovy.xml.XmlUtil def HTML_STRING = ''' <html> <div></div> <div>Some text</d ...

"Responsive header design using Bootstrap for both desktop and mobile devices

Is there a way to create a header that adjusts its width based on the device viewing it, with maximum width for desktop and no padding for mobile? Here is an example of the code: <div class="row"> <div class="span12"> ...

Hide the div in PHP if the active variable is null

It looks like I'll need to implement Jquery for this task. I have a print button that should only appear if my array contains data. This is a simplified version of what I'm aiming to achieve: HTML <?include 'anotherpage.php'?> ...

execute a different function once the animation has finished running with the .bind

When attempting to detect the completion of a CSS animation in order to remove a class, I encountered an issue where using .bind causes the animation end event to trigger even for the other function. You can view the complete code here. How can I ensure ...

Tips for maintaining color on <a> tags even after they have been clicked

My webpage has 3 <a> tag links to 3 different pages. I have set the hover state color to red, and now I want the background-color of the clicked page to remain as it was in the hover state. How can I achieve this? <html lang="en"> < ...

Designing a drop-down selection interface

http://jsfiddle.net/367ms/1/ I need assistance with my code. My goal is to have a blue border that appears 10 px below the text when hovered over. Additionally, I have a submenu that opens on hover, but it automatically closes when trying to navigate down ...

Switch from scrolling the entire page to scrolling within a single div

Whenever I click on an element on my webpage, a modal window with a fixed position appears. The issue I am facing is that when I scroll using the mouse or touch gestures on my phone or tablet, the entire page scrolls instead of just the content within the ...

Why does the last-of-type selector work, but the first-of-type doesn't seem to?

The SCSS code snippet below is causing me some confusion: li { &.menu-item-type-custom { margin-bottom: 10px; a { // } &:last-of-type { margin-bottom: 40px; } &:first-of-type { margin-top: 40px; ...

An easy method for modifying data on a webpage hosted by an ESP8266 device

In my web page, I have a form to input times. The page is saved in flash memory and I want to update the times with previously stored values in EEPROM without loading the page into RAM. I prefer not to use websockets or AJAX for this purpose. I am looking ...

position property in div element disrupts slider functionality

I've been working on incorporating a simple slider into my website and stumbled upon this example on jsfiddle My goal is to have the slider positioned "relative" within my site, but when I change the CSS to position: relative;, the slider no longer ...

A minimalist web page appearing as blank in Internet Explorer

After testing my simple web page in various browsers, I discovered that it only showed a blank white page in Internet Explorer. I combed through my CSS code and identified background = rgba(some stuff) as the unsupported line by IE. However, this particula ...

Elements that are added dynamically do not contain any space between each other

I have a markup template that looks like this. <template id="unsequenced-template"> <button type="button" class="btn btn-primary railcar"></button> </template> Then, I use the following JavaScript ...

What is the best way to design a shape (a quadrilateral with rounded edges) using CSS?

Struggling with creating a widget in React Js that has an image as the background? The image is designed to be dynamic, changing color and size based on the device. I've attempted using inline SVG and SVG within an img tag but can't seem to contr ...

Experiencing difficulty aligning the Material UI grid to float on the right side

I'm currently in the learning phase of material-ui and encountering an issue with floating the grid content to the right side. Despite trying alignItems="flex-start" justify="flex-end" direction="row" in the container grid, as well as using the css p ...

The command `browser.find_elements_by_class_name('foo')` returned zero results

I'm encountering an issue with selenium while using Python 3.7. The website I'm working on has a structure similar to this: <html> <body> <div class="foo"> <div class="bar1">some ...

Enhance Vuetify pagination with personalized styles

I'm attempting to create a toolbar with pagination control aligned to the right, featuring smaller pagination control buttons. To achieve this, I took the following steps: Wrapped v-pagination in span (since v-spacer did not work without this) Adde ...

"JQuery event handlers not functioning as expected: .click and .on failing

For some time now, I've been facing this issue and I'm at a loss trying to figure it out. I have attempted various solutions such as using .click(), .on(), .delegate, and even utilizing .find() to locate the specific element causing the problem. ...