Alireza Fattahi Alireza Fattahi - 3 months ago 73
CSS Question

Using css calc() with step

We have used a pure css base progress bar.

The main css part is as below.

.container {
width: 600px;
margin: 20px auto;
}

.progressbar {
margin: 0;
padding: 0;
counter-reset: step;
}

.progressbar li {
list-style-type: none;
width: 25%;
float: left;
font-size: 12px;
position: relative;
text-align: center;
text-transform: uppercase;
color: #7d7d7d;
}

.progressbar li:before {
width: 30px;
height: 30px;
content: counter(step);
counter-increment: step;
line-height: 30px;
border: 2px solid #7d7d7d;
display: block;
text-align: center;
margin: 0 auto 10px auto;
border-radius: 50%;
background-color: white;
}

.progressbar li:after {
width: 100%;
height: 2px;
content: '';
position: absolute;
background-color: #7d7d7d;
top: 15px;
left: -50%;
z-index: -1;
}

...............


The html

<div class="container">
<ul class="progressbar">
<li class="active">login</li>
<li>choose interest</li>
........


Complete sample could be found at https://jsfiddle.net/wbj7e79p/.

As you can see it mess up for seven step. The reason is
.progressbar li width
which is fixed to
25%
we wanted to make it dynamic base on number of steps.

So we tried width :
calc (100% / steps)
or
calc (100% / counter(steps))
but none of them worked. Any idea !




Please consider that we are building a component which build a progress bar on the fly, so we can not find the actual number of steps

Answer

Did you consider flexbox?

body {
  font-family: 'Alegreya Sans', sans-serif;
  margin: 0;
  padding: 0;
}
.container {
  margin: 20px auto;
}
.progressbar {
  margin: 0;
  padding: 0;
  counter-reset: step;
  display: flex;
}
.progressbar li {
  list-style-type: none;
  flex: 1;
  font-size: 12px;
  position: relative;
  text-align: center;
  text-transform: uppercase;
  color: #7d7d7d;
}
.progressbar li:before {
  width: 30px;
  height: 30px;
  content: counter(step);
  counter-increment: step;
  line-height: 30px;
  border: 2px solid #7d7d7d;
  display: block;
  text-align: center;
  margin: 0 auto 10px auto;
  border-radius: 50%;
  background-color: white;
}
.progressbar li:after {
  width: 100%;
  height: 2px;
  content: '';
  position: absolute;
  background-color: #7d7d7d;
  top: 15px;
  left: -50%;
  z-index: -1;
}
.progressbar li:first-child:after {
  content: none;
}
.progressbar li.active {
  color: green;
}
.progressbar li.active:before {
  border-color: #55b776;
}
.progressbar li.active + li:after {
  background-color: #55b776;
}
<h1>Four Steps</h1>
<div class="container">
  <ul class="progressbar">
    <li class="active">login</li>
    <li>choose interest</li>
    <li>add friends</li>
    <li>View map</li>
  </ul>
</div>
<h1> Seven Steps</h1>

<div class="container">
  <ul class="progressbar">
    <li class="active">login</li>
    <li>choose interest</li>
    <li>add friends</li>
    <li>remove</li>
    <li>fix users</li>
    <li>review</li>
    <li>save all</li>
  </ul>
</div>