div周围的边界不完整

时间:2016-01-27 06:46:24

标签: html css css3 css-shapes

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

Incomplete borders around div

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

Approach to create incomplet borders around div

但我对自适应版本有点担心 - 缩放三种形状。那么也许是个更好的主意?

5 个答案:

答案 0 :(得分:25)

你可以使用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/

答案 1 :(得分:22)

您可以使用:before:after伪元素

执行此操作

完整设计 Fiddle

// i will update this function when you provide more details
var getData = function(users){
  for(var i in users{
    if(users[i]["theFieldYouWant"] == theValueYouWant){
        return users[i];
    }   
  }
}

$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
            // this callback will be called asynchronously
            // when the response is available
            $scope.results = getData(response);
        },
        function errorCallback(response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        }
    );
.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;
}

SVG

<div class="square"></div>
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;
}

答案 2 :(得分:15)

这种方法允许您:

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

它依赖于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>

答案 3 :(得分:10)

好吧,请按照上述答案,我建议使用pseudo元素来实现此效果。

  

但是有另一种方法可以在不使用的情况下实现这一目标   伪元素。

这是你应该怎么做的。

&#13;
&#13;
.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;
    }
&#13;
<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>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

我们可以使用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>

enter image description here

相关问题