skew

  1. div {
  2. width: 200px;
  3. height: 100px;
  4. background: #fabc08;
  5. margin-left: 100px;
  6. transform: skew(-45deg);
  7. }

skew,里面的内容也会跟着变形

image.png

使用嵌套

  1. <a href="#" class="button">
  2. <div>我不要变形</div>
  3. </a>

对内容进行反向skew

  1. .button {
  2. width: 100px;
  3. margin-left: 100px;
  4. background: #fabc08;
  5. display: block;
  6. transform: skew(-45deg);
  7. }
  8. .button > div {
  9. transform: skew(45deg);
  10. }

image.png

使用伪元素

  1. div {
  2. width: 100px;
  3. height: 50px;
  4. margin-left: 100px;
  5. position: relative;
  6. line-height: 50px;
  7. text-align: center;
  8. }
  9. div::before {
  10. content: '';
  11. position: absolute;
  12. left: 0;top: 0;bottom: 0;right: 0; /* 变成和div一样大小 */
  13. background: #fabc08;
  14. z-index: -1;
  15. transform: skew(-45deg)
  16. }

image.png

菱形

把正方形skew,再rotate就得到菱形了