CSS

1、边框条纹效果

2021-05-26-14-04-42-513916.png

代码思路

在原先的元素下方创建一个有条纹背景的伪元素,并保证原先元素覆盖住它就行,这样就模拟了边框的效果。
可以使用 repeating-linear-gradient 来实现条纹背景。

HTML 代码

  1. <div class="card w-80">
  2. <div class="border-stripe rounded-xl">
  3. Lorem ipsum...
  4. </div>
  5. </div>

CSS 代码

  1. .border-stripe {
  2. --stripe-width: 0.5rem;
  3. --stripe-deg: -45deg;
  4. --stripe-color-1: var(--grey-color-1);
  5. --stripe-offset-1: 2px;
  6. --stripe-color-2: var(--skin-color-2);
  7. --stripe-offset-2: 1rem;
  8. --stripe-radius: 15px;
  9. --stripe-inset: calc(var(--stripe-width) * -1);
  10. &::before {
  11. @include inset(var(--stripe-inset));
  12. content: "";
  13. z-index: -1;
  14. background: repeating-linear-gradient(
  15. var(--stripe-deg),
  16. var(--stripe-color-1) 0 var(--stripe-offset-1),
  17. var(--stripe-color-2) 0 var(--stripe-offset-2)
  18. );
  19. border-radius: var(--stripe-radius);
  20. }
  21. }

2、不规则形状

2021-05-26-14-04-42-570764.png

代码思路

中间是一个矩形,矩形下方有 2 个三角形,左右 2 侧各有一个被裁切过的矩形。使用 clip-path 这个属性就可以了。

HTML 代码

  1. <div class="ribbon">
  2. Pure CSS Ribbon
  3. <div class="block"></div>
  4. <div class="block"></div>
  5. <div class="block"></div>
  6. <div class="block"></div>
  7. </div>

CSS 代码

  1. .ribbon {
  2. --ribbon-color-1: var(--yellow-color-1);
  3. --ribbon-color-2: var(--yellow-color-2);
  4. --ribbon-color-3: var(--yellow-color-3);
  5. position: relative;
  6. padding: 0.5rem 1rem;
  7. color: white;
  8. background: var(--ribbon-color-1);
  9. .block {
  10. &:nth-child(1),
  11. &:nth-child(2) {
  12. position: absolute;
  13. bottom: -20%;
  14. width: 20%;
  15. height: 20%;
  16. background: var(--ribbon-color-2);
  17. clip-path: polygon(0 0, 100% 100%, 100% 0);
  18. }
  19. &:nth-child(1) {
  20. left: 0;
  21. }
  22. &:nth-child(2) {
  23. right: 0;
  24. transform: scaleX(-1); //这起到了水平翻转的作用,它可以防止再写一遍clip-path
  25. }
  26. &:nth-child(3),
  27. &:nth-child(4) {
  28. position: absolute;
  29. z-index: -1;
  30. top: 20%;
  31. width: 40%;
  32. height: 100%;
  33. background: var(--ribbon-color-3);
  34. clip-path: polygon(0 0, 25% 50%, 0 100%, 100% 100%, 100% 0);
  35. }
  36. &:nth-child(3) {
  37. left: -20%;
  38. }
  39. &:nth-child(4) {
  40. right: -20%;
  41. transform: scaleX(-1);
  42. }
  43. }
  44. }

3、浮雕效果

2021-05-26-14-04-42-679512.png

代码思路

这是由 2 个同心的元素组成的,于是自然就想到了 inset 这个 Mixin。光泽可以用 box-shadow 来实现,通过叠加多重阴影,就可以模拟出浮雕的效果了。

HTML 代码

  1. <div class="px-6 py-2 text-xl embossed cursor-pointer" data-text="浮雕按钮" style="--emboss-radius: 1.5rem">
  2. 浮雕按钮
  3. </div>

CSS 代码

  1. :root {
  2. --red-color-1: #af2222;
  3. --red-color-2: #c1423e;
  4. --red-color-3: #c62a2a;
  5. --red-color-4: #951110;
  6. --green-color-1: #486433;
  7. --green-color-2: #2b361a;
  8. --red-grad-1: linear-gradient(
  9. to right,
  10. var(--red-color-1) 50%,
  11. var(--red-color-2) 0
  12. );
  13. }
  14. .embossed {
  15. --emboss-radius: 1rem;
  16. --emboss-out: 6px;
  17. --emboss-out-minus: calc(var(--emboss-out) * -1);
  18. --emboss-inset: 2px;
  19. --emboss-inset-minus: calc(var(--emboss-inset) * -1);
  20. --emboss-blur: 1px;
  21. --emboss-bg-1: var(--red-color-3);
  22. --emboss-bg-2: var(--green-color-1);
  23. --emboss-color-1: white;
  24. --emboss-color-2: var(--red-color-4);
  25. --emboss-color-3: var(--green-color-2);
  26. position: relative;
  27. box-sizing: border-box;
  28. white-space: nowrap;
  29. &::before {
  30. @include inset(var(--emboss-out-minus));
  31. content: "";
  32. background: var(--emboss-bg-1);
  33. box-shadow: inset var(--emboss-inset-minus) var(--emboss-inset-minus)
  34. var(--emboss-blur) var(--emboss-color-1),
  35. inset var(--emboss-inset) var(--emboss-inset) var(--emboss-blur)
  36. var(--emboss-color-2);
  37. border-radius: calc(var(--emboss-radius) + var(--emboss-out));
  38. }
  39. &::after {
  40. @include inset;
  41. @include flex-center;
  42. content: attr(data-text);
  43. color: white;
  44. font-weight: bold;
  45. background: var(--emboss-bg-2);
  46. box-shadow: inset var(--emboss-inset) var(--emboss-inset) var(--emboss-blur)
  47. var(--emboss-color-1),
  48. inset var(--emboss-inset-minus) var(--emboss-inset-minus)
  49. var(--emboss-blur) var(--emboss-color-3);
  50. border-radius: var(--emboss-radius);
  51. }
  52. }