Simple Loading Screen using HTML and CSS

Simple Loading Screen using HTML and CSS

CSS Animation property, Position property of CSS and Keyframes are used to make the simple loading screen.

HTML Code for Loading Screen

Two Divs are made in HTML file. A main div and a spinner div. Look at the HTML code below.

<!DOCTYPE html>
<html>
<body>
<div id="main-div">
 <div id="spinner">
 </div>
</div>
</body>
</html>

CSS Code for Loading Screen

The CSS Code of simple loading screen is explained below.

Main Div CSS

The main div position property is set to fixed so that loading screen is always over the main content of the page, even if user scrolls the page. It's width and height both are set to 100% to cover the whole page. Background color is also set to black with opacity of 0.5.

#main-div
{
position: fixed;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.5);
}

Spinner Div CSS

The spinner div is displayed in the center of the main div both vertically and horizontally using position property. It's width and height, both are set to 75px and border radius is set to 50% to make it a circle.

5pxborder is given to spinner div but the top border color is set to transparent which will make it look like a loading icon.

The spinner div is then animated using animation property of CSS. Time Duration is set to 2s, Iteration count is set to infinite to keep rotating it again and again while timing function is set to linear to keep the speed constant through out the process.

#spinner
{
position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
margin: auto;
width: 75px;
height: 75px;
border: 5px solid #fff;
border-radius: 50%;
border-top-color: transparent;
animation: spin 2s infinite linear;
}

KeyFrames for Loading Screen Spinner

After this, keyframes are defined for the animation, keyframes gradually change the style of element from current CSS style to new CSS style.

In our example the spinner div will start it's rotation from 0 degree angle and after 2 seconds it will complete its rotation at 360 degree angle.

@keyframes spin
{
from 
{ 
transform: rotate(0deg);
}
to
{
transform: rotate(360deg);
}
}

Video Tutorial

Watch video tutorial below to understand it better.