根据宽度百分比裁剪CSS线性渐变

Clipping CSS linear gradient based on width percentage

我在进度条上应用了线性渐变。它的问题是即使百分比更低或更高,梯度也是相同的。

我想要实现的是,百分比越多,它应该越向右。 所以换句话说,完整的渐变应该是 100%,而对于 50%,应该只显示 50% 的渐变。我不想创建多个渐变来解决这个问题,而是尝试使用 clip-path 解决这个问题,但没有成功。任何帮助将不胜感激。

这是我当前的代码 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>

我用了clip-pathpadding-right。我没有使用伪元素或额外元素。

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>

加一行到.progress-bar

.progress-bar{ background-attachment: fixed}

我认为为了达到壮观的效果,颜色应该更加多样化 - 根据闪电的不同,相似的颜色可能看起来相同。
在代码片段中,我更改了它们 - 根据您自己的口味使用颜色,而不是我的:)

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>


看看我的代码片段。您的渐变有两种几乎相同的颜色,差异不会太明显,因为颜色太相似了。

想到另一个选项 - 透明渐变:

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