Lorem Ipsum
Lorem Ipsum

Reputation: 357

Clipping CSS linear gradient based on width percentage

I applied linear gradient for on a progress bar. The problem with it is that the gradient is same even if the percentage is lower or higher.

What I want to achieve is, the more the percentage is the more intense it should get to the right. So in other words, the full gradient should be for 100% and for 50% percent only 50% of the gradient should be shown. I don't want to create multiple gradients to solve this rather I tried to solve this with clip-path but got no success. Any help would be appreciated.

Here is my current code https://codepen.io/taimursaeed/pen/gOrLNyv

body {
  font-size: 30px;
}

.progress-custom {
  display: flex;
  align-items: center;
  margin-bottom: 0.5em;
}

.progress {
  display: flex;
  height: 1rem;
  overflow: hidden;
  font-size: 0.75rem;
  background-color: #e9ecef;
  border-radius: 0.25rem;
  height: 3em;
  font-size: 0.6em;
  flex: 1;
}

.progress-bar {
  display: flex;
  flex-direction: column;
  justify-content: center;
  overflow: hidden;
  color: #fff;
  text-align: center;
  white-space: nowrap;
  background-color: #007bff;
  transition: width 0.6s ease;
  background: linear-gradient(90deg, #ff9800 0%, #ff5722 100%);
}

.progress-custom .progress-value {
  font-size: 0.8em;
  padding: 0 0.5em;
}
<div class="progress-custom">
  <div class="progress">
    <div role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" class="progress-bar" style="width: 70%;">
      My Progress
    </div>
  </div>
  <span class="progress-value"> 70%</span>
</div>
<div class="progress-custom">
  <div class="progress">
    <div role="progressbar" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100" class="progress-bar" style="width: 50%;">
      My Progress
    </div>
  </div>
  <span class="progress-value"> 50%</span>
</div>
<div class="progress-custom">
  <div class="progress">
    <div role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100" class="progress-bar" style="width: 20%;">
      My Progress
    </div>
  </div>
  <span class="progress-value"> 20%</span>
</div>

Upvotes: 1

Views: 1662

Answers (2)

black blue
black blue

Reputation: 819

Add one line to .progress-bar

.progress-bar{ background-attachment: fixed}

I believe that for a spectacular effect, the colours should be more varied - depending on the lightning, similar colours may look the same.
In the snippet I changed them - use the colours according to your own taste, not mine :)

div{
box-sizing: border-box;
margin: 20px auto 20px 0;
padding: 20px;
color: #fff;
text-align: center;
background: linear-gradient(to right, gold, red);
background-attachment: fixed}
<div style="width: 20%;">20%</div>
<div style="width: 50%;">50%</div>
<div style="width: 70%;">70%</div>
<div style="width: 99%;">99%</div>


Look at my snippet. Your gradient has two almost identical colours and difference won't be much visible, because colours are too similar.

Another option came to mind - gradient with transparency:

background: linear-gradient(to right, rgba(200,200,0,0.2), rgba(255,0,0,1.0));

Upvotes: 3

I used clip-path and padding-right. I didn't use pseudo or extra element.

body {
  font-size: 30px;
  width: 350px;
  margin: 50px auto;
}

.progress-custom {
  display: flex;
  align-items: center;
  margin-bottom: 0.5em;
}

.progress {
  display: flex;
  height: 1rem;
  overflow: hidden;
  font-size: 0.75rem;
  background-color: #e9ecef;
  border-radius: 0.25rem;
  height: 3em;
  font-size: 0.6em;
  flex: 1;
  position: relative;
}

.progress-bar {
  display: flex;
  flex-direction: column;
  justify-content: center;
  overflow: hidden;
  color: #fff;
  text-align: center;
  white-space: nowrap;
  transition: width 0.6s ease;
  overflow: hidden;
  background: linear-gradient(to right, gold, red);
  padding-right:calc(100% - var(--width));
  width: 100%;
  height: 100%;
  -webkit-clip-path: inset(0 calc(100% - var(--width)) 0 0);
  clip-path: inset(0 calc(100% - var(--width)) 0 0);
}

.progress-custom .progress-value {
  font-size: 0.8em;
  padding: 0 0.5em;
}
<div class="progress-custom">
  <div class="progress">
    <div role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" class="progress-bar" style="--width: 70%;">
      My Progress
    </div>
  </div>
  <span class="progress-value"> 70%</span>
</div>
<div class="progress-custom">
  <div class="progress">
    <div role="progressbar" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100" class="progress-bar" style="--width: 50%;">
      My Progress
    </div>
  </div>
  <span class="progress-value"> 50%</span>
</div>
<div class="progress-custom">
  <div class="progress">
    <div role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100" class="progress-bar" style="--width: 20%;">
      My Progress
    </div>
  </div>
  <span class="progress-value"> 20%</span>
</div>

Upvotes: 0

Related Questions