与梯度响应的背景 [英] Background with gradient responsive

查看:113
本文介绍了与梯度响应的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:(



但是如果我有一个更大的设备,会发生这种情况:



解决方案

解决方案:(TL; DR)



如果你需要响应渐变, 到[side] [side] 语法,而不是使用角度。下面是一个可生成响应三角形的代码段。



  body {background- color:red;}#grad1 {height:100px; width:100%;背景:线性梯度(右下角,rgba(255,255,255,0)49.9%,rgba(255,255,255,1)50.1%);}  

 < div id =grad1>< / div>  pre> 






说明:



如果我们详细了解渐变的角度如何影响起点和终点,那么可以理解为什么所讨论的梯度变成较小宽度的三角形并且变成宽度较大的梯形的原因






为什么斜线型渐变在不同宽度下会产生不同的形状?

线性渐变始终由轴(称为渐变线)定义。这可以被认为是通过包含渐变的框的中心绘制的线(下面截图中的黑线),并且被旋转梯度的角度。 0deg线从底部开始向上,而90deg线向右。



除了渐变线之外,渐变还有一个虚构的起点和一个终点。渐变的开始线是从框的左上角(与渐变线起点位于同一个象限的角落)到渐变线的垂直线,以及 end



这条假想渐变线上的每一点都将是一条直线,它是从盒子的右下角(对角)到渐变线的垂直线。具有某种颜色,这取决于梯度定义。在下面的情况下,我使用了一个透明度为50%的渐变,并且白色为休止。因此,渐变线上半部分的所有点都将是透明的,下半部分的颜色将是白色。



如下图所示,起始点和结束点之间的距离随着框的宽度增加而变得越来越大,并且这也影响中点(中间绿色线)。因此,渐变变为白色的点随着宽度的增加而改变,因此形状也改变。








下面的代码段不是解决方案(位于顶部)。这是我用来创建上面的屏幕截图,并想到让它在这里有趣:D



对于有角度的渐变:

  div {position:relative; display:inline-block; height:100px;背景:线性梯度(521deg,透明50%,白色50%); margin:200px 50px; border:1px solid black;}#div-large {width:400px;}#div-small {width:200px;} div:after {position:absolute; content:''; width:calc(100%+ 24px); left:-12px;背景:线性梯度(向右,透明计算(50%-1px),黑色计算(50%-1px),黑色计算(50%+ 1px),透明计算(50%+ 1px)到右边,绿色6px,透明6px); background-position:0px 0px,0px,calc(50% -  1px); background-repeat:no-repeat,repeat-x; background-size:100%100%,12px 2px; border-top:2px broken green; border-bottom:2px broken green; transform:rotate(521deg);}#div-large:after {height:calc(100%+ 125px); top:-64px;}#div-small:after {height:calc(100%+ 60px); top:-32px;} body {background:sandybrown;}  

 < div id ='div-small'>< / div>< div id ='div-large'>< / div>   



对于边对边渐变



  div {position:relative; display:inline-block; height:100px; margin:200px 50px; border:1px solid black;}#div-large {width:400px;背景:线性渐变(526deg,透明50%,白色50%);}#div-small {width:200px;背景:线性渐变(513.5deg,透明50%,白色50%);} div:after {position:absolute; content:''; width:calc(100%+ 24px); left:-12px;背景:线性梯度(向右,透明计算(50%-1px),黑色计算(50%-1px),黑色计算(50%+ 1px),透明计算(50%+ 1px)到右边,绿色6px,透明6px); background-position:0px 0px,0px,calc(50% -  1px); background-repeat:no-repeat,repeat-x; background-size:100%100%,12px 2px; border-top:2px broken green; border-bottom:2px dotted green;}#div-large:after {height:calc(100%+ 93px); top:-48px; transform:rotate(526deg);}#div-small:after {height:calc(100%+ 78px); top:-41px; transform:rotate(513.5deg);} body {background:sandybrown;}  

 < div id ='div-small'>< / div>< div id ='div-large'>< / div>  


免责声明: href =https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient =nofollow noreferrer> MDN page ,但我试图把尽可能多的它在我自己的话可能:)



I have the following code: (Fiddle)

body {
  background-color: red;
}
#grad1 {
  height: 100px;
  width: 100%;
  background: linear-gradient(521deg, rgba(138, 138, 138, 0) 50%, rgba(138, 138, 138, 0) 31.9%, #fff 0.1%, #fff 0%);
}

<div id="grad1"></div>

I basically want the width to be responsive and the gradient's shape to be maintained even though the width changes.

What I've tried:

  1. Set the width to 100% this doesn't work because its an empty div

That's about it, I have no other ideas to be honest. I'd appreciate if someone could help.


This is what I'm using it for: (the images are just an example to show what happens/what I mean)

But if I have a bigger device this happens:

解决方案

Solution: (TL;DR)

