惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园_首页
AWS News Blog
AWS News Blog
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tailwind CSS Blog
PCI Perspectives
PCI Perspectives
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 叶小钗
IT之家
IT之家
V2EX - 技术
V2EX - 技术
Cloudbric
Cloudbric
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
宝玉的分享
宝玉的分享
Know Your Adversary
Know Your Adversary
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Schneier on Security
博客园 - 【当耐特】
G
Google Developers Blog
S
Security @ Cisco Blogs
N
Netflix TechBlog - Medium
T
Tenable Blog
C
Check Point Blog
The Cloudflare Blog
J
Java Code Geeks
The Register - Security
The Register - Security
Google Online Security Blog
Google Online Security Blog
Security Latest
Security Latest
T
Tor Project blog
T
The Blog of Author Tim Ferriss
S
Security Affairs
S
Securelist
P
Privacy & Cybersecurity Law Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Privacy International News Feed
博客园 - 三生石上(FineUI控件)
D
DataBreaches.Net
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
G
GRAHAM CLULEY
Jina AI
Jina AI
Spread Privacy
Spread Privacy

CSS Articles by Temani Afif

Get Ready For the Powerful CSS border-shape Property! | CSS-Tricks Let’s Play With Gap Decorations! How to Control Infinite CSS Animations (Part 2 of 2) How to Control Infinite CSS Animations (Part 1 of 2) Two Circles, One Arrow, and Anchor Positioning Making a Responsive Pyramidal Grid With Modern CSS | CSS-Tricks How to Create a CSS-only Elastic Text Effect Making Complex CSS Shapes Using shape() | CSS-Tricks Responsive Hexagon Grid Using Modern CSS | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 2) | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 1) | CSS-Tricks Perfectly Pointed Tooltips: To The Corners Perfectly Pointed Tooltips: All Four Sides Perfectly Pointed Tooltips: A Foundation Sequential linear() Animation With N Elements | CSS-Tricks Infinite Marquee Animation using Modern CSS Better CSS Shapes Using shape() — Part 4: Close and Move | CSS-Tricks Drawing CSS Shapes using corner-shape Better CSS Shapes Using shape() — Part 3: Curves | CSS-Tricks Better CSS Shapes Using shape() — Part 2: More on Arcs | CSS-Tricks Better CSS Shapes Using shape() — Part 1: Lines and Arcs | CSS-Tricks Creating Blob Shapes using clip-path: shape() Creating Flower Shapes using clip-path: shape() Custom progress element using the attr() function A CSS-Only Star Rating Component and More! (Part 2) | CSS-Tricks A CSS-Only Star Rating Component and More! (Part 1) | CSS-Tricks How to Create Wavy Boxes Using CSS Full-Bleed Layout with Modern CSS Fancy Menu Navigation Using Anchor Positioning | CSS-Tricks How to Create a Zig-Zag Box Using CSS How to Create Zig-Zag CSS Loaders Using One Element Custom Progress Element Using Anchor Positioning & Scroll-Driven Animations How to Create Filling CSS Loaders Using One Element How to Create Curved-Edge and Rounded-Edge Shapes Using CSS CSS Tricks That Use Only One Gradient | CSS-Tricks How to create Shapes with Inner Curves using CSS Mask Custom Range Slider Using Anchor Positioning & Scroll-Driven Animations How to Get the Width/Height of Any Element in Only CSS How Keyboard Navigation Works in a CSS Game How To Create Cut-Out Shapes using The clip-path property The Modern Guide For Making CSS Shapes — Smashing Magazine css-shape.com: The Ultimate Collection of CSS-only Shapes ⚡️ Sliding 3D Image Frames In CSS — Smashing Magazine CSS Tricks To Master The clip-path Property Creating Wavy Circles with Fancy Animations in CSS Modern CSS Tooltips And Speech Bubbles (Part 2) — Smashing Magazine Do you need a Tooltip or a Speech Bubble? I have created 100 using CSS 😲 Modern CSS Tooltips And Speech Bubbles (Part 1) — Smashing Magazine Creating Flower Shapes using CSS Mask & Trigonometric Functions CSS Shapes: The Triangle The Complex But Awesome CSS border-image Property — Smashing Magazine CSS Tricks to add 3D Effects to your Text CSS Responsive Multi-Line Ribbon Shapes (Part 2) — Smashing Magazine CSS Responsive Multi-Line Ribbon Shapes (Part 1) — Smashing Magazine I have made 100+ CSS-only Ribbon Shapes | The Perfect Collection 🎀 How to create a CSS-only infinite scroll animation Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 2) — Smashing Magazine Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 1) — Smashing Magazine How to Create CSS Ribbon Shapes with a Single Element CSS Shapes: The Ribbon Revealing Images With CSS Mask Animations — Smashing Magazine css-loaders.com: The Biggest Collection of Loading Animations (more than 500 🤯) How To Define An Array Of Colors With CSS — Smashing Magazine CSS effects on images II Shines, Perspective, And Rotations: Fancy CSS 3D Effects For Images — Smashing Magazine What's your Lucky CSS Pattern? Check It Now! How to Add a CSS Reveal Animation to Your Images Modern Layouts using CSS Grid I created 100+ unique CSS patterns | The best collection 🤩 How to Create a Custom Range Slider Using CSS CSS Shapes: The Heart CSS Shapes: Polygon & Starburst A Text Reveal Animation using CSS CSS Tip: learn CSS the easy way! How to build a CSS-only accordion How to create Breadcrumb Navigation with CSS Different Ways to Get CSS Gradient Shadows | CSS-Tricks A CSS-only responsive Stepper component A Fancy Hover Effect For Your Avatar | CSS-Tricks How to make a zoom effect using CSS Creating a Custom Cursor using CSS CSS Infinite 3D Sliders | CSS-Tricks CSS Infinite Slider Flipping Through Polaroid Images | CSS-Tricks How to create a responsive sidebar menu using CSS CSS Infinite and Circular Rotating Image Slider | CSS-Tricks Making Static Noise From a Weird CSS Gradient Bug | CSS-Tricks CSS Grid and Custom Shapes, Part 3 | CSS-Tricks Fancy Image Decorations: Outlines and Complex Animations | CSS-Tricks Fancy Image Decorations: Masks and Advanced Hover Effects | CSS-Tricks Fancy Image Decorations: Single Element Magic | CSS-Tricks How to create an infinite image slider using CSS How to Create Wavy Shapes & Patterns in CSS | CSS-Tricks How I Made a Pure CSS Puzzle Game | CSS-Tricks How to create a Tooltip/Speech Bubble using CSS CSS Grid and Custom Shapes, Part 2 | CSS-Tricks CSS Grid and Custom Shapes, Part 1 | CSS-Tricks Zooming Images in a Grid Layout | CSS-Tricks How to create a CSS-only loader with one element Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers | CSS-Tricks How to create background pattern using CSS & conic-gradient
How to style a progress bar using CSS
2022-06-12 · via CSS Articles by Temani Afif

