构建一个4角色CSS3渐变

500*_*500 18 css css3 web

是否可以在CSS中生成以下渐变:

在此输入图像描述

Gil*_*mbo 36

在你的情况下

在此输入图像描述

方法1:

jsFiddle演示

div{
overflow: hidden;
background: #f06;
background: linear-gradient(45deg, #fff722, #ff26f9);
min-height: 100%;
width: 256px;
height: 256px;
position: relative;
z-index: 1;
box-shadow: inset -20px 0 38px -18px #ff26f9,inset -3px -13px 65px -18px yellow;
}

div:before,div:after{
    content:'';
    position:absolute;
    width:100%;
    height:100%;
}
div:before{
    background: red;
    box-shadow: 0 0 140px 64px red;
    z-index:2;
    top: -96%;
left: -72%;
    opacity: 0.8;
}
div:after {
background: white;
z-index: 3;
bottom: -96%;
right: -72%;
box-shadow: 0 0 140px 64px white;
opacity: 1;
border-radius: 100%;
}
Run Code Online (Sandbox Code Playgroud)

方法2:

在此输入图像描述

div{
overflow: hidden;
background: #f06;
background: linear-gradient(45deg, #fff722, #ff26f9);
min-height: 100%;
width:256px;
height:256px;  
position:relative;
    z-index:1;
}

div:before,div:after{
    content:'';
    position:absolute;
    width:100%;
    height:100%;
}
div:before{
    background: red;
    box-shadow: 0 0 140px 64px red;
    z-index:2;
    top: -96%;
left: -72%;
    opacity: 0.8;
}
div:after {
background: white;
z-index: 3;
bottom: -96%;
right: -72%;
box-shadow: 0 0 140px 64px white;
opacity: 1;
border-radius: 100%;
}
Run Code Online (Sandbox Code Playgroud)

jsFiddle演示

方法3:多个背景:

在此输入图像描述

div{
background: #f06;
background: linear-gradient(45deg, #fff722, #ff26f9),linear-gradient(142deg, transparent, white),linear-gradient(108deg, red, transparent);
min-height: 100%;
width:256px;
height:256px;  
position:relative;
    z-index:1;
}
Run Code Online (Sandbox Code Playgroud)

jsFiddle演示

方法4:伪元素

div{
background: #f06;
background: linear-gradient(45deg, #fff722, #ff26f9);
min-height: 100%;
width:256px;
height:256px;  
position:relative;
    z-index:1;
}

div:before,div:after{
    content:'';
    position:absolute;
    width:100%;
    height:100%;
    opacity: 0.8;
}
div:before{
    background: linear-gradient(108deg, red, transparent);
    z-index:2;
    top:0;
    left:0;
}
div:after{
    background: linear-gradient(142deg, transparent, white);
    z-index:3;
    bottom:0;
    right:0;
}
Run Code Online (Sandbox Code Playgroud)

标记:

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

jsFiddle演示

方法5:

div{
overflow: hidden;
background: #f06;
background: linear-gradient(45deg, #fff722, #ff26f9);
min-height: 100%;
width:256px;
height:256px;  
position:relative;
    z-index:1;
}

div:before,div:after{
    content:'';
    position:absolute;
    width:100%;
    height:100%;
}
div:before{
    background: linear-gradient(108deg, red, transparent);
    z-index:2;
    top:0;
    left:0;
    opacity: 0.8;
}
div:after {
background: white;
z-index: 3;
bottom: -96%;
right: -72%;
box-shadow: 0 0 110px 54px white;
opacity: 1;
border-radius: 100%;
}
Run Code Online (Sandbox Code Playgroud)

jsFiddle演示

更新:非常感谢Ana-Maria Tudor <3

body{
position:fixed;
top:0;
right:0;
bottom:0;
left:0;
}

body:before {
  content: '';
  position:absolute;
  top:0;
  right:0;
  bottom:0;
  left:0;
  display: block;
  width: 100%;
  height: 600px;
  border-radius: 0%;
  background:
    radial-gradient(circle at 50% 0, 
         rgba(255,0,0,.5), rgba(255,0,0,0) 70.71%), 
    radial-gradient(circle at 6.7% 75%, 
         rgba(0,0,255,.5), rgba(0,0,255,0) 70.71%), 
    radial-gradient(circle at 93.3% 75%, 
         rgba(0,255,0,.5), rgba(0,255,0,0) 70.71%);
}
Run Code Online (Sandbox Code Playgroud)

jsFiddle演示


Kam*_*ski 7

简单的

只需background为您的div元素使用此样式:

.myDiv {
  width: 256px;
  height: 256px;
  background: linear-gradient(to top left, white, rgba(255, 153, 150, 0), red), linear-gradient(to top right, yellow, rgba(255, 153, 150, 0), magenta) rgba(255, 153, 150, 1);
}
Run Code Online (Sandbox Code Playgroud)
<div class="myDiv"></div>
Run Code Online (Sandbox Code Playgroud)

中心rgba(255, 153, 150, _ )的颜色在哪里div,我们在底部使用 a=1 并在 a=0 的渐变中使用它以实现 Safari 兼容性(对于渐变中的其他浏览器,我们可以更改rgba(255, 153, 150, 0)transparetn 50%)。

在这里工作,易于编辑示例。想法来源在这里

中等的

我们需要添加和样式 div:after伪元素:

.myDiv {
  width: 256px;
  height: 256px;
  background: linear-gradient(to bottom, red, yellow);
}

.myDiv::after {
  content: "";
  position: absolute;
  width: inherit;
  height: inherit;
  background: linear-gradient(to bottom, magenta, white);
  -webkit-mask-image: linear-gradient(to left, white, transparent);
}
Run Code Online (Sandbox Code Playgroud)
<div class="myDiv"></div>
Run Code Online (Sandbox Code Playgroud)

在这里工作,易于编辑示例。来自TheDarkln 答案的想法(我制作了纯 css 版本)。

复杂的

.myDivC{
    overflow: hidden;
    background: linear-gradient(45deg, yellow, magenta);
    width:256px; height:256px;  
    position:relative;
    z-index:1;
}

.myDivC:before,.myDivC:after{
    content:'';
    position:absolute;
    width:100%;
    height:100%;
}
.myDivC:before{
    background: red;
    box-shadow: 0 0 140px 64px red;
    z-index:2;
    top: -96%;
    left: -72%;
    opacity: 0.8;
}
.myDivC:after {
    background: white;
    z-index: 3;
    bottom: -96%;
    right: -72%;
    box-shadow: 0 0 100px 64px white;
    opacity: 1;
    border-radius: 100%;
}
Run Code Online (Sandbox Code Playgroud)
<div class="myDivC"></div>
Run Code Online (Sandbox Code Playgroud)

框阴影和更多伪元素 -在此处易于编辑代码。从理念Gildas.Tambo答案(我选择第二种方案-首先,对左下角的文物“黑影子”,其他解决方案不适用于边缘)。

测试

经过测试版本:

  • 简单 - 在Safari(低但可接受的质量)、ChromeFirefoxEdge 上

  • 中等 - 在SafariChromeFirefox 上(不适用于Edge)。

  • 复杂 - ChromeEdge。在Safari浏览器火狐对左上角的“红盒子”假象-这可以通过改变将减少.myDivC:before{ ... top: -96% ...}top: -100%,但我们一点(松红色强度这里

以下是 3 个版本在 chrome 上的比较:

在此处输入图片说明

在简单的解决方案中,我们看到更多的“线性”,介质具有最好的质量。复杂的质量较低:左上角的不对称和人工制品红色矩形 - 当我们在所有解决方案中将黄色更改为黑色时,这可以更清楚地看到 -在这里

在此处输入图片说明

结论

  • 简单版本 - 可以在任何地方使用,但比其他解决方案(尤其是在 Safari 上)的(线性)质量稍低
  • 中等版本 - 提供最佳质量,但不适用于 Edge
  • 复杂版本 - 适用于任何地方,质量比简单版本好但比中等版本差,但是代码非常不方便,需要更多时间来准备、测试和维护

更新

最近我开发了具有以下两种特性的解决方案:高质量和便携性 -在这里


Kam*_*ski 7

高品质、便携

该答案中,我比较了三种解决方案 - 但我发现了第四种解决方案,它可以生成高质量图像并适用于 Chrome、Safari、Firefox 和 Edge - 这里是:

.myDiv {
    width: 256px; height: 256px;
    background-size: 100% 100%;
    background-image: url("data:image/svg+xml;utf8,%3Csvg preserveAspectRatio='none' viewBox='0 0 1 1' version='1.1' xmlns='http://www.w3.org/2000/svg'%3E%3Cdefs%3E%3ClinearGradient id='g'%3E%3Cstop offset='0' stop-color='%23fff' stop-opacity='0'%3E%3C/stop%3E%3Cstop offset='1' stop-color='%23fff' stop-opacity='1'%3E%3C/stop%3E%3C/linearGradient%3E%3Cmask id='m'%3E%3Crect x='0' y='0' width='1' height='1' fill='url(%23g)'%3E%3C/rect%3E%3C/mask%3E%3ClinearGradient id='a' gradientTransform='rotate(90)'%3E%3Cstop offset='0' stop-color='magenta'%3E%3C/stop%3E%3Cstop offset='1' stop-color='white'%3E%3C/stop%3E%3C/linearGradient%3E%3ClinearGradient id='b' gradientTransform='rotate(90)'%3E%3Cstop offset='0' stop-color='yellow'%3E%3C/stop%3E%3Cstop offset='1' stop-color='red'%3E%3C/stop%3E%3C/linearGradient%3E%3C/defs%3E%3Crect x='0' y='0' width='1' height='1' fill='url(%23a)' mask='url(%23m)'%3E%3C/rect%3E%3Crect x='0' y='0' width='1' height='1' fill='url(%23b)' mask='url(%23m)' transform='translate(1,1) rotate(180)'%3E%3C/rect%3E%3C/svg%3E");
}
Run Code Online (Sandbox Code Playgroud)
<div class="myDiv"></div>
Run Code Online (Sandbox Code Playgroud)

在此解决方案中,我们将 SVG dataurl 图像注入 CSS 样式。我们可以自由地更改widthheightin myDiv(以允许我们在 svg 中使用它preserveAspectRatio='none',并且另外还background-size: 100% 100%;可以支持 firefox)。svg 内部使用的颜色magenta, white, yellow, red可以更改为任何 css 格式的颜色。为了与 MS Edge 兼容,在解决方案 SVG 中,我们更改了以下字符:"to '<to %3C >to%3E #to %23(信息来自此处)。

在此输入图像描述

可编辑的例子在这里。Svg 背景生成器位于下面(或此处)。

function generateCSS() {
  let str = svgData.innerHTML;

  let r = str
    .replace('width="256" height="256" ', "")
    .replace('colorTopLeft', colorTopLeft.value)
    .replace('colorTopRight', colorTopRight.value)
    .replace('colorBottomLeft', colorBottomLeft.value)
    .replace('colorBottomRight', colorBottomRight.value)
    .replace(/> +/g, ">")
    .replace(/ +</g, "<")
    .replace(/>/g, "%3E")
    .replace(/</g, "%3C")
    .replace(/>/g, "%3E")
    .replace(/#/g, "%23")
    .replace(/"/g, "'")
    .replace(/ +/g, " ")
    .replace(/^ +/g, "")
    .split("\n")
    .join("");


  svgToText.value = `background-image: url("data:image/svg+xml;utf8,${r}");`
  lengthCss.innerText = `length: ${r.length}`

  preview.style = svgToText.value + '; display: block;'
}
Run Code Online (Sandbox Code Playgroud)
.topForm {
  display: flex;
  margin-bottom: 20px;
}

.previewBox {
  display: none;
  margin-left: 20px;
  width: 256px;
  height: 256px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="topForm">
  <div>
    <div><input id="colorTopLeft" value="red"> top left</div>
    <div><input id="colorTopRight" value="magenta"> top right</div>
    <div><input id="colorBottomLeft" value="yellow"> bottom left</div>
    <div><input id="colorBottomRight" value="white"> bottom right</div>
    <div><button onclick="generateCSS()" style="margin: 20px">Generate CSS</button></div>
  </div>
  <div id="preview" class="previewBox"></div>
</div>



<textarea cols=70 rows=18 id="svgToText" style="top:20px;" placeholder="output CSS background-image"></textarea>

<div id="lengthCss"></div>

<template id="svgData">
  <svg width="256" height="256" preserveAspectRatio='none' viewBox='0 0 1 1' version='1.1' xmlns='http://www.w3.org/2000/svg'>
      <defs>
          <linearGradient id='g' >
              <stop offset='0' stop-color='#fff' stop-opacity='0'/>
              <stop offset='1' stop-color='#fff' stop-opacity='1'/>
          </linearGradient>
          <mask id='m'>
              <rect x='0' y='0' width='1' height='1' fill='url(#g)'/>
          </mask>
          <linearGradient id='a' gradientTransform='rotate(90)'>
              <stop offset='0' stop-color='colorTopRight'/>
              <stop offset='1' stop-color='colorBottomRight'/>
          </linearGradient>
          <linearGradient id='b' gradientTransform='rotate(90)'>
              <stop offset='0' stop-color='colorBottomLeft'/>
              <stop offset='1' stop-color='colorTopLeft'/>
          </linearGradient>
      </defs>

      <rect x='0' y='0' width='1' height='1' fill='url(#a)' mask='url(#m)'/>
      <rect x='0' y='0' width='1' height='1' fill='url(#b)' mask='url(#m)' transform='translate(1,1) rotate(180)'/> 
  </svg>
</template>
Run Code Online (Sandbox Code Playgroud)


The*_*978 5

通过使用遮罩图像和线性渐变,我们可以实现一个无缝的 4 色角渐变,只需要一个::after伪元素。

超文本标记语言

<div id="quad">
</div>
Run Code Online (Sandbox Code Playgroud)

萨斯

@mixin QuadVertexColors($v0, $v1, $v2, $v3) {

  background: linear-gradient(to bottom, $v0, $v2);

  &::after {

    content: "";
    position: absolute;
    width: inherit;
    height: inherit;
    background: linear-gradient(to bottom, $v1, $v3);
    -webkit-mask-image: linear-gradient(to left, white, transparent);
  }
}

body {

  background-color: #111111;
  padding: 0;
  margin: 0;

  #quad {

    $size: 100vh;

    width: $size;
    height: $size;

    @include QuadVertexColors(red, magenta, yellow, white);
  }
}
Run Code Online (Sandbox Code Playgroud)

CodePen 演示