Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fill background color only 50% of it total width

Tags:

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.

like image 275
gaurav jain Avatar asked Sep 05 '13 22:09

gaurav jain


People also ask

How do you make a half background color in HTML?

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.

How do you add a background color to a percentage?

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);

How do I change the width color of a background in CSS?

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 ...

How do you specify a background color on an entire page?

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.


1 Answers

There are multiple different ways to achieve this.

Pseudo element approach:

<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.. */ } 

Linear-gradients approach:

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%); } 

Animation without JS/JQUERY

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;     } } 

Transition approach

EXAMPLE HERE

.progress:after {     /* other styling.. */     width: 0;     transition: all 2s;     -webkit-transition: all 2s; } .progress:hover:after {     width: 50%; } 
like image 156
Josh Crozier Avatar answered Sep 28 '22 08:09

Josh Crozier