If you need responsive gradients, it is always better to use the to [side] [side] syntax instead of using angles. Below is a snippet that would produce a responsive triangle.

body {
  background-color: red;
}
#grad1 {
  height: 100px;
  width: 100%;
  background: linear-gradient(to bottom right, rgba(255, 255, 255, 0) 49.9%, rgba(255, 255, 255, 1) 50.1%);
}

<div id="grad1"></div>


Explanation:

The reason why the gradient in question becomes a triangle at smaller widths and becomes sort of a trapezoid at larger width can be understood if we have a detailed look at how the angle of the gradient affects the start and end points of the gradient.

Why the angled linear gradient produces different shapes at different widths?

Linear gradients are always defined by an axis (which is referred to as the gradient line). This can be thought of as a line that is drawn through the center of the box that contains the gradient (the black line in the below screenshot) and is rotated by the angle of the gradient. The 0deg line starts at bottom and goes upwards whereas 90deg line goes towards right.

In addition to the gradient line, gradients also have an imaginary start point and an end point. The start line of the gradient is the perpendicular line from top-left corner (the corner that's in the same quadrant as the gradient line's starting point) of the box to the gradient line and the end line is the perpendicular line from the bottom-right corner (opposite corner) of the box to the gradient line.

Each point on this imaginary gradient line will have a certain color depending on the gradient definition. In the below case, I have used a gradient that is transparent for 50% and is white for rest. So all points on the top half of the gradient line will be transparent and those on the bottom half will be white in color.

As you can see in the below screenshot, the distance between the start and end points become more and more as the width of the box increases and this affects the mid point also (the mid green line). So, the point from where the gradient becomes white changes as the width increases and thus the shape also changes.

Why the side to side linear gradient maintains the shape at all widths?

When the side to side syntax is used, the gradient line is angled such that it points into (or towards) the same quadrant in which the specified corner is (for this case, it points towards the 2nd quadrant because that is where the bottom right corner of the box is). Its is also angled in such a way that it is perpendicular to a line that connects the two neighbouring corners of the box (in this case, it'd be the bottom left corner and top right corner). Since the angle of the gradient line is automatically adjusted such that it is perpendicular to the diagonal (line connecting the two neighbouring corners), it always produces a triangle for a half-n-half gradient.


The below snippet is not the solution (that is on top). This is what I used to create the screenshot above and thought of leaving it here for fun :D

For angled gradient:

div {
  position: relative;
  display: inline-block;
  height: 100px;
  background: linear-gradient(521deg, transparent 50%, white 50%);
  margin: 200px 50px;
  border: 1px solid black;
}
#div-large {
  width: 400px;
}
#div-small {
  width: 200px;
}
div:after {
  position: absolute;
  content: '';
  width: calc(100% + 24px);
  left: -12px;
  background: linear-gradient(to right, transparent calc(50% - 1px), black calc(50% - 1px), black calc(50% + 1px), transparent calc(50% + 1px)), linear-gradient(to right, green 6px, transparent 6px);
  background-position: 0px 0px, 0px, calc(50% - 1px);
  background-repeat: no-repeat, repeat-x;
  background-size: 100% 100%, 12px 2px;
  border-top: 2px dashed green;
  border-bottom: 2px dashed green;
  transform: rotate(521deg);
}
#div-large:after{
  height: calc(100% + 125px);
  top: -64px;
}
#div-small:after{
  height: calc(100% + 60px);
  top: -32px;
}
body {
  background: sandybrown;
}

<div id='div-small'></div>
<div id='div-large'></div>

For side to side gradient:

div {
  position: relative;
  display: inline-block;
  height: 100px;
  margin: 200px 50px;
  border: 1px solid black;
}
#div-large {
  width: 400px;
  background: linear-gradient(526deg, transparent 50%, white 50%);
}
#div-small {
  width: 200px;
  background: linear-gradient(513.5deg, transparent 50%, white 50%);
}
div:after {
  position: absolute;
  content: '';
  width: calc(100% + 24px);
  left: -12px;
  background: linear-gradient(to right, transparent calc(50% - 1px), black calc(50% - 1px), black calc(50% + 1px), transparent calc(50% + 1px)), linear-gradient(to right, green 6px, transparent 6px);
  background-position: 0px 0px, 0px, calc(50% - 1px);
  background-repeat: no-repeat, repeat-x;
  background-size: 100% 100%, 12px 2px;
  border-top: 2px dashed green;
  border-bottom: 2px dashed green;
}
#div-large:after{
  height: calc(100% + 93px);
  top: -48px;
  transform: rotate(526deg);
}
#div-small:after{
  height: calc(100% + 78px);
  top: -41px;
  transform: rotate(513.5deg);
}
body {
  background: sandybrown;
}

<div id='div-small'></div>
<div id='div-large'></div>

Disclaimer: My explanation is heavily influenced by this MDN page but I've tried to put as much of it in my own words as possible :)

这篇关于与梯度响应的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