This time I will bring you two ways to implement progress bar in CSS3. What are the notes for implementing progress bar in CSS3? Here are practical cases, let’s take a look.
The renderings are as follows:
The first posture is as follows
First enter the code
<p> <span></span> </p>
/*对应CSS*/ #progress{ width: 300px; height: 30px; border:1px solid #ccc; border-radius: 15px; overflow: hidden; /*注意这里*/ box-shadow: 0 0 5px 0px #ddd inset; } #progress span { display: inline-block; width: 70%; height: 100%; background: linear-gradient(45deg, #2989d8 30%,#7db9e8 31%,#7db9e8 58%,#2989d8 59%); background-size: 60px 30px; text-align: center; color:#fff; animation:load 3s ease-in; } @keyframes load{ 0%{ width: 0%; } 100%{ width:70%; } }
The gradient color above is used Linear-gradient
linear-gradient syntax in css3
<linear-gradient> = linear-gradient([ [ <angle> | to <side-or-corner> ] ,]? <color-stop>[, <color-stop>]+) <side-or-corner> = [left | right] || [top | bottom] <color-stop> = <color> [ <length> | <percentage> ]?</percentage></length></color></color-stop></side-or-corner></color-stop></color-stop></side-or-corner></angle></linear-gradient>
angle: Use the angle value to specify the direction (or angle) of the gradient ).
to left: Set the gradient from right to left. Equivalent to: 270deg
to right: Set the gradient from left to right. Equivalent to: 90deg
to top: Set the gradient from bottom to top. Equivalent to: 0deg
#to bottom: Set the gradient from top to bottom. Equivalent to: 180deg. This is the default value, which is equivalent to leaving it blank.
color-stop: Used to specify the starting and ending colors of the gradient:
color: Specify the color.
length: Use the length value to specify the starting and ending color positions. Negative values are not allowed
percentage: Use percentage to specify the starting and ending color positions.
Chestnut:
.test1{ background: linear-gradient(#EA2000, #E5A399); } .test2 { background: linear-gradient(45deg,#EA2000 20%,#E5632D 50%, #E5B12D 80%); } .test3 { background: linear-gradient(to top right, #000, #416796 50%, #B5CAE4); } .test4{ background: linear-gradient(45deg, #2989d8 30%,#7db9e8 31%,#7db9e8 58%,#2989d8 59%); }
Rendering:
Animation is implemented through keyframes, and the progress effect is achieved by changing the width of span. Isn’t it simple? !
The current progress bar effect is only 70% effective. Just change the value of width. Just like the picture below, change it to a certain value.
Second posture
First, structure it A square p, symmetrically divide the square into two left and right pieces, as follows
What is constructed here is a square of 200px 200px, divided into two rectangles of 100px200px.
<p> </p><p> </p> <p> </p>
.progress2{ width: 200px; height: 200px; margin: 100px auto; position: relative; border:1px solid #ddd; } .rect{ width: 100px; height: 200px; position: absolute; top:0; overflow: hidden; /*注意这里*/ } .right{ right:0; } .left{ left:0; }
The following effect
Next, construct a hollow circle in each rectangle, first construct the right half, as follows
<p> </p><p> </p><p></p> <p> </p>
.circle{ width: 160px; height: 160px; border:20px solid #ccc; border-radius: 50%; position: absolute; top:0; } .rightcircle{ border-top:20px solid rgb(41,137,216); border-right:20px solid rgb(41,137,216); right:0; }
You will see the following effect
Since overflow is set to hidden in class:rect, the overflow part is After covering it, the subsequent effect realization is also closely connected with this attribute relationship.
If this property is not set, the effect will be like this.
Then you will find that part of it is covered, we can rotate it 45 degrees
.circle{ width: 160px; height: 160px; border:20px solid #ccc; border-radius: 50%; position: absolute; top:0; transform: rotate(45deg); /*注意这里*/ }
The effect is as follows
.rightcircle{ border-top:20px solid rgb(41,137,216); border-right:20px solid rgb(41,137,216); right:0; animation: load_right 5s linear infinite; } @keyframes load_right{ 0%{ transform: rotate(45deg); } 50%{ transform: rotate(225deg); } 100%{ transform: rotate(225deg); } }This is the effect of not setting overflow:hidden in class rect
这是在class rect中设置了overflow:hidden的效果
现在,我们就可以把左半边也凑上了,同理,动画效果一开始先旋转45度,调正,然后一直保持不变,到50%之后,再开始旋转180度。这样就与右半边衔接上了。
<p> </p><p> </p><p></p> <p> </p><p></p>
.leftcircle{ border-bottom:20px solid rgb(41,137,216); border-left:20px solid rgb(41,137,216); left:0; animation: load_left 5s linear infinite; } @keyframes load_left{ 0%{ transform: rotate(45deg); } 50%{ transform: rotate(45deg); } 100%{ transform: rotate(225deg); } }
这是整体的效果
可以调整角度或者调整颜色即可实现反向的效果。
我是调整了颜色就可以得到最开始的动画效果啦,下面是完整的代码
<p> </p><p> </p><p></p> <p> </p><p></p>
.progress2{ width: 200px; height: 200px; margin: 100px auto; position: relative; } .rect{ width: 100px; height: 200px; position: absolute; top:0; overflow: hidden; } .right{ right:0; } .left{ left:0; } .circle{ width: 160px; height: 160px; border:20px solid rgb(41,137,216); border-radius: 50%; position: absolute; top:0; transform: rotate(45deg); } .rightcircle{ border-top:20px solid #ccc; border-right:20px solid #ccc; right:0; animation: load_right 5s linear infinite; } .leftcircle{ border-bottom:20px solid #ccc; border-left:20px solid #ccc; left:0; animation: load_left 5s linear infinite; } @keyframes load_right{ 0%{ transform: rotate(45deg); } 50%{ transform: rotate(225deg); } 100%{ transform: rotate(225deg); } } @keyframes load_left{ 0%{ transform: rotate(45deg); } 50%{ transform: rotate(45deg); } 100%{ transform: rotate(225deg); } }
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
css的background-attachment进阶使用方法
The above is the detailed content of Two ways to implement progress bars in CSS3. For more information, please follow other related articles on the PHP Chinese website!

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

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.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
