我想创建一个渐变为背景的按钮,并根据其内部的文本量调整其尖端。页面背景可能会有所不同,因此我不能在目标端使用纯色。我想出的最好的解决方案是在伪元素之后使用svg代码作为背景图像,将背景高度设置为100%,但是这种解决方案不能满足我的要求,因为我不知道它是否看起来像在所有现代浏览器上都不错。你有什么想法?也许我应该使用Javascript?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Button example</title>
<style>
html, body {
height: 100%;
}
body {
background-color: #e5e5e5;
}
.wrapper {
background-color: #ffffff;
padding: 15px;
max-width: 180px;
margin: 0 auto;
}
.btn {
position: relative;
display: inline-block;
background: #000000 linear-gradient(to bottom, #000000, #666666);
color: #ffffff;
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
padding: 8px 20px;
margin-right: 12px;
margin-bottom: 15px;
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
}
.btn::after {
content: '';
position: absolute;
top: 0px;
right: -12px;
display: inline-block;
height: 100%;
width: 13px;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='13' height='40'%3E%3ClinearGradient id='css-btn-example' gradientUnits='userSpaceOnUse' x1='6.5' y1='40' x2='6.5'%3E%3Cstop offset='0' stop-color='%23666'/%3E%3Cstop offset='1'/%3E%3C/linearGradient%3E%3Cpath fill='url(%23css-btn-example)' d='M0 0h1l12 20L1 40H0z'/%3E%3C/svg%3E");
background-size: 13px 100%;
}
.btn:last-child {
margin-bottom: 0;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="btn">One line</div>
<div class="btn">Two line button example</div>
<div class="btn">Three line button long example</div>
</div>
</body>
</html>
答案 0 :(得分:1)
如果渐变始终是从上到下(或从下到上),则可以使用如下所示的偏斜来考虑一些技巧。想法是使用伪元素,在其中应用两个不同的渐变,这些渐变将以相同的颜色相交,从而产生一个渐变的错觉。
.box {
width: 150px;
padding: 20px;
box-sizing: border-box;
position: relative;
color: #fff;
z-index: 0;
overflow: hidden;
margin:5px;
border-radius:5px 0 0 5px;
}
.box:before,
.box:after {
content: "";
position: absolute;
z-index:-1;
height: 50%;
left: 0;
right: 0;
}
.box:before {
top: 0;
background: linear-gradient(to bottom, blue, #4100bf);
transform: skewX(30deg);
transform-origin: bottom right;
}
.box:after {
bottom: 0;
background: linear-gradient(to top, purple, #4100bf);
transform: skewX(-30deg);
transform-origin: top right;
}
<div class="box">
Some text here
</div>
<div class="box">
Some text here Some text here
</div>
另一个没有透明度的想法是考虑使用多色背景,并用白色(或背景的任何颜色)隐藏渐变。然后,您可以考虑任何种类的渐变,甚至是径向渐变。
.box {
width: 150px;
padding: 20px;
box-sizing: border-box;
color: #fff;
margin:5px;
background:
linear-gradient(to bottom right,transparent 48%,#fff 50%) bottom right/20px 50%,
linear-gradient(to top right,transparent 48%,#fff 50%) top right/20px 50%,
linear-gradient(to bottom, blue,purple);
background-repeat:no-repeat;
border-radius:5px 0 0 5px;
}
<div class="box">
Some text here
</div>
<div class="box">
Some text here Some text here
</div>
您还具有剪切路径解决方案:
.box {
width: 150px;
padding: 20px;
box-sizing: border-box;
color: #fff;
margin: 5px;
background: linear-gradient(to bottom, blue, purple);
background-repeat: no-repeat;
border-radius: 5px 0 0 5px;
-webkit-clip-path: polygon(0% 0%, calc(100% - 20px) 0%, 100% 50%, calc(100% - 20px) 100%, 0% 100%);
clip-path: polygon(0% 0%, calc(100% - 20px) 0%, 100% 50%, calc(100% - 20px) 100%, 0% 100%);
}
<div class="box">
Some text here
</div>
<div class="box">
Some text here Some text here
</div>