How to style a progress bar using CSS

Written on by Temani AfifWeb Developer

Estimated read time 9 minutes

HTML offers a native element to show progression which is <progress>. In this post, we will learn a few CSS tricks to style such an element. We will also see how to make it look the same across browsers because the native element will, by default, render differently based on the browser.

First, let’s start by defining the HTML code:

<label>Progression: <progress value="70" max="100">70 %</progress></label>

We consider a <label> where we put the <progress> element and a relevant text that reflects the progression we want to highlight. Then, we define two attributes for our element. The max value which is, in most of the cases, equal to 100 to indicate 100% then the value that indicates the progress and it should be within the range [0 max]

There is no specific requirement or restriction on the value we should use. We can for example consider

<label>Progression: <progress value=".7" max="1">70 %</progress></label>

You may notice the text I am adding inside the progress as well (70%). This one is not mandatory but recommended as a fallback in case we are using an old browser that doesn’t support the <progress> element. In 2022, it’s quite rare to deal with such browsers but it’s always good to have a good coverage even for very old browsers.

Before we deal with CSS it should be noted that the implementation of <progress> element is different for each browser in the sense that its inner HTML structure is different. We mainly have 2 structures.

The one used by Firefox:

<progress>
  <div pseudo="-moz-progress-bar"></div>
