我尝试使用 CSS 伪元素为我创建一个自定义按钮。我也可以做到,但这取决于背景。我的意思是伪元素之一应该与 div 背景颜色完全相同。例如,如果我想将按钮放置在图像或渐变背景中,就会出现问题。我怎样才能做出类似不依赖于背景对象颜色的东西?
我的方法是:
.teno-btn {
border: none;
display: block;
text-align: center;
cursor: pointer;
text-transform: uppercase;
outline: none;
overflow: hidden;
position: relative;
color: #fff;
font-weight: 700;
font-size: 15px;
background-color: #000644;
padding: 17px 40px;
margin: 0 auto;
/* box-shadow: 0 5px 15px rgba(0, 0, 0, 0.20); */
}
.teno-btn span {
position: relative;
z-index: 1;
}
.teno-btn:after {
content: "";
position: absolute;
left: 40px;
top: 40px;
height: 40px;
width: 100px;
background: #ffffff;
-webkit-transform: translateX(-98%) translateY(-25%) rotate(45deg);
transform: translateX(-98%) translateY(-25%) rotate(45deg);
}
.teno-btn:before {
content: "";
position: absolute;
left: 20px;
top: 40px;
height: 40px;
width: 100px;
transform: rotate(45deg);
background: #5ceb95;
-webkit-transform: translateX(-98%) translateY(-25%) rotate(45deg);
transform: translateX(-98%) translateY(-25%) rotate(0deg);
}
<button class="teno-btn mi-ripple mi-ripple-rise">
<span> Download Now </span>
</button>
这是我期望的按钮,我可以在任何背景或任何地方使用:
你可以像下面这样不使用伪元素
button {
--c: 15px; /* size of the cut */
padding: .5em 1.2em;
font-size: 20px;
border: none;
background:
conic-gradient(green 0 0) 0 100%/var(--c) var(--c) no-repeat,
red; /* you can have a gradient or an image if you want */
clip-path: polygon(0 0,100% 0,100% 100%,var(--c) 100%,0 calc(100% - var(--c)));
}
<button>a button</button>