Loader with HTML and CSS
Creating a Loader with HTML and CSS
Loading animations are a great way to keep users engaged while they wait for content to load on a website. In this tutorial, we will show you how to create a simple yet attractive loader using HTML and CSS. We will provide you with the complete source code and explain step by step how it works.
HTML Structure
Let’s start by setting up the HTML structure for our loader. We will create one section element with class loader and five child div elements with class slider. Each div will represent a part of our loader animation.
<!DOCTYPE html> <html lang="en"> <head> Â <meta charset="UTF-8"> Â <title>Loader StudyMuch</title> Â <style,type="text/css"> Â Â Â /* Your CSS code goes here */ Â </style> </head> <body> <section class="loader"> Â <div class="slider" style="--i:0"></div> Â <div class="slider" style="--i:1"></div> Â <div class="slider" style="--i:2"></div> Â <div class="slider" style="--i:3"></div> Â <div class="slider" style="--i:4"></div> </section> </body> </html>
Styling with CSS
Now, let’s add the CSS code to style our loader. We’ll explain the key parts of the CSS code step by step.
<style type="text/css"> body { Â display: flex; Â justify-content: center; Â align-items: center; Â background: black; Â height: 50vh; } .loader { Â display: flex; Â align-items: center; Â Â Â justify-content: center; Â flex-direction: row; Â } .slider { Â overflow: hidden; Â background-color: white; Â margin: 0 15px; Â height: 80px; Â width: 20px; Â border-radius: 30px; Â Â box-shadow: 10px 5px 20px rgba(0, 0, 0, 0.1), Â Â Â Â Â Â Â -15px -15px 30px #b8b8b8, Â Â Â Â Â Â Â inset 5px 5px 10px rgba(0, 0, 255, 0.1), Â Â Â Â Â Â Â inset 35px 5px 10px rgba(0, 0, 0, 0.1); Â position: relative; } .slider::before { Â content: ""; Â position: absolute; Â height: 20px; Â width: 20px; Â box-shadow: inset 0px 0px 0px rgba(0, 0, 0, 0.3), Â Â Â Â Â Â Â 0px 420px 0 400px #2697f3, Â Â Â Â Â Â Â inset 0px 0px 0px rgba(0, 0, 0, 0.1); Â animation: animate_2 1.5s ease-in-out infinite; Â animation-delay: calc(-0.2s * var(--i)); Â } @keyframes animate_2 { Â Â 0% { Â Â Â transform: translateY(250px); Â Â Â filter: hue-rotate(0deg); Â Â } Â Â 50% { Â Â Â transform: translateY(0); Â Â } Â Â 100% { Â Â Â transform: translateY(250px); Â Â Â filter: hue-rotate(180deg); Â Â } } </style>
CSS Explanation
- We start by styling the body element to center
our loader on a black background and set the height to 50vh (50% of the viewport height) to make it visually appealing.
- The .loader class
styles the container of our loader, centering it both horizontally and vertically.
- Each .slider
represents a part of our loader. We’ve added rounded corners, shadows, and other styles to make it look visually appealing.
- The .slider::before
pseudo-element is used to create the animated element within each slider.
- We define a keyframe animation called animate_2
that moves the element vertically and changes its hue to create the loading effect.
- The animation-delay
property is set to stagger the animations of each slider.
Conclusion;
With this HTML and CSS code, you’ve created a visually appealing loader animation that you can use in your web projects. Feel free to customize the colors, sizes, and animation timings to match your website’s design. Loaders like this add a touch of professionalism and engagement to your site, enhancing the user experience during content loading. And last if you have any doubt regarding this post you can ask with any hesitation in the comment section.
Learn More;
1 Comment
canada pharmacies · October 29, 2023 at 5:42 am
Awesome! Its in fact amazing post, I have got much clear idea concerning from this post.