单侧倾斜与css

时间:2015-05-16 19:19:37

标签: css skew

我想创建一个单侧偏斜,但未能这样做。这是我到目前为止所尝试的:

    .bg-style1 {
      background: #ccc;
      position: relative;
      display: block;
      z-index: 2;
    }
    .bg-style1:after {
      content: " ";
      position: absolute;
      display: block;
      width: 30%;
      height: 100%;
      top: 0;
      left: 0;
      z-index: -1;
      background: #333333;
      -ms-transform: skew(-30deg, 0deg);
      -webkit-transform: skew(-30deg, 0deg);
      transform: skew(-30deg, 0deg);
    }
<section class="page">
  <div class="bg-style1">
    .....content goes here
  </div>
</section>

但它不起作用。看:enter image description here

我想要完成的任务:enter image description here

2 个答案:

答案 0 :(得分:1)

overflow:hidden添加到.bg-style1规则,将transformX(-40%)添加到transform:

中的.bg-style1:after

&#13;
&#13;
.bg-style1 {
  padding-top: ;
  background: #ccc;
  position: relative;
  display: block;
  z-index: 2;
  overflow: hidden;
}
.bg-style1:after {
  content: " ";
  position: absolute;
  display: block;
  width: 30%;
  height: 100%;
  left: 0;
  top: 0;
  z-index: -1;
  background: #333333;
  -ms-transform: skew(-30deg, 0deg);
  -webkit-transform: skew(-30deg, 0deg);
  transform: skew(-30deg, 0deg) translateX(-50%);
}
&#13;
<section class="page">
  <div class="bg-style1">
    <h1>What is lorem ipsum</h1>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
    survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
    software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
</section>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你的问题是因为父母背景出现在斜块的左边。如果是,那么您应该使用transform-origin: 0 0;

    .bg-style1 {
      background: #ccc;
      position: relative;
      display: block;
      z-index: 2;
    }
    .bg-style1:after {
      content: " ";
      position: absolute;
      display: block;
      width: 30%;
      height: 100%;
      top: 0;
      left: 0;
      z-index: -1;
      background: #333333;
      -ms-transform: skew(-30deg, 0deg);
      -webkit-transform: skew(-30deg, 0deg);
      transform: skew(-30deg, 0deg);
      transform-origin: 0 0;
    }
<section class="page">
  <div class="bg-style1">
    .....content goes here
  </div>
</section>