简体   繁体   English

线性渐变不适用于 div

[英]Linear gradient not working with div

I am creating trapezoid using following CSS:我正在使用以下 CSS 创建梯形:

 .trapezoid { border-bottom: 100px solid red; border-left: 50px solid transparent; border-right: 50px solid transparent; height: 0; width: 100px; background: linear-gradient(red, yellow); }
 <div class='trapezoid'></div>

The linear-gradient attribute is not working.线性渐变属性不起作用。 I want the trapezoid as shadow ie its color should eventually fade away.我想要梯形作为阴影,即它的颜色最终会消失。 Can anyone please help me on this?任何人都可以帮我解决这个问题吗?

You cannot apply gradient in this way as you are using border and your element has a height of 0 so background won't be visible.您不能以这种方式应用渐变,因为您正在使用边框并且您的元素的高度为 0,因此背景将不可见。

Instead you can try to use multiple gradient to create the shape:相反,您可以尝试使用多个渐变来创建形状:

 .trapezoid { height: 100px; width: 200px; background: linear-gradient(to bottom left,white 50%,transparent 52%) 100% 0/40px 100% no-repeat, linear-gradient(to bottom right,white 50%,transparent 52%) 0 0/40px 100% no-repeat, linear-gradient(red, yellow); }
 <div class='trapezoid'></div>

Or use clip-path:或使用剪辑路径:

 .trapezoid { height: 100px; width: 200px; background: linear-gradient(red, yellow); -webkit-clip-path: polygon(20% 0%, 80% 0%, 100% 100%, 0% 100%); clip-path: polygon(20% 0%, 80% 0%, 100% 100%, 0% 100%); }
 <div class='trapezoid'></div>

Another method with skew and pseudo-element:另一种带有偏斜和伪元素的方法:

 .trapezoid { height: 100px; width: 200px; position: relative; } .trapezoid:before, .trapezoid:after{ content: ""; position: absolute; top: 0; bottom: 0; right: 0; width: 60%; background: linear-gradient(red, yellow); transform:skew(20deg); transform-origin:bottom right; } .trapezoid:after { left: 0; transform:skew(-20deg); transform-origin:bottom left; }
 <div class='trapezoid'></div>

Or use a transform on a suitably sized element (or pseudo-element).或者在适当大小的元素(或伪元素)上使用变换。

 .trapezoid { width: 100px; height: 100px; margin: auto; transform: perspective(100px) rotateX(40deg); background: linear-gradient(red, yellow); }
 <div class='trapezoid'></div>

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

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