【问题标题】:How to make border with square on corners?如何在角落制作带有正方形的边框?
【发布时间】:2020-05-13 23:24:59
【问题描述】:

如何在角上制作带有正方形的边框?并打破其中一个边界。就像图片上的一样。

我做了四个额外的块,但我想可能有更好的方法。而且我不知道如何打破外边界。

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

【问题讨论】:

标签: html css css-shapes


【解决方案1】:

您可以在 CSS 中使用 border-image 属性。

按照以下步骤操作:

  1. 如下创建图像:

  1. .wrapper 上申请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.stack.imgur.com/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>

【讨论】:

    【解决方案2】:

    这是一个带有多个背景和 CSS 变量的想法,可以轻松控制一切:

    .box {
      --s:20px; /* size of square */
      --w:calc(100% - 60px);  /* width of outer border*/
      --ot:3px; /* offset of outer border from outisde */
      --ob:5px; /* offset of inner border from inside */
      
      width:150px;
      height:120px;
      display:inline-block;
      margin:10px;
      border:var(--ot) solid transparent;
      background:
         /* squares */
         linear-gradient(#000,#000) top    left /var(--s) var(--s) border-box,
         linear-gradient(#000,#000) top    right/var(--s) var(--s) border-box,
         linear-gradient(#000,#000) bottom left /var(--s) var(--s) border-box,
         linear-gradient(#000,#000) bottom right/var(--s) var(--s) border-box,
         /*borders*/
         linear-gradient(#000,#000) top    /var(--w) 2px,
         linear-gradient(#000,#000) left   /2px var(--w),
         linear-gradient(#000,#000) bottom /var(--w) 2px,
         linear-gradient(#000,#000) right  /2px var(--w);
      background-repeat:no-repeat;
      position:relative;
      z-index:0;
    }
    .box::before {
      content:"";
      position:absolute;
      z-index:-1;
      top:   calc(var(--s) - var(--ot) - var(--ob));
      left:  calc(var(--s) - var(--ot) - var(--ob));
      right: calc(var(--s) - var(--ot) - var(--ob));
      bottom:calc(var(--s) - var(--ot) - var(--ob));
      border:2px solid;
    }
    <div class="box"></div>
    
    <div class="box" style="--w:calc(100% - 100px);--s:30px;--ob:10px"></div>
    
    <div class="box" style="--w:80px;--s:15px;--ot:0px;--ob:0px"></div>
    
    <div class="box" style="--w:100%;--s:15px;--ot:5px;--ob:0px"></div>
    
    <div class="box" style="--w:calc(100% - 20px);--s:0px;--ot:5px;--ob:-15px"></div>
    
    <div class="box" style="--w:calc(100% - 20px);--s:0px;--ot:5px;--ob:5px"></div>
    
    <div class="box" style="--w:0;--s:20px;--ot:0px;--ob:10px"></div>
    
    <div class="box" style="--w:calc(100% - 80px);--s:20px;--ot:10px;--ob:-40px"></div>

    【讨论】:

    • 嗨@temani-afif,我选择border-image 作为主要答案,但您的选择非常有趣,可能真的很有用。我希望你会得到很多赞成票。非常感谢。
    • @UserTest013 没问题 ;) 接受的答案并不重要。有多个答案更重要。顺便说一句,我建议您考虑为您的边框图像使用 SVG 而不是 png,以具有更好的渲染效果并避免在调整大小时产生一些模糊效果。
    猜你喜欢
    • 1970-01-01
    • 2017-06-10
    • 2022-06-10
    • 1970-01-01
    • 2016-08-18
    • 2017-06-17
    • 1970-01-01
    • 2016-03-25
    • 2017-09-22
    相关资源
    最近更新 更多