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

推荐订阅源

T
Threatpost
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
D
DataBreaches.Net
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
Netflix TechBlog - Medium
Microsoft Azure Blog
Microsoft Azure Blog
Microsoft Security Blog
Microsoft Security Blog
B
Blog
U
Unit 42
有赞技术团队
有赞技术团队
博客园 - 聂微东
GbyAI
GbyAI
宝玉的分享
宝玉的分享
F
Full Disclosure
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MyScale Blog
MyScale Blog
Jina AI
Jina AI
Martin Fowler
Martin Fowler
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Docker
P
Proofpoint News Feed
A
About on SuperTechFans
I
InfoQ
博客园 - 【当耐特】
C
Check Point Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog
Project Zero
Project Zero
WordPress大学
WordPress大学
小众软件
小众软件
AWS News Blog
AWS News Blog
博客园 - 司徒正美
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 热门话题
I
Intezer
Engineering at Meta
Engineering at Meta
C
CXSECURITY Database RSS Feed - CXSecurity.com
J
Java Code Geeks
T
Tenable Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes

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 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 Single Element Loaders: The Bars | CSS-Tricks
A CSS-only responsive Stepper component
2023-02-09 · via CSS Articles by Temani Afif

A CSS-only responsive Stepper component

Written on by Temani AfifWeb Developer

Estimated read time 5 minutes

In this article, we will build a component commonly used everywhere especially when it comes to multi-step forms: The stepper component. If you are not familiar with such a component here is a figure to give you an idea.

Overview of a stepper component

The goal is to use the smallest HTML code possible and at the same time make the component scalable. You will be able to add as many steps as you want without the need to touch the CSS code. We will also make it responsive and the “active” steps can be controlled easily.

The HTML markup

Let’s start with the HTML code which is a list defined using the <ol> element:

<ol class="stepper">
   <li>Step A</li>
   <li class="active">Step B</li>
   <li>Step C</li>
   <li>Step D</li>
</ol>

We don’t need to clutter our code with a lot of divs. A simple list can do the job. Also, note the use of the active class on the second element that defines the current or active step. This class will control the coloration as you can see in the first figure where both Step A and Step B are in purple (we will call it the “active” coloration) and the other one in grey (we will call it the “default” coloration).

Defining the variables

Let’s define a few variables to easily control our stepper.

ol.stepper {
  --default-b: lightgrey;
  --default-c: black;
  --active-b: purple;
  --active-c: white;
  --circle: 3.5em; /* size of circle */
  --b: 5px; /* line thickness */
}

The code should be self-explanatory. We have the color variables and the ones that control the size of the circle and the line.

40%

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

with the discount code

SERVERS-SALE

Styling the Stepper

Here, we are going to rely on flexbox and the use of space-between to correctly place the steps inside the stepper:

ol.stepper {
  display: flex;
  list-style: none;
  justify-content: space-between;
  margin: 20px;
  padding: 0;
  font-size: 22px;
}

This configuration allows us to have as many steps as we want without having to adjust the position of each one. The space-between will do the job for us

You are probably wondering why we removed the numbers using list-style: none. It’s because we cannot style the default numeration so we will disable it and replace it with a custom one using counter.

ol.stepper {
  counter-reset: step;
}
ol.stepper li::before {
  content: counter(step) " ";
  counter-increment: step;
}

Styling the steps

For the steps we will use a CSS grid to have a 2-rows configuration plus some basic CSS to style the numbers:

Overview of one default step

ol.stepper li {
  display: grid;
  place-items: center;
  gap: 5px;
}
ol.stepper li::before {
  content: counter(step) " ";
  counter-increment: step;
  display: grid;
  place-content: center;
  height: var(--circle);
  aspect-ratio: 1;
  background: var(--active-b);
  color: var(--active-c);
  border: 5px solid #fff;
  box-sizing: border-box;
  border-radius: 50%;
}
ol.stepper li.active ~ li::before{
  background: var(--default-b);
  color: var(--default-c);
}

The tricky part here is how we handle the coloration. All the items will have the “active" coloration but using the sibling selector ~ we make all the elements that are placed after the active element with the “default” coloration. This way, we only need to place the active class on the active/current step and the CSS will do the job for us.

See the Pen by Temani Afif () on .

We are almost done! All that is missing is the line between the elements and to do this we will first define a background on the stepper element to create the line with the default coloration

background: 
    linear-gradient(var(--default-b) 0 0) no-repeat
    50% calc((var(--circle) - var(--b))/2)/100% var(--b);

Note the use of the different variables we defined previously. This will create a line with a thickness equal to --b and a width equal to 100%. For the position, it needs to be placed at the center of the circle so we offset it from the top by half the circle size minus half the line size.

The above will create a line with only the “default" coloration. We still need a line with the “active” coloration and for this one, we will use the pseudo-element of the active element

ol.stepper li.active::after {
  content: "";
  position: absolute;
  height: var(--b);
  right: 100%;
  top: calc((var(--circle) - var(--b))/2); /* same offset as the background */
  width: 100vw; /* very big value */
  background: var(--active-b);
}

This will create a very big line placed at the right of the active line with the “active” coloration that will overlap the previous one. We also need to hide the overflow on the main element since we are using a big value that will create an overflow. Don’t forget to adjust the z-indexes if needed as well.

Here is the demo with the full code:

Our stepper component is now perfect!

Note the small gap between the lines and the circle. It’s the use of the white border so if you have a different background color you need to adjust the color to match the one of your background.

UP TO 70% OFF ALL VERPEX RESELLER HOSTING PLANS

with the discount code

AWESOME

Making the Stepper responsive

To make our stepper responsive we will switch from a horizontal configuration to a vertical one.

The vertical version of the Stepper

For this, we are going to use a media query and adjust a few values

@media (max-width: 600px) { 
  ol.stepper {
   display: grid;
   gap: 20px;
   background: 
     linear-gradient(var(--default-b) 0 0) no-repeat
     calc((var(--circle) - var(--b))/2) 50%/var(--b) 100%;
  }
  ol.stepper li {
    display: flex;
  }
  ol.stepper li.active::after {
    content: "";
    position: absolute;
    width: var(--b);
    bottom: 100%;
    left: calc((var(--circle) - var(--b))/2);
    top: auto;
    right: auto;
    height: 100vw;
    background: var(--active-b);
  }
}

We switch from flexbox to CSS grid on the main element to move from one row of elements to one element per row. We apply the same logic on the step element by using flexbox instead of CSS grid to do the opposite.

For the lines, you will notice that in the background property, we kept the same values but the order is different. What we have used for the width is now a height and vice versa. Same logic for the pseudo element. What we used for right is used with bottom, width becomes height, top becomes left, and so on.

Here is the full demo:

The vertical version of the Stepper

50%

💰 EXTRA 50% OFF ALL VERPEX YEARLY MANAGED CLOUD SERVER PLANS

with the discount code

SERVERS-SALE

Conclusion

I hope you enjoyed this step-by-step tutorial to create a stepper component. Using a basic HTML code, all you have to do is to update a few variables and place the active class on the current element and you are done!

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