skip to Main Content

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

2

Answers


  1. 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>
    Login or Signup to reply.
  2. There’s no way you can make it independent since you are trying to cover the button’s background using the ::after pseudo element.
    The solution would be to use one pseudo element and shape it like a triangle, then cut the bottom left corner of your button so the background behind it becomes visible.
    Here is the updated CSS:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            .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;
                padding: 17px 40px;
                margin: 0 auto;
                background-color: #000644;
    
                /* Cutting the bottom left corner */
                clip-path: polygon(0 0, 100% 0, 100% 100%, 28px 100%, 0 calc(100% - 28px));
            }
    
            .teno-btn::before {
                content: "";
                position: absolute;
                bottom: 0;
                left: 0;
    
                /* Creating a right angle triangle */
                width: 0;
                height: 0;
                border-top: 28px solid blue;
                border-left: 28px solid transparent;
            }
    
        </style>
    </head>
    
    <body>
        <button class="teno-btn">
            Download Now
        </button>
        <script src="./script.js"></script>
    </body>
    
    </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search