• 【css揭秘】- 47个不可不知的 css 技巧(上篇0-19)


    我正在参与掘金创作者训练营第6期,点击了解活动详情

    前言

    《css 揭秘》这本书以案例的形式,介绍了 47 个网页设计经典难题的解决方案,我在学习之余将其一一记录下来,方便回顾,本文介绍前 19个案例效果及代码。

    在线预览 play.csssecrets.io

    1-半透明边框

    难题

    当你用 rgba() 或者 hsla() ,写了一个半透明的颜色,给到容器边框时候,你会发现,没有半透明效果,这是因为默认情况下,背景色会填充到边框区域,导致边框的半透明效果失效,当把 border 样式设置为 dashed 时候,你会很直观的发现这一点。

    方案

    使用 background-clip 属性调整上面的默认效果,这个属性的默认值为 border-box,此时背景会被元素的border 给遮盖,它还可以配置 padding-box || content-box, 此时浏览器将以内边距或内容区外沿来渲染。 修改后,border 的半透明效果就生效了。 

    拓展

    background-clip 还有个 text 属性,很有意思,当设置为text 后,背景会被裁剪成文字的前景色。 

    2-多重边框

    难题

    使用 border 来生成单一的边框很容易,但是若想生成多重边框就做不到了,通常需要使用各种 hack 例如使用多个元素来模拟实现。

    方案1: box-shadow

    一个正值的扩张半径加上两个为零的偏移量以及为零的模糊值,得到的“投影”其实就是一条实线;再结合 box-shadow 的逗号分隔语法,来创建任意数量的投影。

     注意:

    1. 投影行为跟边框不完全一致
    2. 生成的边框默认出现在元素外圈,可以加上 inset 关键字来使投影绘制在元素的内圈,注意预留足够的内边距来腾出足够的间隙

    方案2 : outline

    如果只需要两层边框,可以在常规边框的基础上,增加 outline(描边)属性来产生外层的边框,特点是比较灵活。

    总结

    这两种方案都可以实现多重边框的效果,但是outline 只适用于双层边框的场景,如果需要更多层边框,可以用 box-shadow 来实现,另外这两种方案都有一个潜在的缺陷,采用时一定要在不同的浏览器中测试好最终效果。

    3-灵活的背景定位

    难题

    想要的效果:使背景图片针对某个角进行准确的偏移定位?

    方案1 background-position

    方案2 background-origin

    这种方案的优点是,当内边距改变时,其会自动进行位置偏移更新,不用重新声明新的偏移量。

    方案3 calc()

    calc() 也可以结合 background-position 进行准确的计算偏移量,达到同样的效果。注意 calc() 函数内部的 - 和 + 运算符两侧需要各加一个空白符,否则会解析错误。

    1. background-position: calc(100% - 20px) calc(100% - 10px);
    2. 复制代码

    4-边框内圆角

    想要的效果如下,只显示内部圆角,外部仍然为矩角 

    方案1双元素叠加

    使用双 div 元素叠加来实现

    1. <div class="parent">
    2. <div class="child">div>
    3. div>
    4. .parent {
    5. margin: 100px auto;
    6. width: 400px;
    7. height: 200px;
    8. background: #655;
    9. padding: 0.8em;
    10. }
    11. .child {
    12. height: 170px;
    13. background: tan;
    14. padding: 1em;
    15. border-radius: 0.8em;
    16. }
    17. 复制代码

    方案2 单元素

    这种方案在书中提到是个 hack, 果然我写这篇文章的时候,验证了谷歌浏览器中 outline 的样式会跟着border-radius 走,所以这个方案基本已经失效了。

    1. div {
    2. outline: .6em solid #655;
    3. box-shadow: 0 0 0 .4em #655; /* todo calculate max of this */
    4. max-width: 10em;
    5. border-radius: .8em;
    6. padding: 1em;
    7. margin: 1em;
    8. background: tan;
    9. font: 100%/1.5 sans-serif;
    10. }
    11. 复制代码

    5-条纹背景

    介绍

    传统方案都是用 svg 或者图片来解决,这里直接使用 css 来创建条纹图案

    1. // 水平条纹效果
    2. background: linear-gradient(#fb3 50%, #58a 50%);
    3. background-size: 100% 30px;
    4. // 垂直条纹效果
    5. background: linear-gradient(to right, #fb3 50%, #58a 0);
    6. background-size: 30px 100%;
    7. // 斜向条纹
    8. background: linear-gradient(45deg, #fb3 50%, #58a 0);
    9. background-size: 30px 30px;
    10. // 同色系条纹
    11. background: repeating-linear-gradient(30deg, #79b, #79b 15px, #58a 0, #58a 30px);
    12. background: #58a;
    13. // 同色系条纹 重构
    14. background: repeating-linear-gradient(
    15. 30deg,
    16. hsla(0, 0%, 100%, .1),
    17. hsla(0, 0%, 100%, .1) 15px,
    18. transparent 0,
    19. transparent 30px);
    20. background: #58a;
    21. 复制代码

    6-复杂的背景图案

    桌布图案

    1. /**
    2. * Checkerboard
    3. */
    4. background: #eee;
    5. background-image:
    6. linear-gradient(45deg, rgba(0,0,0,.25) 25%, transparent 0, transparent 75%, rgba(0,0,0,.25) 0),
    7. linear-gradient(45deg, rgba(0,0,0,.25) 25%, transparent 0, transparent 75%, rgba(0,0,0,.25) 0);
    8. background-position: 0 0, 15px 15px;
    9. background-size: 30px 30px;
    10. min-height: 100%;
    11. /**
    12. * Polka dot
    13. */
    14. background: #655;
    15. background-image: radial-gradient(tan 20%, transparent 0),
    16. radial-gradient(tan 20%, transparent 0);
    17. background-size: 30px 30px;
    18. background-position: 0 0, 15px 15px;
    19. 复制代码

    棋盘

    1. /**
    2. * Checkerboard
    3. */
    4. background: #eee;
    5. background-image:
    6. linear-gradient(45deg, rgba(0,0,0,.25) 25%, transparent 0, transparent 75%, rgba(0,0,0,.25) 0),
    7. linear-gradient(45deg, rgba(0,0,0,.25) 25%, transparent 0, transparent 75%, rgba(0,0,0,.25) 0);
    8. background-position: 0 0, 15px 15px;
    9. background-size: 30px 30px;
    10. min-height: 100%;
    11. /**
    12. * Checkerboard with SVG
    13. */
    14. background: #eee url('data:image/svg+xml,\
    15. \
    16. \
    17. \
    18. ');
    19. background-size: 30px 30px;
    20. 复制代码

    角向渐变

    1. /**
    2. * Conic gradients test
    3. * PASS if green gradient, FAIL if red.
    4. */
    5. background: red;
    6. background: conic-gradient(limegreen, green, limegreen);
    7. min-height: 100%;
    8. 复制代码

    7-伪随机背景

    难点:重复的平铺图案虽然美观,但是不太自然,下面介绍增加随机性背景的方法 

    1. /**
    2. * Pseudorandom stripes
    3. */
    4. background: hsl(20, 40%, 90%);
    5. background-image:
    6. linear-gradient(90deg, #fb3 11px, transparent 0),
    7. linear-gradient(90deg, #ab4 23px, transparent 0),
    8. linear-gradient(90deg, #655 23px, transparent 0);
    9. background-size: 83px 100%, 61px 100%, 41px 100%;
    10. 复制代码

    8-连续的图像边框

    难点:通常这种效果是借助双 Dom 来实现,一个作为背景图,一个作为内容;我们的改进方案是基于一个元素来实现的。

    1. /**
    2. * Basic border-image demo
    3. */
    4. div {
    5. border: 40px solid transparent;
    6. border-image: 33.334% url('data:image/svg+xml,<svg xmlns="http:%2F%2Fwww.w3.org%2F2000%2Fsvg" width="30" height="30"> \
    7. <circle cx="5" cy="5" r="5" fill="%23ab4"%2F><circle cx="15" cy="5" r="5" fill=" %23655"%2F> \
    8. <circle cx="25" cy="5" r="5" fill="%23e07"%2F><circle cx="5" cy="15" r="5" fill=" %23655"%2F> \
    9. <circle cx="15" cy="15" r="5" fill="hsl(15, 25%, 75%)"%2F> \
    10. <circle cx="25" cy="15" r="5" fill=" %23655"%2F><circle cx="5" cy="25" r="5" fill="%23fb3"%2F> \
    11. <circle cx="15" cy="25" r="5" fill=" %23655"%2F><circle cx="25" cy="25" r="5" fill="%2358a"%2F><%2Fsvg>');
    12. padding: 1em;
    13. max-width: 20em;
    14. font: 130%/1.6 Baskerville, Palatino, serif;
    15. }
    16. div:nth-child(2) {
    17. margin-top: 1em;
    18. border-image-repeat: round;
    19. }

    利用上面的条纹背景

    1. /**
    2. * Vintage envelope themed border
    3. */
    4. div {
    5. padding: 1em;
    6. border: 1em solid transparent;
    7. background: linear-gradient(white, white) padding-box,
    8. repeating-linear-gradient(-45deg, red 0, red 12.5%, transparent 0, transparent 25%,
    9. #58a 0, #58a 37.5%, transparent 0, transparent 50%) 0 / 6em 6em;
    10. max-width: 20em;
    11. font: 100%/1.6 Baskerville, Palatino, serif;
    12. }

    9-自适应的椭圆

    难点:border-radius 其实可以单独指定水平和垂直半径,用斜杠(/)分隔这两个值即可。利用这个特性可以创建椭圆圆角。

    椭圆

    1. /**
    2. * Flexible ellipse
    3. */
    4. div {
    5. width: 20em;
    6. height: 10em;
    7. background: #fb3;
    8. border-radius: 50%;
    9. }

    半椭圆

    1. /**
    2. * Flexible half ellipse
    3. */
    4. div {
    5. display: inline-block;
    6. width: 16em;
    7. height: 10em;
    8. margin: 1em;
    9. background: #fb3;
    10. border-radius: 50% / 100% 100% 0 0;
    11. }
    12. div:nth-of-type(2) { border-radius: 50% / 0 0 100% 100%; }
    13. div:nth-of-type(3) { border-radius: 100% 0 0 100% / 50%; }
    14. div:nth-of-type(4) { border-radius: 0 100% 100% 0 / 50%; }

    四分之一椭圆

    1. /**
    2. * Flexible quarter ellipse
    3. */
    4. div {
    5. display: inline-block;
    6. width: 16em;
    7. height: 10em;
    8. margin: 1em;
    9. background: #fb3;
    10. border-radius: 100% 0 0 0;
    11. }
    12. div:nth-of-type(2) { border-radius: 0 100% 0 0; }
    13. div:nth-of-type(3) { border-radius: 0 0 100% 0; }
    14. div:nth-of-type(4) { border-radius: 0 0 0 100%; }

    10-平行四边形

    背景知识: 基本的 css 变形 transform: skewx()

    潜套元素方案

    可以解决文字变形的问题

    1. /**
    2. * Parallelograms — with extra HTML element
    3. */
    4. <a href="#yolo" class="button"><div>Click mediv>a>
    5. <button class="button"><div>Click mediv>button>
    6. .button { transform: skewX(45deg); }
    7. .button > div { transform: skewX(-45deg); }
    8. .button {
    9. display: inline-block;
    10. padding: .5em 1em;
    11. border: 0; margin: .5em;
    12. background: #58a;
    13. color: white;
    14. text-transform: uppercase;
    15. text-decoration: none;
    16. font: bold 200%/1 sans-serif;
    17. }

    伪元素方案

    1. /**
    2. * Parallelograms — with pseudoelements
    3. */
    4. <a href="#yolo" class="button"><div>Click mediv>a>
    5. <button class="button"><div>Click mediv>button>
    6. .button {
    7. position: relative;
    8. display: inline-block;
    9. padding: .5em 1em;
    10. border: 0; margin: .5em;
    11. background: transparent;
    12. color: white;
    13. text-transform: uppercase;
    14. text-decoration: none;
    15. font: bold 200%/1 sans-serif;
    16. }
    17. .button::before {
    18. content: ''; /* To generate the box */
    19. position: absolute;
    20. top: 0; right: 0; bottom: 0; left: 0;
    21. z-index: -1;
    22. background: #58a;
    23. transform: skew(45deg);
    24. }

    11-菱形图片

    介绍两种使用 css 制作菱形图片的方案,相对于设计师提供裁切好的图片,更加灵活 

    基于变形的方案

    1. /**
    2. * Diamond images — via transforms
    3. */
    4. <div class="diamond">
    5. <img src="http://placekitten.com/200/300" />
  • .diamond {
  • width: 250px;
  • height: 250px;
  • transform: rotate(45deg);
  • overflow: hidden;
  • margin: 100px;
  • }
  • .diamond img {
  • max-width: 100%;
  • transform: rotate(-45deg) scale(1.42);
  • z-index: -1;
  • position: relative;
  • }
  • 裁切路径方案

    1. /**
    2. * Diamond images — via clip-path
    3. */
    4. img {
    5. max-width: 250px;
    6. margin: 20px;
    7. -webkit-clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
    8. clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
    9. transition: 1s;
    10. }
    11. img:hover {
    12. -webkit-clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
    13. clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
    14. }

    12-切角效果

    背景知识:css 渐变,background-size,'条纹背景'

    切角是一种流行的设计风格,使用css 制作切角可以实现更加灵活和多样的颜色效果

    渐变方案

    1. <div>Hey, focus! You’re er!div>
    2. /**
    3. * Beveled corners — with gradients
    4. */
    5. div {
    6. background: #58a;
    7. background: linear-gradient(135deg, transparent 15px, #58a 0) top left,
    8. linear-gradient(-135deg, transparent 15px, #58a 0) top right,
    9. linear-gradient(-45deg, transparent 15px, #58a 0) bottom right,
    10. linear-gradient(45deg, transparent 15px, #58a 0) bottom left;
    11. background-size: 50% 50%;
    12. background-repeat: no-repeat;
    13. padding: 1em 1.2em;
    14. max-width: 12em;
    15. color: white;
    16. font: 150%/1.6 Baskerville, Palatino, serif;
    17. }

    弧形切角

    渐变技巧的一个变种 

    1. /**
    2. * Scoop corners
    3. */
    4. div {
    5. background: #58a;
    6. background: radial-gradient(circle at top left, transparent 15px, #58a 0) top left,
    7. radial-gradient(circle at top right, transparent 15px, #58a 0) top right,
    8. radial-gradient(circle at bottom right, transparent 15px, #58a 0) bottom right,
    9. radial-gradient(circle at bottom left, transparent 15px, #58a 0) bottom left;
    10. background-size: 50% 50%;
    11. background-repeat: no-repeat;
    12. padding: 1em 1.2em;
    13. max-width: 12em;
    14. color: white;
    15. font: 130%/1.6 Baskerville, Palatino, serif;
    16. }

    内联 SVG 与 border-image 方案

    1. /**
    2. * Beveled corners — with border-image and an inline SVG
    3. */
    4. div {
    5. border: 21px solid transparent;
    6. border-image: 1 url('data:image/svg+xml,\
    7. <svg xmlns="http://www.w3.org/2000/svg" width="3" height="3" fill="%2358a">\
    8. <polygon points="0,1 1,0 2,0 3,1 3,2 2,3 1,3 0,2" />\
    9. svg>');
    10. background: #58a;
    11. background-clip: padding-box;
    12. padding: .2em .3em;
    13. max-width: 12em;
    14. color: white;
    15. font: 150%/1.6 Baskerville, Palatino, serif;
    16. }

    裁切路径方案

     强烈推荐这种,可以比较方便的制作大量不同颜色的切角图片,只需要改变背景色就好。

    1. /**
    2. * Beveled corners — with clip-path
    3. */
    4. div {
    5. background: #58a;
    6. -webkit-clip-path:
    7. polygon(20px 0, calc(100% - 20px) 0, 100% 20px, 100% calc(100% - 20px),
    8. calc(100% - 20px) 100%,
    9. 20px 100%, 0 calc(100% - 20px), 0 20px);
    10. clip-path:
    11. polygon(20px 0, calc(100% - 20px) 0, 100% 20px, 100% calc(100% - 20px),
    12. calc(100% - 20px) 100%,
    13. 20px 100%, 0 calc(100% - 20px), 0 20px);
    14. padding: 1em 1.2em;
    15. max-width: 12em;
    16. color: white;
    17. font: 150%/1.6 Baskerville, Palatino, serif;
    18. }

    13-梯形标签页

    背景知识:基本的3D变形,“平行四边形”

    方案1: 伪元素制作两条斜边,border 制作上下平行边 方案2: 3D 旋转,普通的3D 旋转会有副作用比如 文字的变形

    1. <nav>
    2. <a href="#">Homea>
    3. <a href="#" class="selected">Projectsa>
    4. <a href="#">Abouta>
    5. nav>
    6. <main>
    7. Content area
    8. main>
    9. <nav class="left">
    10. <a href="#">Homea>
    11. <a href="#" class="selected">Projectsa>
    12. <a href="#">Abouta>
    13. nav>
    14. <main>
    15. Content area
    16. main>
    17. <nav class="right">
    18. <a href="#">Homea>
    19. <a href="#" class="selected">Projectsa>
    20. <a href="#">Abouta>
    21. nav>
    22. <main>
    23. Content area
    24. main>
    25. /**
    26. * Trapezoid tabs
    27. */
    28. body {
    29. padding: 40px;
    30. font: 130%/2 Frutiger LT Std, sans-serif;
    31. }
    32. nav {
    33. position: relative;
    34. z-index: 1;
    35. padding-left: 1em;
    36. }
    37. nav > a {
    38. position: relative;
    39. display: inline-block;
    40. padding: .3em 1em 0;
    41. color: inherit;
    42. text-decoration: none;
    43. margin: 0 -.3em;
    44. }
    45. nav > a::before,
    46. main {
    47. border: .1em solid rgba(0,0,0,.4);
    48. }
    49. nav a::before {
    50. content: ''; /* To generate the box */
    51. position: absolute;
    52. top: 0; right: 0; bottom: 0; left: 0;
    53. z-index: -1;
    54. border-bottom: none;
    55. border-radius: .5em .5em 0 0;
    56. background: #ccc linear-gradient(hsla(0,0%,100%,.6), hsla(0,0%,100%,0));
    57. box-shadow: 0 .15em white inset;
    58. transform: scale(1.1, 1.3) perspective(.5em) rotateX(5deg);
    59. transform-origin: bottom;
    60. }
    61. nav a.selected { z-index: 2;}
    62. nav a.selected::before {
    63. background-color: #eee;
    64. margin-bottom: -.08em;
    65. }
    66. main {
    67. display: block;
    68. margin-bottom: 1em;
    69. background: #eee;
    70. padding: 1em;
    71. border-radius: .15em;
    72. }
    73. nav.left > a::before {
    74. transform: scale(1.2, 1.3) perspective(.5em) rotateX(5deg);
    75. transform-origin: bottom left;
    76. }
    77. nav.right { padding-left: 2em; }
    78. nav.right > a::before {
    79. transform: scale(1.2, 1.3) perspective(.5em) rotateX(5deg);
    80. transform-origin: bottom right;
    81. }

    14-简单的饼图 

    基于transtorm 和 animation

    1. /**
    2. * Animated pie chart
    3. */
    4. .pie {
    5. width: 100px; height: 100px;
    6. border-radius: 50%;
    7. background: yellowgreen;
    8. background-image: linear-gradient(to right, transparent 50%, currentColor 0);
    9. color: #655;
    10. }
    11. .pie::before {
    12. content: '';
    13. display: block;
    14. margin-left: 50%;
    15. height: 100%;
    16. border-radius: 0 100% 100% 0 / 50%;
    17. background-color: inherit;
    18. transform-origin: left;
    19. animation: spin 3s linear infinite, bg 6s step-end infinite;
    20. }
    21. @keyframes spin {
    22. to { transform: rotate(.5turn); }
    23. }
    24. @keyframes bg {
    25. 50% { background: currentColor; }
    26. }

    1. /**
    2. * Static pie charts
    3. */
    4. .pie {
    5. display: inline-block;
    6. position: relative;
    7. width: 100px;
    8. line-height: 100px;
    9. border-radius: 50%;
    10. background: yellowgreen;
    11. background-image: linear-gradient(to right, transparent 50%, #655 0);
    12. color: transparent;
    13. text-align: center;
    14. }
    15. @keyframes spin {
    16. to { transform: rotate(.5turn); }
    17. }
    18. @keyframes bg {
    19. 50% { background: #655; }
    20. }
    21. .pie::before {
    22. content: '';
    23. position: absolute;
    24. top: 0; left: 50%;
    25. width: 50%; height: 100%;
    26. border-radius: 0 100% 100% 0 / 50%;
    27. background-color: inherit;
    28. transform-origin: left;
    29. animation: spin 50s linear infinite, bg 100s step-end infinite;
    30. animation-play-state: paused;
    31. animation-delay: inherit;
    32. }

    SVG 方案

    1. /**
    2. * Pie charts — with SVG
    3. */
    4. .pie {
    5. width: 100px;
    6. height: 100px;
    7. display: inline-block;
    8. margin: 10px;
    9. transform: rotate(-90deg);
    10. }
    11. svg {
    12. background: yellowgreen;
    13. border-radius: 50%;
    14. }
    15. circle {
    16. fill: yellowgreen;
    17. stroke: #655;
    18. stroke-width: 32;
    19. }
    20. @keyframes grow { to { stroke-dasharray: 100 100 } }
    21. .pie.animated circle {
    22. animation: grow 2s infinite linear;
    23. }

    15-单侧投影

    单侧投影

    1. /**
    2. * One-sided shadow
    3. */
    4. div {
    5. width: 1.6in;
    6. height: 1in;
    7. background: #fb3;
    8. box-shadow: 0 5px 4px -4px black;
    9. }

    邻边投影

    1. /**
    2. * One-sided shadow
    3. */
    4. div {
    5. width: 1.6in;
    6. height: 1in;
    7. background: #fb3;
    8. box-shadow: 3px 3px 6px -3px black;
    9. }

    双侧投影

    1. /**
    2. * One-sided shadow
    3. */
    4. div {
    5. width: 1.6in;
    6. height: 1in;
    7. background: #fb3;
    8. box-shadow: 5px 0 5px -5px black,
    9. -5px 0 5px -5px black;
    10. }
    11. 复制代码

    16-不规则投影

    1. <div class="speech">Speech bubblediv>
    2. <div class="dotted">Dotted borderdiv>
    3. <div class="cutout">Cutout cornersdiv>
    4. /**
    5. * Irregular drop-shadows
    6. */
    7. div {
    8. position: relative;
    9. display: inline-flex;
    10. flex-direction: column;
    11. justify-content: center;
    12. vertical-align: bottom;
    13. box-sizing: border-box;
    14. width: 5.9em;
    15. height: 5.2em;
    16. margin: .6em;
    17. background: #fb3;
    18. /*box-shadow: .1em .1em .3em rgba(0,0,0,.5);*/
    19. -webkit-filter: drop-shadow(.1em .1em .1em rgba(0,0,0,.5));
    20. filter: drop-shadow(.1em .1em .1em rgba(0,0,0,.5));
    21. font: 200%/1.6 Baskerville, Palatino, serif;
    22. text-align: center;
    23. }
    24. .speech {
    25. border-radius: .3em;
    26. }
    27. .speech::before {
    28. content: '';
    29. position: absolute;
    30. top: 1em;
    31. right: -.7em;
    32. width: 0;
    33. height: 0;
    34. border: 1em solid transparent;
    35. border-left-color: #fb3;
    36. border-right-width: 0;
    37. }
    38. .dotted {
    39. background: transparent;
    40. border: .3em dotted #fb3;
    41. }
    42. .cutout {
    43. border: .5em solid #58a;
    44. border-image: 1 url('data:image/svg+xml,\
    45. width="3" height="3" fill="%23fb3">\
    46. \
    47. ');
    48. background-clip: padding-box;
    49. }

    17-染色效果

    基于滤镜的方案

    1. /**
    2. * Color tinting — with filters
    3. */
    4. img {
    5. max-width: 640px;
    6. transition: 1s filter, 1s -webkit-filter;
    7. -webkit-filter: sepia() saturate(4) hue-rotate(295deg);
    8. filter: sepia() saturate(4) hue-rotate(295deg);
    9. }
    10. img:hover,
    11. img:focus {
    12. -webkit-filter: none;
    13. filter: none;
    14. }

    基于混合模式的方案

    1. /**
    2. * Color tinting — with blending modes
    3. */
    4. .tinted-image {
    5. width: 300px; height: 440px;
    6. background-size: cover;
    7. background-color: hsl(335, 100%, 50%);
    8. background-blend-mode: luminosity;
    9. transition: .5s background-color;
    10. }
    11. .tinted-image:hover {
    12. background-color: transparent;
    13. }

    18-毛玻璃效果

    背景知识:PGBA/HSLA 颜色 , filter: blur()

    1. /**
    2. * Frosted glass effect
    3. */
    4. body {
    5. min-height: 100vh;
    6. box-sizing: border-box;
    7. margin: 0;
    8. padding-top: calc(50vh - 6em);
    9. font: 150%/1.6 Baskerville, Palatino, serif;
    10. }
    11. body, main::before {
    12. background: url("http://placekitten.com/200/300") 0 / cover fixed;
    13. }
    14. main {
    15. position: relative;
    16. margin: 0 auto;
    17. padding: 1em;
    18. max-width: 23em;
    19. background: hsla(0,0%,100%,.25) border-box;
    20. overflow: hidden;
    21. border-radius: .3em;
    22. box-shadow: 0 0 0 1px hsla(0,0%,100%,.3) inset,
    23. 0 .5em 1em rgba(0, 0, 0, 0.6);
    24. text-shadow: 0 1px 1px hsla(0,0%,100%,.3);
    25. }
    26. main::before {
    27. content: '';
    28. position: absolute;
    29. top: 0; right: 0; bottom: 0; left: 0;
    30. margin: -30px;
    31. z-index: -1;
    32. -webkit-filter: blur(20px);
    33. filter: blur(20px);
    34. }
    35. blockquote { font-style: italic }
    36. blockquote cite { font-style: normal; }
    37. 复制代码

    19-折角效果

    背景知识: css 变形, css 渐变, “切角效果”

    45度折角的解决方案

    1. /**
    2. * Folded corner effect
    3. */
    4. div {
    5. width: 12em;
    6. background: #58a; /* Fallback */
    7. background:
    8. linear-gradient(to left bottom, transparent 50%, rgba(0,0,0,.4) 0) 100% 0 no-repeat,
    9. linear-gradient(-135deg, transparent 1.5em, #58a 0);
    10. background-size: 2em 2em, auto;
    11. padding: 2em;
    12. color: white;
    13. font: 100%/1.6 Baskerville, Palatino, serif;
    14. }
    15. 复制代码

    其他角度的解决方案

    1. /**
    2. * Folded corner effect — at an angle
    3. */
    4. div {
    5. position: relative;
    6. width: 12em;
    7. background: #58a; /* Fallback */
    8. background: linear-gradient(-150deg, transparent 1.5em, #58a 0);
    9. padding: 2em;
    10. color: white;
    11. font: 100%/1.6 Baskerville, Palatino, serif;
    12. border-radius: .5em;
    13. }
    14. div::before {
    15. content: '';
    16. position: absolute;
    17. top: 0; right: 0;
    18. width: 1.73em; height: 3em;
    19. background: linear-gradient(to left bottom, transparent 50%, rgba(0,0,0,.2) 0, rgba(0,0,0,.4)) 100% 0 no-repeat;
    20. transform: translateY(-1.3em) rotate(-30deg);
    21. transform-origin: bottom right;
    22. border-bottom-left-radius: .5em;
    23. box-shadow: -.2em .2em .3em -.1em rgba(0,0,0,.15)
    24. }

     总结给大家推荐一个实用面试题库

     1、前端面试题库 (面试必备)            推荐:★★★★★

    地址:前端面试题库

  • 相关阅读:
    22Excel
    直播课堂系统01-数据库表设计
    Opencv_4_图像像素的读写操作
    指针练习(2)
    开发需知的文件加密与解密
    Matplotlib(三)通过plt.subplots创建子绘图
    前端部署iis后axios跨域请求问题
    解决electron-builder制作的安装包在安装过程中出现“安装中止”的问题
    一文带你了解MySQL之redo日志
    Day801.内存问题排查方案 -Java 性能调优实战
  • 原文地址:https://blog.csdn.net/weixin_42981560/article/details/126678775