当前位置:Gxlcms > css > 纯CSS实现底层毛玻璃效果(代码示例)

纯CSS实现底层毛玻璃效果(代码示例)

时间:2021-07-01 10:21:17 帮助过:25人阅读

本篇文章给大家带来的内容是关于纯CSS实现底层毛玻璃效果(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

毛玻璃背景是一个很常见的网页样式,想要实现,其实并不难,但经过我在网上的搜索发现,大量实现方法都较为不规范,且把问题复杂化了(例如各种z-index属性和position的定位)

现提供一个代码很直白且实现效果良好的实现方案,改良自W3Schools

HTML部分

  1. <!DOCTYPE html>
  2. <html dir="ltr">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>FrostedGlass</title>
  6. <link rel="stylesheet" href="frostedGlass.css">
  7. </head>
  8. <body>
  9. <div>
  10. <div>
  11. <p>this is FrostedGlass</p>
  12. </div>
  13. </div>
  14. </body>
  15. </html>

.mainHolder是主框体
.textHolder是毛玻璃区域
.p是浮于毛玻璃上的文字内容

CSS部分

  1. * {
  2. box-sizing: border-box;
  3. }
  4. .mainHolder {
  5. width: 600px;
  6. height: 600px;
  7. background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/skyscrapers.jpg);
  8. background-attachment: fixed;
  9. background-position: center;
  10. background-size: cover;
  11. position: relative;
  12. }
  13. .textHolder {
  14. width: 100%;
  15. height: 200px;
  16. position: absolute;
  17. right: 0;
  18. bottom: 0;
  19. background: inherit;
  20. overflow: hidden;
  21. }
  22. .textHolder::before {
  23. content: '';
  24. position: absolute;
  25. top:0;
  26. right: 0;
  27. bottom: 0;
  28. left: 0;
  29. background: inherit;
  30. background-attachment: fixed;
  31. filter: blur(4px);
  32. }
  33. .textHolder::after {
  34. content: "";
  35. position: absolute;
  36. top:0;
  37. right: 0;
  38. bottom: 0;
  39. left: 0;
  40. background: rgba(0, 0, 0, 0.25);
  41. }
  42. p {
  43. z-index: 1;
  44. color: white;
  45. position: relative;
  46. margin: 0;
  47. }

解决毛玻璃效果里最核心的问题:模糊效果不能影响字体,采用了伪元素::after于::before
值得注意的是,在p标签里的position属性。设置为relative后,会将p从被遮挡状态“提起来”。
另外,对于不同的浏览器内核,filter的写法会有些许不同。

以上就是纯CSS实现底层毛玻璃效果(代码示例)的详细内容,更多请关注Gxlcms其它相关文章!

人气教程排行