箱形与直角梯形

时间:2014-11-09 17:07:10

标签: html css css3 css-shapes

我想知道这个形状是否可以用尽可能少的html在css3中完成:

Screenshot

到目前为止,我已设法做到了这一点:



.wrapper {
  position: relative;
}
.box {
  width: 100px;
  height: 100px;
  border: 1px solid #000;
  position: absolute;
  top: 100px;
  left: 100px;
}
.box:before {
  content: "";
  border: 1px solid #000;
  border-bottom: 1px solid #fff;
  width: 50%;
  height: 10px;
  position: absolute;
  top: -12px;
  left: -1px;
}
.box:after {
  content: "";
  border: 1px solid #000;
  border-top: 1px solid #fff;
  width: 50%;
  height: 10px;
  position: absolute;
  bottom: -12px;
  right: -1px;
}

<div class="wrapper">
  <div class="box"></div>
</div>
&#13;
&#13;
&#13;

小提琴是here,但我不知道如何歪曲它,以便我在顶部和底部都有直角梯形。

3 个答案:

答案 0 :(得分:16)

形状不需要额外的元素

只需<div>

即可创建形状
  • 左侧是div,左边,上边和下边框。

  • 右侧由:before及其顶部,右侧和底部边框构成

  • 由于:after

  • ,使用skewY创建了加入这两个框的范围

Note the browser support of the transform property. IE 9需要-ms-前缀,Safari和Android浏览器需要-webkit-

Screenshot

工作示例 - 只是形状

CSS已经压缩,伪元素的边框样式继承自div本身。

&#13;
&#13;
div {
  border: solid 4px #000;
  border-right-width: 0;
  width: 100px;
  height: 200px;
  position: relative;
}
div:before,div:after {
  content: '';
  display: block;
  height: 100%;
  width: 100%;
  border: inherit;
  border-right-width: 4px;
  border-left: none;
  position: absolute;
  left: 100%;
  top: 13px;
  margin-left: 20px;
}
div:after {
  width: 20px;
  border-right: none;
  top: 5px;
  transform: skewY(40deg);
  margin: 0;
}
&#13;
<div></div>
&#13;
&#13;
&#13;

工作示例 - 带文字

通过上面的示例,内容不会包含在整个形状中。相反,它将被限制在div半宽内。内容需要包含在宽度为200%的<span>中,以便将其打到divs约束之外。

&#13;
&#13;
div {
  border: solid 4px #000;
  border-right-width: 0;
  width: 100px;
  height: 200px;
  position: relative;
}
div:before,div:after {
  content: '';
  display: block;
  height: 100%;
  width: 100%;
  border: inherit;
  border-right-width: 4px;
  border-left: none;
  position: absolute;
  left: 100%;
  top: 13px;
  margin-left: 20px;
}
div:after {
  width: 20px;
  border-right: none;
  top: 5px;
  transform: skewY(40deg);
  margin: 0;
}
span {
  width: 200%;
  display: block;
  padding: 20px 10px 10px;
}
&#13;
<div><span>This is me writing a large amount of words into the div. I think that you may want a span in order to contain them.</span></div>
&#13;
&#13;
&#13;

答案 1 :(得分:4)

使用两个不同的元素:

1)将形状分成两个不同的矩形

2)使用伪元素afterbefore创建连接线后。

我的方法:

enter image description here

.wrapper {
  position: relative;
}
.box {
  width: 50px;
  height: 100px;
  border: 4px solid #000;
  position: absolute;
  top: 100px;
  left: 100px;
  border-right: 0;
}
.box2 {
  width: 50px;
  height: 100px;
  border: 4px solid #000;
  position: absolute;
  top: 112px;
  left: 164px;
  border-left: 0;
}
.box:after {
  content: "";
  position: absolute;
  width: 15px;
  border: 2px solid #000;
  right: -15px;
  top: 2px;
  transform: rotate(45deg);
}
.box:before {
  content: "";
  position: absolute;
  width: 15px;
  border: 2px solid #000;
  right: -15px;
  bottom: -10px;
  transform: rotate(45deg);
}
<div class="box"></div>
<div class="box2"></div>

答案 2 :(得分:3)

我使用了四个div.left.right.middle-top.middle-bottom;并且偏向.middle-top.middle-bottom以添加这些连接线。

.left {
    width: 40px;
    height: 100px;
    border: 3px solid black;
    border-right: 1px solid white;
    position: absolute;
    top: 50px;
    left: 100px;
}
.right {
    width: 40px;
    height: 100px;
    border: 3px solid #000;
    border-left: 1px solid white;
    position: absolute;
    top: 60px;
    left: 160px;
}
.middle-top {
    width: 20px;
    height: 20px;
    border-top: 3px solid black;
    position: absolute;
    transform: matrix(1, 0.5, -0.5, 1, 0, 0);
    top: 55px;
    left: 137px;
    z-index: 9;
}
.middle-bottom {
    width: 21px;
    height: 20px;
    border-top: 3px solid black;
    position: absolute;
    transform: matrix(1, 0.5, -0.5, 1, 0, 0);
    top: 158px;
    left: 135px;
    z-index: 9;
}
<div class="left"></div>
<div class="middle-top"></div>
<div class="middle-bottom"></div>
<div class="right"></div>