search
HomeWeb Front-endCSS TutorialImplement a simple skeleton screen using CSS

Implement a simple skeleton screen using CSS

Preface

When loading network data, in order to improve the user experience, a loading animation that rotates in circles or a Skeleton Screen is usually used. Compared with loading animation, Skeleton Screen has a more vivid effect and is also very simple to implement. You can implement a simple Skeleton Screen using CSS. (What is Skeleton Screen?)

Recommended learning: CSS video tutorial, CSS tutorial (picture and text )

Ideas

  • HTML skeleton

  • CSS plus style

  • CSS plus animation

Start by building the skeleton

The skeleton structure is very simple, just randomly place a few block-level elements you like and it’s ok.

<p>
  </p>
        
  •     
  •     
  •   

Look, it’s that simple.

CSS coloring

The skeleton screen we often see looks like this

Implement a simple skeleton screen using CSS

For the convenience of description , to enhance the contrast, I will first make a ghost version of

Implement a simple skeleton screen using CSS

First use the linear-gradient attribute of css to draw a red A gradient picture with a hint of green in the middle, and fill it as the background for the li tag

linear-gradient() can create a picture with a linear gradient of multiple colors. To learn more, you can read here

li{    background-image: linear-gradient(90deg, #ff0000 25%, #41de6a 37%, #ff0000 63%);    width: 100%;    height: 0.6rem;    list-style: none;
}
Implement a simple skeleton screen using CSS

In actual use, change the gradient map to a normal color, such as: background-image: linear-gradient(90deg, #f2f2f2 25%, #e6e6e6 37%, #f2f2f2 63%)

Let it move

The rest What we have to do is to make the green in the middle move

Can you think of any way to make it move?

What is used here is to stretch the background image, dynamically set the background positioning percentage, change the background positioning, and thereby calculate the different offset values ​​of the image relative to the container, thereby achieving the animation effect. .

li{    background-image: linear-gradient(90deg, #ff0000 25%, #41de6a 37%, #ff0000 63%);    width: 100%;    height: 0.6rem;    list-style: none;    background-size: 400% 100%;    background-position: 100% 50%;    animation: skeleton-loading 1.4s ease infinite;
}

@keyframes skeleton-loading {
  0% {    background-position: 100% 50%;
  }
  100% {    background-position: 0 50%;
  }
}

Two values ​​are set here for the background-position attribute. The first value represents the offset of the horizontal position relative to the container, and the second value represents the offset of the vertical position relative to the container.

When using percentage to set the background-position value, it will perform a formula to calculate the actual positioning value (container width - image width) * (position x%) = (x offset value), that is, the width difference between the container and the image is multiplied by the set percentage positioning value. The result is the actual offset value. Set the width of background-size to one of 400% The purpose is to create a width difference with the container.

Some students may ask, setting the background-size value to 50% can also create a width difference from the container. Yes, but in this way, the background image will tile the entire container, and you will be surprised to find that the green dot becomes a double.

You can try setting different values ​​for background-size, observe its performance, and think about why this happens.

Finally, use keyframe animation to set the background-position value at the x coordinate from 100% to 0%

@keyframes skeleton-loading {
  0% {    background-position: 100% 50%;
  }
  100% {    background-position: 0 50%;
  }
}

Assume that the width of the container is 100px, then the width of the background image is 400px. Using the above formula, in the first frame of animation, the background image is offset relative to the container The real value is

(100px-400px)*100% = -300px

The actual offset of the last frame

(100px-400px)*0% = 0

The animation process is actually a linear background image 3 times the width of the container. The offset relative to the container is from -300px The process of change from to 0.

More related tutorial recommendations: "PHP Programming Introduction Tutorial"

The above is the detailed content of Implement a simple skeleton screen using CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
What does margin: 40px 100px 120px 80px signify?What does margin: 40px 100px 120px 80px signify?Apr 28, 2025 pm 05:31 PM

Article discusses CSS margin property, specifically "margin: 40px 100px 120px 80px", its application, and effects on webpage layout.

What are the different CSS border properties?What are the different CSS border properties?Apr 28, 2025 pm 05:30 PM

The article discusses CSS border properties, focusing on customization, best practices, and responsiveness. Main argument: border-radius is most effective for responsive designs.

What are CSS backgrounds, list the properties?What are CSS backgrounds, list the properties?Apr 28, 2025 pm 05:29 PM

The article discusses CSS background properties, their uses in enhancing website design, and common mistakes to avoid. Key focus is on responsive design using background-size.

What are CSS HSL Colors?What are CSS HSL Colors?Apr 28, 2025 pm 05:28 PM

Article discusses CSS HSL colors, their use in web design, and advantages over RGB. Main focus is on enhancing design and accessibility through intuitive color manipulation.

How can we add comments in CSS?How can we add comments in CSS?Apr 28, 2025 pm 05:27 PM

The article discusses the use of comments in CSS, detailing single-line and multi-line comment syntaxes. It argues that comments enhance code readability, maintainability, and collaboration, but may impact website performance if not managed properly.

What are CSS Selectors?What are CSS Selectors?Apr 28, 2025 pm 05:26 PM

The article discusses CSS Selectors, their types, and usage for styling HTML elements. It compares ID and class selectors and addresses performance issues with complex selectors.

Which type of CSS holds the highest priority?Which type of CSS holds the highest priority?Apr 28, 2025 pm 05:25 PM

The article discusses CSS priority, focusing on inline styles having the highest specificity. It explains specificity levels, overriding methods, and debugging tools for managing CSS conflicts.

In how many ways can we add CSS to our HTML file?In how many ways can we add CSS to our HTML file?Apr 28, 2025 pm 05:24 PM

Article discusses three methods to add CSS to HTML: inline, internal, and external. Each method's impact on website performance and suitability for beginners is analyzed.(159 characters)

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.