Trapezium responsive Div with CSS or jQuery

前端 未结 2 956
你的背包
你的背包 2020-12-22 13:17

I have to build this website and I am faced with a unique challenge of alternating Trapezium Divs. I have never come across anything like this before and I have no idea how

相关标签:
2条回答
  • 2020-12-22 13:30

    You could use the SkewX transform to skew a div's before element:

    div {
      height: 50px;
      display: inline-block;
      background: lightgray;
      padding: 10px;
      line-height: 50px;
      text-align: center;
      position: relative;
      transition: all 0.6s;
      z-index:1;
    }
    div:before {
      content: "";
      position: absolute;
      height: inherit;
      background: inherit;
      top: 0;
      left: 30%;
      height: 70px;
      width: 100%;
      -webkit-transform: skewX(45deg);
      -moz-transform: skewX(45deg);
      transform: skewX(45deg);
      z-index: -1;
    }
    div:hover {
      background: tomato;
    }
    <div>Some text here</div>


    You could do this for many different trapeziums:

    html,
    body {
      margin: 0;
      padding: 0;
      text-align: center;
    }
    body {
      background: blue;
    }
    div {
      height: 50px;
      display: inline-block;
      background: lightgray;
      line-height: 50px;
      text-align: center;
      position: relative;
      transition: all 0.6s;
      cursor: pointer;
      z-index: 1;
    }
    .right:before {
      content: "";
      position: absolute;
      height: inherit;
      background: inherit;
      top: 0;
      left: 50%;
      height: 100%;
      width: 100%;
      -webkit-transform: skewX(45deg);
      -moz-transform: skewX(45deg);
      transform: skewX(45deg);
      z-index: -1;
    }
    div:hover {
      background: tomato;
    }
    .left {
      margin-left: 50px;
    }
    .right {
      margin-right: 50px;
    }
    .left:after {
      content: "";
      position: absolute;
      height: inherit;
      background: inherit;
      top: 0;
      left: -50%;
      height: 100%;
      width: 100%;
      -webkit-transform: skewX(-45deg);
      -moz-transform: skewX(-45deg);
      transform: skewX(-45deg);
      z-index: -1;
    }
    <div class="right">Some Text saying</div>
    
    <br/>
    <br/>
    <div class="left">how much I love the</div>
    
    <br/>
    <br/>
    <div class="left right">MINIONS!</div>

    0 讨论(0)
  • 2020-12-22 13:31

    SVG

    If you define multiple trapzoids in svg you can reuse them inside divs to get the shape you desire. Defining a template of shapes makes the shapes reusable.
    To reuse them simply define a <use xlink:href="#targetid"> element

    .example {
      width: 400px;
      height: 200px;
      border: 2px solid pink;
    }
    .template {
      display: none;
    }
    <svg class="template" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
      <def>
        <polygon id="trapzoid" points="0,20 100,0 100,100 0,80" />
        <polygon id="trapzoid2" points="20,0 80,0 100,100, 0,100" />
      </def>
    </svg>
    
    <div class="example">
      <svg width="100%" height="100%" preserveAspectRatio="none" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
        <use xlink:href="#trapzoid" />
      </svg>
    </div>
    <div class="example">
      <svg width="100%" height="100%" preserveAspectRatio="none" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
        <use xlink:href="#trapzoid2" />
      </svg>
    </div>

    0 讨论(0)
提交回复
热议问题