skip to Main Content

I want to achieve a button where there will a border behind the button. I have gone through many methods but the closest one is Using Box Shadows the code looks something like this:

.button-with-bg-outline-shadow a{
    box-shadow:
   12px 8px 0 0px #042036,
   14px 7px 0 0px var(--e-global-color-a29f98a ),
   14px 9px 0 0px var(--e-global-color-a29f98a ),
   10px 8px 0 0px var(--e-global-color-a29f98a );
   transition: all .3s linear;
}

but the problem with this is when I have different background, I need to change colors in the box-shadow property and thus I need to write new CSS for every button separately.

Demo: https://imgur.com/j9BChj4

I want the area covered by the outline should be transparent so that I can reuse one element everywhere.

2

Answers


  1. Create a pseudo-element by inheriting some of its parent’s properties. Then apply a mask to cut out the inner area.

    /* Only for example --> */ body { margin: 0; display: grid; place-items: center; min-height: 100vh; background: 0 0 / cover url('https://i.stack.imgur.com/tWGMC.jpg'); }
    
    a {
      position: relative;
      display: grid;
      place-items: center;
      height: 127px; width: 606px;
      border-radius: 23px;
      font: bold 40px/1em sans-serif;
      text-decoration: none;
      color: #000;
      background-color: #ff0;
      transition: all 0.3s linear;
    }
    a::before {
      content: '';
      position: absolute;
      top: 23px; left: 23px; z-index: -1;
      height: inherit; width: inherit;
      border: 3px solid #0000;
      border-radius: inherit;
      color: #b2b2b2;
      background-color: inherit;
      --mask: linear-gradient(#000 0 0), linear-gradient(#000 0 0);
      -webkit-mask-image: var(--mask);
      -webkit-mask-size: 100% 100%;
      -webkit-mask-clip: padding-box, border-box;
      -webkit-mask-composite: xor;
      mask-image: var(--mask);
      mask-size: 100% 100%;
      mask-clip: padding-box, border-box;
      mask-composite: exclude;
      transition: all 0.3s linear;
    }
    <a href="#">Book An Appotment</a>
    Login or Signup to reply.
  2. Use :after + css variables + currentColor. This is funcy buttons 🙃:

    button {
      height: 48px;
      padding: 0 32px;
      border-radius: 24px;
      display: grid;
      place-items: center;
      border: 0;
      background-color: currentColor;
      cursor: pointer;
      transition: transform .4s;
      font-weight: 700;
      will-change: transform;
    }
    
    button:after {
      content: '';
      position: absolute;
      inset: 8px -8px -8px 8px;
      border: solid 2px currentColor;
      border-radius: inherit;
      pointer-events: none;
      transition: transform .4s;
      z-index: -1;
    }
    
    button span {
      color: var(--text-color, #fff);
    }
    
    button:hover {
      transform: translate(2px, 2px);
    }
    
    button:hover:after {
      transform: translate(-2px, -2px);
    }
    
    button:active {
      transform: translate(4px, 4px);
    }
    
    button:active:after {
      transform: translate(-4px, -4px);
    }
    <button style="color: blue;">
      <span>Button Red</span>
    </button>
    <br>
    <button style="color: lightblue; --text-color:black;">
      <span>Button Red</span>
    </button>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search