htmlcssanimationbutton

How to make background independ pseudo button in CSS?


I tried to create a custom button for me using CSS pseudo elements. I can make it too, but it is background dependent. I mean one of the pseudo elements should be the exact same color of the div background. For example, if I want to place the button in an image or gradient background, then the problem creates. How can I make something like that which is not depend on background objects color ?

My Approach is:

.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>

Here is my expected button that I can use in any background or anywhere:

enter image description here


Solution

  • You can do it like below without pseudo-elements

    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 use 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>

    If you want the cut to be rectangle, we add another variable:

    button {
      --cx: 20px; 
      --cy: 15px; 
    
      padding: .5em 1.5em;
      font-size: 20px;
      border: none;
      background: 
        conic-gradient(green 0 0) 0 100%/var(--cx) var(--cy) no-repeat,
        red; /* <-- you can use a gradient or an image if you want */
      clip-path: polygon(0 0,100% 0,100% 100%,var(--cx) 100%,0 calc(100% - var(--cy)));
    }
    <button>a button</button>

    And if your background is a solid coloration, you can use one gradient to define both colors:

    button {
      --cx: 20px; 
      --cy: 15px; 
    
      padding: .5em 1.5em;
      font-size: 20px;
      border: none;
      background: conic-gradient(from 180deg at var(--cx) calc(100% - var(--cy)),green 25%,red 0);
      clip-path: polygon(0 0,100% 0,100% 100%,var(--cx) 100%,0 calc(100% - var(--cy)));
    }
    <button>a button</button>