</progress>

And the one used by Webkit and blink browsers such as Chrome, Safari, and Opera:

<progress>
 <div pseudo="-webkit-progress-inner-element">
   <div pseudo="-webkit-progress-bar">
     <div pseudo="-webkit-progress-value"></div>
   </div>
 </div>
<progress>

Such a difference is one reason that makes styling the progress element a tedious task as we have to write a style that matches both structures.

UP TO 70% OFF ALL VERPEX RESELLER HOSTING PLANS

with the discount code

AWESOME

How to make a horizontal progress bar

The first thing to do is to disable the default browser styles by using

progress[value] {
  -webkit-appearance:none;
  -moz-appearance:none;        
  appearance: none;
}

You may wonder why I am adding [value] to the selector. It’s because the progress element without the value attribute will not indicate a progression and we are not interested in such behavior in our article:

If there is no value attribute, the progress bar is indeterminate; this indicates that an activity is ongoing with no indication of how long it is expected to take. ref

Then we will style the element for the Firefox browser using:

progress[value] {
  --color: blue;  /* the progress color */
  --background: lightgrey; /* the background color */
  
  border: none; /* Firefox add a default border */
  width: 200px;
  margin: 0 10px;
  border-radius: 10em;
  background: var(--background);
}
progress[value]::-moz-progress-bar {
  border-radius: 10em;
  background: var(--color);
}

The code should be self-explanatory. The main element will take the gray coloration defined with --background while the progress bar will get the blue color defined with --color. Everything else is basic CSS to give some dimension, rounded corners, and margin.

For the Webkit and blink browsers, we will do the same but with different selectors

progress[value] {
  --color: blue; /* the progress color */
  --background: lightgrey; /* the background color */

  width: 200px;
  margin: 0 10px;
}
progress[value]::-webkit-progress-bar {
  border-radius: 10em;
  background: var(--background);
}
progress[value]::-webkit-progress-value {
  border-radius: 10em;
  background: var(--color);
}

Finally, we put all the styles together and we are done!

progress[value] {
  --color: blue; /* the progress color */
  --background: lightgrey; /* the background color */

  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  border: none;
  width: 200px;
  margin: 0 10px;
  border-radius: 10em;
  background: var(--background);
}
progress[value]::-webkit-progress-bar {
  border-radius: 10em;
  background: var(--background);
}
progress[value]::-webkit-progress-value {
  border-radius: 10em;
  background: var(--color);
}
progress[value]::-moz-progress-bar {
  border-radius: 10em;
  background: var(--color);
}

Now that we know how to style the <progress> element let’s go further and make it look fancier. To do this, I will simply update coloration by adjusting the --color variable.

Let’s do the following:

