当前位置: 首页 > news >正文

HTML 和 CSS 来实现毛玻璃效果(Glassmorphism)

毛玻璃效果简介

        它的主要特征就是半透明的背景,以及阴影和边框。

同时还要为背景加上模糊效果,使得背景之后的元素根据自身内容产生漂亮的“变形”效果,示例:

Glassmorphism

 代码实现

首先,创建一个 HTML 文件,写入如下内容:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Glassmorphism effect</title>
</head>
<body><!-- code goes here -->
</body>
</html>

为 body 标签添加一些样式,并使用鲜艳的颜色和渐变作为背景:

body {padding: 4.5rem;margin: 0;background: #edc0bf;background: linear-gradient(90deg, #edc0bf 0,#c4caef 58%);font-family: 'Inter', sans-serif;
}

现在基本的标签和样式做好了,接下来在 body 标签里创建一个卡片元素:

<div class="card"><h3 class="card-title">Glassmorphism is awesome</h3><p>A modern CSS UI library based on the glassmorphism design principles that will help you quickly design and build beautiful websites and applications.</p><a href="https://ui.glass">Read more</a>
</div>

添加样式

.card {width: 400px;height: auto;padding: 2rem;border-radius: 1rem;
}.card-title {margin-top: 0;margin-bottom: .5rem;font-size: 1.2rem;
}p, a {font-size: 1rem;
}a {color: #4d4ae8;text-decoration: none;
}

 你要做的只是设置一个半透明背景颜色,并使用 backdrop-filter 属性设置模糊效果。在 .card 元素的样式中加入以下 CSS:

.card {/* other styles */background: rgba(255, 255, 255, .7);-webkit-backdrop-filter: blur(10px);backdrop-filter: blur(10px);
}

 

后方没有形状时的效果

让我们使用 img 标签在 body 起始标签之后加入一个形状图片:

<img class="shape" src="https://s3.us-east-2.amazonaws.com/ui.glass/shape.svg" alt="">

 并为 .shape 元素添加以下样式,将其定位在页面的合适位置:

.shape {position: absolute;width: 150px;top: .5rem;left: .5rem;
}

效果如下图所示:

 

 完整代码html和css代码如下:

glass.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" type="text/css" href="glass.css"><title>Glassmorphism effect</title>
</head>
<body><img class="shape" src="https://s3.us-east-2.amazonaws.com/ui.glass/shape.svg" alt=""><!-- code goes here -->
<div class="card"><h3 class="card-title">Glassmorphism is awesome</h3><p>A modern CSS UI library based on the glassmorphism design principles that will help you quickly design and build beautiful websites and applications.</p><a href="https://www.baidu.com">Read more</a>
</div>
</body>
</html>

glass.css:

body {padding: 4.5rem;margin: 0;background: #edc0bf;background: linear-gradient(90deg, #edc0bf 0,#c4caef 58%);font-family: 'Inter', sans-serif;
}.card {width: 400px;height: auto;padding: 2rem;border-radius: 1rem;background: rgba(255, 255, 255, .7);webkit-backdrop-filter: blur(10px);backdrop-filter: blur(10px);
}.card-title {margin-top: 0;margin-bottom: .5rem;font-size: 1.2rem;
}p, a {font-size: 1rem;
}a {color: #4d4ae8;text-decoration: none;
}.shape {position: absolute;width: 150px;top: .5rem;left: .5rem;
}

 

http://www.lryc.cn/news/136092.html

相关文章:

  • 【技术】国标GB28181视频平台EasyGBS通过对应密钥上传到其他平台展示的详细步骤
  • SpeedBI数据可视化工具:浏览器上做分析
  • 8.21笔记
  • MyBatis-Plus中公共字段的统一处理
  • SQL的导出与导入
  • 记录一次wordpress项目的发布过程
  • HTML详解连载(8)
  • Linux系统之安装OneNav个人书签管理器
  • 主程技术分享: 游戏项目帧同步,状态同步如何选
  • ChatGPT-4: 半年的深度使用思考
  • 【健康医疗】Axure用药提醒小程序原型图,健康管理用药助手原型模板
  • ERROR in static/js/xxx.js from UglifyJs
  • 阿里云ECS服务器安装PostgreSQL
  • 【核磁共振成像】傅里叶重建
  • Camunda 工作流节点跳转 - 多实例节点判断和跳转
  • MySQL不停重启问题
  • ol-cesium 暴露 Cesium viewer 对象以及二三维切换、viewer 添加点功能示例
  • 国产化-达梦数据库安装2
  • 延长OLED透明屏的使用寿命:关键因素与有效方法分享
  • 域名是指哪一部分?
  • MPP 还是主流架构吗
  • ubuntu查看网速
  • 【官方中文文档】Mybatis-Spring #使用 MyBatis API
  • go gorm belong to也就是多对一的情况
  • 亚马逊云科技 云技能孵化营——机器学习心得
  • Django实现音乐网站 ⒀
  • PySide6学习笔记--基础环境的安装配置
  • 算法通关村第九关——中序遍历与搜索树
  • 测试框架pytest教程(5)运行失败用例-rerun failed tests
  • 【车载开发系列】UDS当中的时间参数