html – 带有直角梯形的盒子形状

前端之家收集整理的这篇文章主要介绍了html – 带有直角梯形的盒子形状前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道如果这个形状可以在css3中尽可能少的html来完成:

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

  1. .wrapper {
  2. position: relative;
  3. }
  4. .Box {
  5. width: 100px;
  6. height: 100px;
  7. border: 1px solid #000;
  8. position: absolute;
  9. top: 100px;
  10. left: 100px;
  11. }
  12. .Box:before {
  13. content: "";
  14. border: 1px solid #000;
  15. border-bottom: 1px solid #fff;
  16. width: 50%;
  17. height: 10px;
  18. position: absolute;
  19. top: -12px;
  20. left: -1px;
  21. }
  22. .Box:after {
  23. content: "";
  24. border: 1px solid #000;
  25. border-top: 1px solid #fff;
  26. width: 50%;
  27. height: 10px;
  28. position: absolute;
  29. bottom: -12px;
  30. right: -1px;
  31. }
  1. <div class="wrapper">
  2. <div class="Box"></div>
  3. </div>

小提琴是here,但我不知道如何扭曲它,使我在顶部和底部有直角梯形.

解决方法

形状不需要额外的元素

可以使用< div&gt ;:::创建形状
>左边是左边的div,top和bottom的边框.
>右边是由…制成的:之前和它的顶部,右边和底部的边界
>加入两个框的跨度是用以下方式创建的:感谢skewY

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

工作实例 – 只是形状

CSS已被压缩,伪元素的边框样式从div本身继承.

  1. div {
  2. border: solid 4px #000;
  3. border-right-width: 0;
  4. width: 100px;
  5. height: 200px;
  6. position: relative;
  7. }
  8. div:before,div:after {
  9. content: '';
  10. display: block;
  11. height: 100%;
  12. width: 100%;
  13. border: inherit;
  14. border-right-width: 4px;
  15. border-left: none;
  16. position: absolute;
  17. left: 100%;
  18. top: 13px;
  19. margin-left: 20px;
  20. }
  21. div:after {
  22. width: 20px;
  23. border-right: none;
  24. top: 5px;
  25. transform: skewY(40deg);
  26. margin: 0;
  27. }
  1. <div></div>

工作示例 – 带文本

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

  1. div {
  2. border: solid 4px #000;
  3. border-right-width: 0;
  4. width: 100px;
  5. height: 200px;
  6. position: relative;
  7. }
  8. div:before,div:after {
  9. content: '';
  10. display: block;
  11. height: 100%;
  12. width: 100%;
  13. border: inherit;
  14. border-right-width: 4px;
  15. border-left: none;
  16. position: absolute;
  17. left: 100%;
  18. top: 13px;
  19. margin-left: 20px;
  20. }
  21. div:after {
  22. width: 20px;
  23. border-right: none;
  24. top: 5px;
  25. transform: skewY(40deg);
  26. margin: 0;
  27. }
  28. span {
  29. width: 200%;
  30. display: block;
  31. padding: 20px 10px 10px;
  32. }
  1. <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>

猜你在找的HTML相关文章