【问题标题】:CSS background linear gradient bug using Chrome?使用 Chrome 的 CSS 背景线性渐变错误?
【发布时间】:2017-05-12 08:20:06
【问题描述】:

html {
  background:#ffffff;
}
body {
  height:250px;
  background: linear-gradient(
    to bottom,
    #ffffff 0px,
    #ffffff 100px,
    #0065A2 100px,
    #0065A2 145px,
    #074A8B 145px,
    #074A8B 163px,
    #0065A2 163px,
    #0065A2 203px,
    transparent 203px
  );
}

我正在尝试使用背景线性渐变,令人惊讶的是,它在 Firefox 和 IE 上运行良好,但在 Google Chrome 上却不行。

代码在这里例如:https://jsfiddle.net/be1rgpez/1/

background: linear-gradient(
    to bottom,
    #ffffff 0px,
    #ffffff 100px,
    #0065A2 100px,
    #0065A2 145px,
    #074A8B 145px,
    #074A8B 163px,
    #0065A2 163px,
    #0065A2 203px,
    transparent 203px
);

我需要一个带有多个色标的线性渐变,但使用 Google Chrome 会在颜色之间呈现奇怪的阴影(见左图)。我需要的效果是没有阴影的“条纹”)。

在附件中,我展示了我使用 Chrome 看到的内容。 左边的方框是我需要的,但没有阴影(就像右边的方框一样)。相同的 jsfiddle 在 Firefox 和 IE 上正确呈现。

更新:这是一张放大的图片。如您所见,左侧框在白色和蓝色之间(以及其他颜色之间)有一个小阴影。

【问题讨论】:

  • 我对渐变不太了解,但是如果你使用 % 而不是 px 就可以了。
  • @Pete 在左框中的图片中显示了颜色之间的小阴影。我需要右框的效果,颜色(白色和蓝色)之间没有阴影。但我需要更多颜色(白色、蓝色、深蓝色......)。当使用超过 2 种颜色(或 2 条条纹)时,Google chrome 似乎会呈现那个小阴影。

标签: css google-chrome gradient linear-gradients


【解决方案1】:

试试这个代码:

.left {
    background: linear-gradient( to bottom, #ffffff 0px, #ffffff 100px, #0065A2 100px);
    background: -webkit-linear-gradient( to bottom, #ffffff 0px, #ffffff 100px, #0065A2 100px);
}

.right {
    background: linear-gradient( to bottom, #ffffff 0px, #ffffff 100px, #0065A2 100px);
    background: -webkit-linear-gradient( to bottom, #ffffff 0px, #ffffff 100px, #0065A2 100px);
}

【讨论】:

  • 此代码只有 1 种颜色变化(从 #ffffff 到 #0065A2),它适用于 chrome。但我需要更多的颜色变化:#ffffff、#0065A2、#074A8B,然后是#0065A2,最后是透明的。几种颜色在 chrome 上似乎有错误的行为(就像我在问题中写的那样)。
【解决方案2】:

我发现线性渐变容器溢出之间存在问题。 我尝试了很多解决方案,但都没有奏效。

然后,当我尝试提供 overflow:auto 时,它对我有用。

this is before i apply the fix to the right container which holds many content

this is after applying the overflow: auto !important; to the right container 现在它工作正常,没有任何问题,就在这里。

【讨论】:

    【解决方案3】:

    您已经为不同的颜色定义了两次相同的起点。下面没有重复的代码看起来很好:

    .left {
         background: linear-gradient(
            to bottom,
            #ffffff 0px,
            #0065A2 100px,
            #074A8B 145px,
            #0065A2 163px,
            transparent 203px
        );
    }
    

    【讨论】:

    • 此代码生成渐变。我需要带有净颜色变化的条纹。不需要渐变。这就是为什么我使用相同的像素“停止”:所以我可以在没有平滑/渐变效果的情况下改变颜色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多