一、自适应椭圆

border-radius 可以给 div 的四个角上做圆角,参数可以写椭圆的横向半径和纵向半径。

  1. .exercise1-1 {
  2. background: tan;
  3. border-radius: 50% / 50%;
  4. }
  5. .exercise1-2 {
  6. background: tan;
  7. border-radius: 50% / 100% 100% 0 0;
  8. }
  9. .exercise1-3 {
  10. background: tan;
  11. border-radius: 100% 0 0 100% / 50%;
  12. }
  13. .exercise1-4 {
  14. background: tan;
  15. border-radius: 100% 0 0 0 / 100% 0 0 0;
  16. }

image.png

二、平行四边形

transform 属性中的 skew 函数可以使元素做倾斜,它做了全部的倾斜,如果子元素想回正,就把它做反向倾斜。使用伪元素也可以达到这样的效果,需要注意的是设置一下 z-index,放在文字的下面。

  1. .exercise2-11 {
  2. background: tan;
  3. transform: skewX(-45deg);
  4. }
  5. .exercise2-12 {
  6. transform: skewX(45deg);
  7. }
  8. .exercise2-2 {
  9. position: relative;
  10. }
  11. .exercise2-2:after {
  12. position: absolute;
  13. content: '';
  14. background: tan;
  15. transform: skewX(-45deg);
  16. top: 0;
  17. left: 0;
  18. bottom: 0;
  19. right: 0;
  20. z-index: -1;
  21. }

image.png

三、菱形图片

菱形图片可以使用 transform 中 rotate 函数做旋转 45°,也可以直接在原图上做裁剪。

  1. .exercise3-11 {
  2. margin: 50px;
  3. transform: rotate(45deg);
  4. overflow: hidden;
  5. }
  6. .exercise3-11 .exercise3-12 {
  7. vertical-align: middle;
  8. transform: rotate(-45deg) scale(1.42);
  9. }
  10. .exercise3-2 {
  11. clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
  12. }
  13. .exercise3-3 {
  14. clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
  15. transition: 1s clip-path;
  16. }
  17. .exercise3-3:hover {
  18. clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%)
  19. }

image.png

「@浪里淘沙的小法师」