我想知道如果这个形状可以在css3中尽可能少的html来完成:
到目前为止,我已经设法做到这一点:
- .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>
解决方法
形状不需要额外的元素
可以使用< div> ;:::创建形状
>左边是左边的div,top和bottom的边框.
>右边是由…制成的:之前和它的顶部,右边和底部的边界
>加入两个框的跨度是用以下方式创建的:感谢skewY
Note the browser support of the transform property. IE 9需要-ms-前缀,Safari和Android浏览器需要-webkit-.
工作实例 – 只是形状
CSS已被压缩,伪元素的边框样式从div本身继承.
- 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;
- }
- <div></div>
工作示例 – 带文本
通过上面的例子,内容将不会被包含在整个形状内.相反,它将被限制在div的半宽度内.内容需要包裹在< span>宽度为200%,可以将其打印到divs约束之外.
- 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;
- }
- <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>