--color: 
    linear-gradient(#fff8,#fff0),
    repeating-linear-gradient(135deg,#0003 0 10px,#0000 0 20px),
    #31c6f7;

How to make a horizontal progress bar

By using a gradient coloration instead of a simple color we get another great looking for our element. You can imagine all the possibilities we can have.

Here is another idea:

How to make a horizontal progress bar

For the above, I have introduced a new variable to define the width that I am also using to define the gradient dimension.

We can also have a dynamic coloration where the color change based on the value of the progress:

How to make a horizontal progress bar

Thanks to the use of calc() combined with the variable --w we can express a condition based on the value to have a dynamic coloration. I have considered three colors but we can easily extend to more.

Another idea where I will add a mask to get another fancy progress bar:

How to make a horizontal progress bar

40%

💰 40% OFF YOUR FIRST MONTH WITH VERPEX MANAGED HOSTING PLANS FOR WORDPRESS

with the discount code

SERVERS-SALE

How to make a circular progress bar

In this section, I am going to create two types of circular progress bars, still, with the same HTML structure used in the previous section.

Here is the result of the first type

How to make a horizontal progress bar

To achieve the above, we need to style the <label> to make a square element and place its content at the center.

label {
  --w: 150px; /* the width*/
  /* we use CSS grid to center the content */
  display: inline-grid;
  place-content: center;
  /* */
  width: var(--w);
  aspect-ratio: 1; /* make a square element */
  position: relative;
}

Note that I am also using position: relative because I am going to use position: absolute with the <progress> element.

progress[value] {
  --color:  /* the progress color */
    /* if < 30% "red" */
    linear-gradient(red    0 0) 0 /calc(var(--w)*.3 - 100%) 1px,
    /* if < 60% "orange" */
    linear-gradient(orange 0 0) 0 /calc(var(--w)*.6 - 100%) 1px,
    /* else "green" */
    green;
  --background: lightgrey; /* the background color */

  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  border: none;
  /* (1) */
  position: absolute;
  inset: 0;
  height: 100%;
  width: 100%;
  /* */
  border-radius: 50%;
  overflow: hidden;
  background: var(--background);
  transform: rotate(-90deg); /* (2) */
  -webkit-mask: radial-gradient(#0000 59%,#000 60% 70%,#0000 71%) /* (3) */
}
progress[value]::-webkit-progress-bar {
  background: var(--background);
}
progress[value]::-webkit-progress-value {
  background: var(--color);
}
progress[value]::-moz-progress-bar {
  background: var(--color);
}

A big part of the code is similar to what we already did in the first section of the article. For the rest of the code:

  1. We are making our element full width/height of the label

  2. We are adding a rotation to make the progression goes from bottom to top instead of left to right

  3. We are applying a mask to cut the inner part

Below is a figure to better understand the trick:

Circular progress bar

Let’s move to the second type which is the most common one:

How to make a horizontal progress bar

For this one, I am going to “cheat” a lit bit by modifying the HTML like the below:

<label> label <progress max="100" value="10" style="--p:10%">10%</progress></label>

I had to introduce an extra CSS variable defined as an inline style that should be equal to the progression. I will use that variable for the gradient configuration that will define the colors:

conic-gradient(#31c6f7 var(--p),lightgrey 0)

Note that for this type of progress we only need one value for the color applied to the main element. We are making the inner elements transparent:

progress[value] {
  --background: conic-gradient(#31c6f7 var(--p),lightgrey 0); 

  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  border: none;
  position: absolute;
  inset: 0;
  height: 100%;
  width: 100%;
  border-radius: 50%;
  overflow: hidden;
  background: var(--background);
  -webkit-mask: radial-gradient(#0000 59%,#000 60% 70%,#0000 71%)
}
progress[value]::-webkit-progress-bar {
  background: var(--background);
}
progress[value]::-webkit-progress-value {
  background: #0000; /* transparent */
}
progress[value]::-moz-progress-bar {
  background: #0000; /* transparent */
}

We can also consider another variation of the previous design. We remove the grey coloration and add rounded edges:

How to make a horizontal progress bar

For this one, I am defining the variable --p inside the label instead of the progress element because I will need to define a pseudo-element that will create one of the rounded edges. Note that it needs to be without the percentage unit this time:

<label style="--p:10"> label <progress max="100" value="10">10%</progress></label>

I also introduced a CSS variable for the color to easily update it. I also need that variable to make the color of the pseudo-element match the color of the gradient.

40%

💰 40% OFF YOUR FIRST MONTH WITH VERPEX MANAGED CLOUD SERVERS

with the discount code

SERVERS-SALE

Conclusion

Through different examples of progress components, we saw how to style the native <progress> element using only CSS and how to make it look the same cross-browser. Using the same minimal HTML code we can easily get a different result that we can easily control thanks to CSS variables and some modern CSS techniques.

Ready to host your next web project?

If you're building client sites or managing multiple projects, Verpex's reseller hosting gives you the speed, reliability, and control to handle it all from one place. White-label accounts, cPanel access, and 24/7 support included.

Frequently Asked Questions

Temani Afif

Temani Afif is an expert web developer, a content creator, and a CSS addict. He is the mastermind behind CSS Loaders, CSS Generators, CSS Tip and many other CSS-related websites.

View more posts by Temani Afif