Search code examples
htmlcsscss-shapes

How to make border with square on corners?


How to make border with square on corners? And break one of the borders. Like on the image.

enter image description here

I did it with four additional blocks but I guess there might be a better way. And I don't know how to break outer border.

:root {
  --size: 8px;
  --r: -3px;
}

.wrapper {
  position: relative;
  border: 1px solid black;
  margin: 25px auto;
  padding: 2px;
  width: max-content;
}

.inner {
  padding: 15px 25px;
  border: 1px solid black;
}

.conner {
  position: absolute;
  height: var(--size);
  width: var(--size);
  background-color: black;
}

.bottom {
  bottom: var(--r);
}

.right {
  right: var(--r);
}

.top {
  top: var(--r);
}

.left {
  left: var(--r);
}
<div class="wrapper">
  <div class="inner">qwerty</div>
  <div class="conner top left"></div>
  <div class="conner top right"></div>
  <div class="conner bottom left"></div>
  <div class="conner bottom right"></div>
</div>


Solution

  • You can use border-image property in CSS.

    Follow these steps:

    1. Create an image as below:

    border image

    1. apply border-image on .wrapper and provide the image url. Read more about border-image : https://developer.mozilla.org/en-US/docs/Web/CSS/border-image

    .wrapper {
      height: 160px;
      width: 250px;
      border-style: solid; 
      border-image: url(https://i.sstatic.net/2RoPg.png) 12 / 6 stretch;
      position: relative;
      box-sizing: border-box;
    }
    
    .inner {
      height: 100%;
      width: 100%;
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      box-sizing: border-box;  
    }
    <div class="wrapper">
      <div class="inner">QWERTY</div>
    </div>