如何在 Safari 中实现径向渐变?

Nea*_*eal 2 css safari radial-gradients css-gradients

我在我正在构建的新网站上使用径向渐变,但颜色在桌面上的 Safari 中呈现不同(更暗)。有没有更好的跨浏览器语法可供使用?

我尝试了不同的前缀,但这并没有解决问题。我正在使用的代码如下。

.item1 {
  background: -webkit-radial-gradient( bottom left, farthest-side, rgba(218, 218, 216, 0.5), transparent), -webkit-radial-gradient( bottom right, farthest-corner, rgba(253, 253, 253, 0.5), transparent 300px);
  background: -o-radial-gradient( bottom left, farthest-side, rgba(218, 218, 216, 0.5), transparent), -o-radial-gradient( bottom right, farthest-corner, rgba(253, 253, 253, 0.5), transparent 300px);
  background: radial-gradient( farthest-side at bottom left, rgba(218, 218, 216, 0.5), transparent), radial-gradient( farthest-corner at bottom right, rgba(253, 253, 253, 0.5), transparent 300px);
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
Run Code Online (Sandbox Code Playgroud)

当前在 Chrome 和 Firefox 中看到的正确输出: https: //i.stack.imgur.com/ej9pO.jpg

Safari 中的输出:https://i.stack.imgur.com/KYa13.jpg

如您所见,Safari 中的颜色要暗得多。

有人对如何解决这个问题有任何想法吗?

Tem*_*fif 6

首先,您的渐变可以像下面这样简化。

.box{
 background: 
  radial-gradient( farthest-side at bottom left, rgba(218, 218, 216, 0.5), transparent), 
  radial-gradient( farthest-corner at bottom right, rgba(253, 253, 253, 0.5), transparent 300px);
 height:200px;
}

body {
 background:blue;
}
Run Code Online (Sandbox Code Playgroud)
<div class="box">

</div>
Run Code Online (Sandbox Code Playgroud)

现在的问题是 safari 不支持使用 with 的语法,正如您在MDN 页面at中看到的那样:

在此输入图像描述

您可以像下面这样更改语法。我将分割渐变以更好地查看结果,然后您可以轻松地将它们组合起来。

第一个梯度:

.box{
 background: 
  radial-gradient( farthest-side at bottom left, rgba(218, 218, 216, 0.5), red);
 height:200px;
}

.box2{
 background: 
  radial-gradient( farthest-side, rgba(218, 218, 216, 0.5), red) top right/200% 200%;
 height:200px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="box">

</div>
<div class="box2">

</div>
Run Code Online (Sandbox Code Playgroud)

第二个梯度

.box{
 background:  
  radial-gradient( farthest-corner at bottom right, rgba(253, 253, 253, 0.5), red 300px);
 height:200px;
}

.box2{
 background:  
  radial-gradient( farthest-corner, rgba(253, 253, 253, 0.5), red 300px) top left/200% 200%;
 height:200px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="box">

</div>
<div class="box2">

</div>
Run Code Online (Sandbox Code Playgroud)

技巧是,如果您删除at,渐变将默认从中心开始,当从中心开始时,我们需要一段X距离才能到达角或边,这与从角开始时需要两倍的距离不同X。 。这就是为什么我将渐变的大小设置为200% 200%,并且我只需调整背景位置以获得相同的视觉效果。

这是最终的背景:

.box{
 background: 
  radial-gradient( farthest-side, rgba(218, 218, 216, 0.5) , transparent) top right/200% 200%, 
  radial-gradient( farthest-corner, rgba(253, 253, 253, 0.5), transparent 300px) top left/200% 200%;
 height:200px;
}

body {
 background:blue;
}
Run Code Online (Sandbox Code Playgroud)
<div class="box">

</div>
Run Code Online (Sandbox Code Playgroud)

相关问题:如何使用 CSS 制作径向渐变动画?