Is it possible in CSS to fill background color only 50% of its total width?
I am trying make a progress bar where I need to fill background color based on the "%"
of progress.
Please provide pointers.
You can apply a background color to the html element, and then apply a background-image to the body element and use the background-size property to set it to 50% of the page width. This results in a similar effect, though would really only be used over gradients if you happen to be using an image or two.
var stepSize = 50; setTimeout((function() { var filler = document. getElementById("filler"), percentage = 0; return function progress() { filler. style. width = percentage + "%"; percentage +=1; if (percentage <= 100) { setTimeout(progress, stepSize); } } }()), stepSize);
There are four different syntaxes you can use with this property: the keyword syntax ("auto", "cover" and "contain"), the one-value syntax (sets the width of the image (height becomes "auto"), the two-value syntax (first value: width of the image, second value: height), and the multiple background syntax (separated ...
To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.
There are multiple different ways to achieve this.
<div class="progress"></div>
EXAMPLE HERE
.progress { width: 200px; height: 50px; border: 1px solid black; position: relative; } .progress:after { content: '\A'; position: absolute; background: black; top: 0; bottom: 0; left: 0; width: 50%; /* Specify the width.. */ }
The advantage to this approach is that you can specify multiple different colors.
EXAMPLE HERE
.progress { width: 200px; height: 50px; border: 1px solid black; background: linear-gradient(to right, black 50%, white 50%); }
Either of these approaches can be animated with pure CSS:
EXAMPLE HERE
.progress:after { /* other styling .. */ width: 50%; /* End width.. */ -webkit-animation: filler 2s ease-in-out; -moz-animation: filler 2s ease-in-out; animation: filler 2s ease-in-out; } @-webkit-keyframes filler { 0% { width: 0; } }
EXAMPLE HERE
.progress:after { /* other styling.. */ width: 0; transition: all 2s; -webkit-transition: all 2s; } .progress:hover:after { width: 50%; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With