Safari呈现带有白色/白色的透明线性渐变

时间:2018-11-07 13:29:42

标签: html css safari linear-gradients

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

我想创建一个从左到右的线性渐变,该渐变以透明开头并以黑色结尾。

.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上结果却不一样。它不是透明的“颜色”,而是白色或白色。

enter image description here

我知道透明关键字在Safari上的行为有所不同,因此我完全不使用它。

你们有解决方案吗?

1 个答案:

答案 0 :(得分:0)

好的。事实证明,在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>