one sided skew with css

帅比萌擦擦* 提交于 2019-11-28 12:43:19

问题


I want to create a one sided skew, but failed to do so. here is what I've tried so far:

    .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>

but it is not working. look:

what I want to accomplish:


回答1:


Add overflow:hidden to your .bg-style1 rule and transformX(-40%) to the transform: in .bg-style1:after

.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%);
}
<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>



回答2:


Is your problem because of parents background showing up at the left of skewed block. If yes then you should use 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>


来源:https://stackoverflow.com/questions/30279790/one-sided-skew-with-css

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!