简体   繁体   English

具有线性渐变的三角形中的CSS颜色过渡

[英]CSS color transition in triangles with linear gradient

I am working on a rectangular background which is divided into 2 triangles by a line from top left to bottom right, as shown in the pic. 我正在一个矩形背景上工作,如图所示,该背景被从左上到右下的一条线分为2个三角形。

What I want to achieve is color transition in each triangle: 我要实现的是每个三角形的颜色过渡:

  1. In triangle ABD: pink becomes darker from left to right 在三角形ABD中:粉红色从左到右变暗
  2. In triangle ACD: blue becomes darker from left to right 在三角形ACD中:蓝色从左到右变暗

Note: The width and height are not fixed to 600 and 250. I just use them for demo purpose. 注意:宽度和高度不固定为600和250。我只是将它们用于演示目的。

HTML code: HTML代码:

<div class="background-wrapper">
  <p class="float-left">A</p>
  <p class="float-right">B</p>

  <br><br><br><br><br><br><br><br><br><br><br><br><br><br>

  <p class="float-left">C</p>
  <p class="float-right">D</p>
</div>

CSS code: CSS代码:

.background-wrapper {
  position: relative;
  width: 600px;
  height: 250px;
  color: #FFFFFF;
  padding: 20px 50px 80px 50px;
  background: linear-gradient(to left bottom, pink 50%, blue 50%);
}

.float-left {
  float: left;
}

.float-right {
  float: right;
}

Demo jsfiddle here 在这里演示jsfiddle

在此处输入图片说明

One posibility, that is cross-browser but that gives washed colors, is to overlay the triangles with a semitransparent gradient that is white on one side and black in the other. 一种可能性是跨浏览器,但会产生水洗的颜色,它是用半透明渐变覆盖三角形,该渐变的一侧为白色,另一侧为黑色。

This effect gets much better using blend modes, but the support is lower. 使用混合模式可获得更好的效果,但支持较低。

 .test { width: 400px; height: 300px; background-image: linear-gradient(to left, rgba(0,0,0,.5), rgba(0,0,0,0) 40%, rgba(255,255,255,0) 60%, rgba(255,255,255,.5)), linear-gradient(to top right, blue 50%, fuchsia 50%); } 
 <div class="test"></div> 

You can use more colors after you define the linear-gradient position, so you can do stuff like: 定义线性渐变位置后,可以使用更多颜色,因此可以执行以下操作:

background: linear-gradient(to left bottom, deeppink 0%, pink 50%, blue 50%,midnightblue 100%);

Check your updated fiddle 检查您更新的小提琴

I modified your code quite a bit from the original. 我从原始代码中修改了很多代码。 I added two new elements to act as the background. 我添加了两个新元素作为背景。 May not be the solution you're looking for but off the top of my head this is what works. 可能不是您正在寻找的解决方案,但最重要的是这是行之有效的。

Fiddle 小提琴

 .background-wrapper { position: relative; width: 600px; height: 250px; color: #FFFFFF; padding: 20px 50px 80px 50px; overflow: hidden; } .triangle { position: absolute; top: -65%; right: -30%; width: 125%; height: 125%; transform: rotate(26.5deg); background: linear-gradient(to right, pink, #f44274); } .triangle.bottom { top: initial; right: initial; left: -30%; bottom: -64.8%; background: linear-gradient(to right, blue, navy); } 
 <div class="background-wrapper"> <div class="triangle top"></div> <div class="triangle bottom"></div> </div> 

.background-wrapper {
  position: relative;
  width: 800px;
  height: 450px;
  background: #ffffff;
/* Old Browsers */background: -moz-linear-gradient(45deg, #ffffff 0%, #6176ff 49%, #ff80d9 50%, #ffffff 100%);
 /* FF3.6+ */background: -webkit-gradient(left bottom, right top, color-stop(0%, #ffffff), color-stop(49%, #6176ff), color-stop(50%, #ff80d9), color-stop(100%, #ffffff));
/* Chrome, Safari4+ */background: -webkit-linear-gradient(45deg, #ffffff 0%, #6176ff 49%, #ff80d9 50%, #ffffff 100%);
 /* Chrome10+,Safari5.1+ */background: -o-linear-gradient(45deg, #ffffff 0%, #6176ff 49%, #ff80d9 50%, #ffffff 100%);
 /* Opera 11.10+ */background: -ms-linear-gradient(45deg, #ffffff 0%, #6176ff 49%, #ff80d9 50%, #ffffff 100%);
 /* IE 10+ */background: linear-gradient(45deg, #ffffff 0%, #6176ff 49%, #ff80d9 50%, #ffffff 100%);
/* W3C */filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#ffffff', GradientType=1 );
/* IE6-9 fallback on horizontal gradient */
}

.float-left {
  float: left;
}

.float-right {
  float: right;
}

you can specify the angle in the gradient. 您可以在渐变中指定角度。 Try the above code. 试试上面的代码。 it works with width and height. 它可以与宽度和高度一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM