Loading bars and spinners using CSS and Javascript

A CSS and JavaScript library for inserting animated both determinate and indeterminate loading bars and spinners directly into HTML code. The goal of this library is simple usage with a lot of variability. So for inserting spinner into the page is used one line of code, specifically inserting `div` with class names that specify its properties.
The library also provides JavaScript tools to manipulate with determinate loading bars and color settings.



This library is available at GitHub or at npm.
There are two files necessary for the library to function properly and have all its functionality – main.css and
main.js. They are both located in the dist/ directory and they need to be linked in the html file.

Color configuration

Colors of individual types of elements can be configured in src/main.scss on the top. There are multiple variables
which define individual parts of an element. After making changes, the .scss file has to be recompiled. If there is
yarn installed, the command yarn gulp can be used in the commandline in the main directory to compile it.

Here is an example of correctly filled color variables for normal elements:

$line_background: lightgray; 
$line_color: #000000; 
$bordered_line_color: #14c100; 
$determinate_line_background: lightgray; 
$determinate_line_color: #004c00; 
$determinate_bordered_line_border: #F39C12; 
$determinate_bordered_line_color: #3498DB; 
$circles_color: #ae8300; 
$spinner_shadow: darkgrey; 
$spinner_color: #343a40; 
$determinate_circle_background: darkgrey; 
$determinate_circle_color: #343a40; 
$dashed_color: #138D75; 

There are three special spinners, bars, squares and dots. These can be configured to consist of multiple
shades of one color. To their color variable is inserted the darkest of these shades, the rest is scaled to lighter
shades based on scale variable.

$bars_color: #4b0049; 
$squares_color: #00064b; 
$dots_color: #004c00; 

Each scale variable has a range in which its value has to be to work properly. If zero is entered the color will not
scale and all parts of the element will have the same color as defined in the color variable. The higher the number,
the bigger the difference between colors of the two next parts.

// how much will the color lighten; when x = 0, all parts will be the same color
$bars_scale: 5;  // 0 <= x <= 14
$squares_scale: 10;  // 0 <= x <= 33
$dots_scale: 10;  // 0 <= x <= 33

Creating elements

Element basics

An element is inserted into HTML content with <div class="name_of_element"></div> and can be specified more with some
optional parameters (more below).

There are two types of elements – determinate, which show progress and range from 0 to 100%, and indeterminate,
which do not represent any value and move infinitely until they are removed or hidden.

There are 8 indeterminate bars and spinners:

  1. lv-bars
  2. lv-squares
  3. lv-circles
  4. lv-dots
  5. lv-spinner
  6. lv-dashed
  7. lv-line
  8. lv-bordered_line

and 3 determinate bars and spinners:

  1. lv-determinate_circle
  2. lv-determinate_line
  3. lv-determinate_bordered_line

Examples can be seen in the DEMO web page.

There are more possible ways to insert them into the page. One is inserting the code directly to HTML. Another one is to
insert it into HTML with JavaScript or JQuery.


After an element is inserted into the HTML code, it has to be initialized.

Automatic initialization

This library provides a method to automatically initialize new elements. This can be done by following commands:

let loader = new lv();

These two lines start an observer, which watches for newly created elements and automatically initializes them. However,
if there already are some not initialized elements in the HTML code before starting the observer, the command should be
a little different to initialize them all:

let loader = new lv();

After setting this up all new elements are automatically initialized and shown.

Manual initialization

If the automatic initialization is not set up, it has to be done manually for each element separately. To achieve this,
it is necessary to select an element to initialize and pass it as a parameter to a create function. If hide and show
and other functions will be used in the future, it is also necessary to assign the create function to a variable.
If not, just calling the create function is enough. Example manual initialization could look like this:

let element = lv.create(document.getElementById("element_id"));


If JQuery is used to select the element, it is necessary to add [0] next to it, because JQuery returns finds as a collection
and not as element, so the element would not be initialized.

let element = lv.create($("#element_id")[0]);


