CSS的对角形状

时间:2016-09-02 07:55:49

标签: css diagonal

使用纯CSS创建对角线形状like this的最佳方法是什么?可能只在内容div上使用:after和:before伪元素?感谢。

2 个答案:

答案 0 :(得分:1)

一种方法是在容器上设置背景并使用CSS变换进行旋转。

这是一个简单的例子,根据需要使用尽可能多的重叠容器:



body {
    margin: 0;
    overflow-x: hidden;
    font-family: monospace;
    color: #fff;
}
.container {
    width: 110vw;
    margin-top:10vh;
    margin-left: -10vw;
    padding-left: 10vw;
    background: red;
    box-shadow: 0 0 15px rgba(0, 0, 0, .33);
    transform: rotate(-5deg);
}
.container-inner {
    padding: 10vw;
    max-width: 80vw;
    transform: rotate(5deg);
}
h1 {
   margin-left: 10%;
   padding-top: 50px;
}

<div class="container">
    <h1>Diagonal Background</h1>
    <div class="container-inner">
        <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis.
       </p>
   </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

也许吧:

&#13;
&#13;
.content {
  position: relative;
  perspective: 100vw;
  padding: 30px;
}

.content::before {
    content: '';
    position: absolute;
    z-index: -1;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    width: 130%;
    background: tomato;
    transform-origin: 100% 50%;
    transform: translate(-25%) rotateY(-20deg);
    outline: 1px solid transparent;
}
&#13;
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repudiandae, laudantium.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate voluptatum recusandae quos soluta impedit! Deleniti facere enim, quia ut quisquam.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Deleniti eligendi dignissimos adipisci voluptatem voluptate ex maiores consequuntur, cum ducimus voluptatum, ratione similique asperiores reiciendis, odit quis sed. Reprehenderit doloribus, delectus.
</div>
&#13;
&#13;
&#13;