【问题标题】:Gradient with Shape渐变形状
【发布时间】:2019-11-25 14:30:00
【问题描述】:
我正在尝试在按钮上进行渐变,但无法使其具有与按钮其他部分相同的渐变。
我尝试在渐变中添加渐变,但它似乎不起作用并且找不到解决方案。
这是我正在使用的代码:
button{
color: white;
padding: 3px 3px 3px 0px;
border: 0px;
font-size: 20px;
width: 200px;
text-align: right;
background: linear-gradient(50deg , transparent 50%, rgb(149, 151, 155) 0%) left no-repeat, linear-gradient(rgb(200, 205, 212), rgb(149, 151, 155)) 30px 0 no-repeat;
background-size: 30px 100%, 100% 100%;
position: relative;
cursor: pointer;
}
<button>Meet the Team</button>
有没有办法解决这个问题?
提前致谢
【问题讨论】:
标签:
html
css
gradient
linear-gradients
【解决方案1】:
考虑在应用渐变的伪元素上进行倾斜变换。由于它是一个顶部/底部方向,因此渐变不会受到倾斜的影响
button{
color: white;
padding: 3px 3px 3px 0px;
border: 0px;
font-size: 20px;
width: 200px;
text-align: right;
position: relative;
cursor: pointer;
overflow:hidden;
background:none;
z-index:0;
}
button::before {
content:"";
position:absolute;
z-index:-1;
top:0;
left:0;
right:0;
bottom:0;
transform-origin:top;
transform:skewX(50deg);
background:linear-gradient(rgb(200, 205, 212), rgb(149, 151, 155));
}
<button>Meet the Team</button>
对于另一个方向,你可能需要调整渐变的度数:
button{
color: white;
padding: 3px 3px 3px 0px;
border: 0px;
font-size: 20px;
width: 200px;
text-align: right;
position: relative;
cursor: pointer;
overflow:hidden;
background:none;
z-index:0;
}
button::before {
content:"";
position:absolute;
z-index:-1;
top:0;
left:0;
right:0;
bottom:0;
transform-origin:top;
transform:skewX(50deg);
background:linear-gradient(-50deg,purple,red);
}
<button>Meet the Team</button>
【解决方案2】:
这是因为您已经在使用 linear-gradient 属性作为在按钮上创建三角形末端的技巧。
您不能多次使用同一个属性。如果你想要一个三角形的末端,可以在你的按钮上坚持使用纯色。