div 周围的边界不完整

Incomplete borders around div

我正在寻找一种方法来创建一个 带有边框 的不完整正方形,其中包含一些文本和纯 css 的背景。这是我想要实现的目标:

我最初的想法是根据三个形状创建形状,然后相应地为边框着色:

但我有点担心自适应版本 - 缩放三个形状。所以也许有更好的主意,有人吗?

你可以用 :before and :after 伪元素来做到这一点

完成设计Fiddle

.square {
  width: 200px;
  height: 300px;
  border-left: 1px solid gray;
  border-bottom: 1px solid gray;
  border-top: 1px solid gray;
  position: relative;
}

.square:before, .square:after {
  content: "";
  height: 20%;
  position: absolute;
  right: 0;
  border-right: 1px solid gray;
}

.square:before {
  bottom: 0;
}
<div class="square"></div>

SVG

line {
  stroke: #6996FB; 
  stroke-width: 2;
}

svg {
  overflow: visible;
}

button {
  padding: 10px 50px;
  border: none;
  color: white;
  margin-right: 10px;
  margin-top: 15px;
}

.btn-blue {
  background: #5D8CFF;
}

.btn-green {
  background: #33F1D9;
}

h3 {
  margin: 0;
}
<svg width="250" height="300" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <line x1="1" y1="1" x2="250" y2="1"></line>
    <line x1="0" y1="300" x2="250" y2="300"></line>
    <line x1="1" y1="1" x2="1" y2="300"></line>
    <line x1="249" y1="0" x2="249" y2="70"></line>
    <line x1="249" y1="230" x2="249" y2="300"></line>
    
    <foreignobject x="60" y="90" width="400" height="180">
      <body xmlns="http://www.w3.org/1999/xhtml">
        <h3>Lorem ipsum dolor sit <br> amet, consectetur adipisicing  elit. Suscipit</h3>
        <button class="btn-blue">Btn 1</button><button class="btn-green">Btn 2</button>
      </body>
    </foreignobject>
    
</svg>

你可以用 css 伪 ::after::before 来做,像这样

.incomplete-box{
  border: solid 1px #fff;
  border-right: none;
  width: 100px;
  height: auto;
  position: relative;
}
.incomplete-box::after,
.incomplete-box::before{
  content: '';
  position: absolute;
  height: 30%;
  width: 1px;
  background-color: #fff;
  right: 0;
}
.incomplete-box::after{
  top: 0;
}
.incomplete-box::before{
  bottom: 0;
}

演示

固定宽度和高度:https://jsfiddle.net/nikhilvkd/qt5ne3yw/

自动宽度和高度:https://jsfiddle.net/nikhilvkd/0v3k8rv8/2/

好吧,按照上面的回答,我推荐使用pseudo元素来实现这个效果。

But There is another way to accomplish this without using pseudo-elements.

这是你应该如何做的。

.row{display:table;table-layout:fixed;}
    .col{display:table-cell;}
    
    .row{width:250px; margin: auto;}
    .mid.row > .col{ height: 100px; }
    
    .col{ text-align: center;}
    .top.col, .bottom.col{
      border-top: 1px solid black;
      border-left: 1px solid black;
      border-right: 1px solid black;
      height: 50px;
    }
   .bottom.col{
     border-top: 0;
     border-bottom: 1px solid black;
   }
    .mid.row > .col{
      border-left: 1px solid black;
      border-right: 0;
      vertical-align: middle;
      text-align: right;
    }
   .mid.row > .col span{
      margin-right: -30px;
     max-width: 300px;
    }
<div class="row">
  <div class="top col"></div>
</div>
<div class="mid row">
  <div class="col">
    <span>Hey you can achieve this without using pseudo elements :)</span>
  </div>
</div>
<div class="row">
  <div class="bottom col"></div>
</div>

这种方法允许您:

  • 添加任何内容,无论内容的高度或宽度如何,边框都会适应周围
  • 支持透明背景,可以显示在图像或非纯色上
  • 不添加任何非语义元素

它依赖于2个绝对定位的伪元素和一个div。内容和边框之间的间距由 div 上的填充控制:

div{
  position:relative;
  display:inline-block;
  padding:50px 100px;
  border-left:1px solid #000;
  text-align:center;
}
div:before, div:after{
  content:'';
  position:absolute;
  right:50%; left:0;
  height:50px;
  border-right:1px solid #000;
}
div:before{
  top:0;
  border-top:1px solid #000;
}
div:after{
  bottom:0;
  border-bottom:1px solid #000;
}
body{background:url('http://i.imgur.com/3IXm5qm.jpg');background-size:cover;}
<div>
  <h2>This is a very long title on<br/> 2 lines</h2>
  <button>Button</button>
  <p>Some text</p>
</div>

我们可以用 linear-gradient 来做到这一点。没有 SVG,没有伪元素。我使用了一些变量来轻松控制一切。

.container {
  /* you can change these variables */
  --border-color: #000;
  --border-width: 2px;
  --space: 100px;
  
  width: 200px;
  height: 300px;
  position: relative;

  background: linear-gradient(var(--border-color), var(--border-color)) 0 0/var(--border-width) 100%, 
              linear-gradient(var(--border-color), var(--border-color)) 0 100%/100% var(--border-width), linear-gradient(var(--border-color), var(--border-color)) 0 0/100% var(--border-width), 
              linear-gradient(var(--border-color), var(--border-color)) 100% 0/var(--border-width) calc(50% - (var(--space) / 2)), 
              linear-gradient(var(--border-color), var(--border-color)) 100% 100%/var(--border-width) calc(50% - (var(--space) / 2));
  background-repeat: no-repeat;
}

.content {
  position: absolute;
  width: 200px;
  top: 50%;
  transform: translateY(-50%);
  right: -100px;
  background: yellow;
}
<div class="container">
  <div class="content">
    Lorem ipsum dolor sit, amet consectetur adipisicing elit.
  </div>
</div>