Safari 使用白色/白色呈现透明线性渐变

Safari renders transparent linear gradient with white / white-ish color

我尝试了很多解决方案都没有成功。

我想创建一个从左到右的线性渐变,从透明开始到黑色结束。

.box1 {
  border: solid 1px #ccc;
  margin: 10px;
  max-width: 500px;
  height: 40px;
  position: relative;
  font-size: 30px;
  white-space: nowrap;
  overflow: hidden;
  background-color: #2d2d2d;
  color: red;
}
.box1::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-image: linear-gradient(
    to right, rgba(255,255,255,0.01), #2d2d2d
  );
}

.box2 {
  border: solid 1px #ccc;
  margin: 10px;
  max-width: 500px;
  height: 40px;
  position: relative;
  font-size: 30px;
  white-space: nowrap;
  overflow: hidden;
  background-color: #2d2d2d;
  color: red;
}
.box2::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  width: 100px;
  background-image: linear-gradient(
    to right, rgba(255,255,255,0), #2d2d2d
  );
}

.box3 {
  border: solid 1px #ccc;
  margin: 10px;
  max-width: 500px;
  height: 40px;
  position: relative;
  font-size: 30px;
  white-space: nowrap;
  overflow: hidden;
  background-color: rgba(45,45,45,1);
  color: red;
}
.box3::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  width: 100px;
  background: linear-gradient(to right, rgba(255,255,255,0) 0%,rgba(45,45,45,1) 100%);
}

.fade1 {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  width: 75px;
  background: linear-gradient(
    to right, rgba(255,255,255,0), black
  );
}

.box {
  border: solid 1px #ccc;
  margin: 10px;
  max-width: 500px;
  height: 40px;
  position: relative;
  font-size: 30px;
  white-space: nowrap;
  overflow: hidden;
  background-color: black;
  color: white;
}
<div class="box1">
</div>
<div class="box2">
</div>
<div class="box3">
</div>

<div class="box">
  <span class="fade1"></span>
</div>

这里还有一个codepen给你。

Chrome 以我想要的方式呈现它,但在 Safari 上结果不一样。它不是透明的 "color",而是白色或略带白色的颜色。

我知道 transparent 关键字在 Safari 上有不同的行为,所以我根本不使用它。

你们有解决办法吗?

好的。事实证明,在 Safari 上,透明颜色应该与结束颜色具有相同的 rgb。

所以:

  • 如果结束颜色为黑色,则起始颜色应为 rgba(0,0,0,0)
  • 如果结束颜色是红色,起始颜色应该是rgba(255,0,0,0)

.fade1 {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  width: 75px;
  background: linear-gradient(
    to right, rgba(0,0,0,0), black
  );
}

.box {
  border: solid 1px #ccc;
  margin: 10px;
  max-width: 500px;
  height: 40px;
  position: relative;
  font-size: 30px;
  white-space: nowrap;
  overflow: hidden;
  background-color: black;
  color: white;
}
<div class="box">
  adsfasdfsdafasdfdasfdasfasdfdasfdasfdsfdasf
  <span class="fade1"></span>
</div>