There are multiple predefined sizes for each element type, which can be added to the class parameter of the div. If there is none specified, the element will occupy whole
space of his container. In this case it is important that for elements 1.-6. indeterminate and 1. determinate the
container should be square, so it is not deformed.

The predefined sizes for elements 1.-6. indeterminate and 1. determinate are:

  • lg -> 200x200px
  • md -> 100x100px
  • sm -> 50x50px
  • tiny -> 30x30px

For 7.-8. indeterminate and 2.-3. determinate is predefined only width, height is variable:

  • lg -> 1000px
  • md -> 600px
  • sm -> 300px

There should be always up to one sizing parameter per element.


There are predefined parameters for positioning the element inside its container. They can be also put inside the class
argument of the div.

There are relative parameters:

  • lv-left -> align the element on the left side
  • lv-mid -> align the element to the middle
  • lv-right -> align the element on the right side

And there are absolute parameters:

  • lvt-{1-5} -> add space on top side of the element {10-30-50-80-100px}
  • lvr-{1-5} -> add space on the right side of the element {10-30-50-80-100px}
  • lvb-{1-5} -> add space on the bottom side of the element {10-30-50-80-100px}
  • lvl-{1-5} -> add space on the left side of the element {10-30-50-80-100px}

Relative and absolute parameters cannot be used together, but there can be multiple absolutes in one class.


This additional parameter adds a label to an element. It is configured by adding data-label="label_text" as a <div>
attribute. This inserts label_text either below, inside or next to an element based on its size. This can be added both to
determinate and indeterminate elements.


This additional parameter adds a percentage counter to an element. It is configured by adding data-percentage="true"
as a <div> attribute. This inserts a counter either inside or next to an element based on its shape or automatically
turns it off if the element is too small. If the argument is omitted or set to anything else than "true" the percentage
counter will not be shown.

Example usage

This can be inserted anywhere in the body of the document.

<div class="lv-bars lg lv-mid lvt-5 lvb-3" data-label="Loading..."></div>
<div class="lv-determinate_circle md lv-right lvt-5 lvb-1" data-label="Loading..." data-percentage="true"></div>

Control of elements

Visibility of elements

All types of elements have their hide and show functions. However, to use them it is necessary to assign creation of the element to a variable
as described in the initialization section. All elements are visible by default.

let element = lv.create(selected_element);



Each element has also its remove() function, which removes the element from the page permanently. After calling this
function the element cannot be shown again and it is necessary to create new element (even the html code of the removed
element is permanently gone).

let element = lv.create(selected_element);





Label control

There are provided functions to change or remove labels with JavaScript. They are called on the element object as well as
hide(), remove() and show() functions. The functions are:



This is possible for all element types. More about labels is described above.

Percentage control

There are provided functions to show or hide percentages with JavaScript. They are called on the element object as well as
hide(), remove() and show() functions. The functions are:



This functionality applies only on determinate loading elements. The functions can be called on indeterminate elements but
they will have no effect. More about percentages is described above.

Controlling the indeterminate bars and spinners

There is no simple way provided to control or stop the animation of indeterminate spinners and bars. They can be hidden using the ‘hide()’ function, removed completely using the remove() function or made visible using the show() function.

Controlling the determinate bars and spinners

Again, to control determinate bars and spinners the element has to be assigned to variable.

There is an universal update function, which can set or add any value to the spinner. It updates both linear and non-linear
elements and its called directly on the variable, where is saved the element.

element.update(type, value, maxValue);
  • type … “add” or “set”, if the value should be added to current or set to a new one
  • value … number to add or set
  • maxValue … value that represents 100% of the progress

Then there are 4 functions to make updating bar easier:

  • maxValue … value that represents 100% of the progress
  • maxValue … value that represents 100% of the progress
element.add(value, maxValue);
  • value … number to add to current state
  • maxValue … value that represents 100% of the progress
element.set(value, maxValue);
  • value … number to add or set
  • maxValue … value that represents 100% of the progress

See live demo and download source code.


This awesome script developed by RIDICS, Visit their official github repository for more information and follow for future updates.

Don’t forget to Subscribe My Public Notebook for more useful free scripts, tutorials and articles.