mask 渐变遮罩属性

图片遮罩

  • hasBgBlock 盒子,背景图为 pic4,仅用于观察渐变过渡效果,可忽略
  • imgBox 盒子
    • 背景图为 pic1
    • 遮罩图层为 mask_front
    • mask-image 的 url 属性生效后,会按照 url 中图片的形状来遮罩背景图


html 复制代码
<div class="hasBgBlock">
  <div class="imgBox"></div>
</div>
<style>
  html,
  body {
    margin: 0;
    padding: 0;
  }

  .hasBgBlock {
    width: 100%;
    background: url("images/pic4.png") no-repeat;
    background-size: cover;
    padding: 80px 0;
  }

  .imgBox {
    width: 640px;
    height: 360px;
    margin: 0 auto;
    background: url("images/pic1.png") no-repeat;
    background-size: cover;
    
    /* 标准属性 */
    mask-image: url('images/mask_front.png');
    mask-size: contain;
    mask-repeat: no-repeat;
    mask-position: center;

    /* 旧版适配 */
    -webkit-mask-image: url('images/mask_front.png');
    -webkit-mask-size: contain;
    -webkit-mask-repeat: no-repeat;
    -webkit-mask-position: center;
  }
</style>

渐变效果

  • hasBgBlock 盒子,背景图为 pic4,仅用于观察渐变过渡效果,可忽略
  • imgBox 盒子
    • 背景图为 pic1
    • 遮罩图层设置为线性渐变从右到左,从透明到显示
    • 色号 #000,可以替换为其他任何非透明颜色
html 复制代码
<div class="hasBgBlock">
  <div class="imgBox"></div>
</div>
<style>
  html,
  body {
    margin: 0;
    padding: 0;
  }

  .hasBgBlock {
    width: 100%;
    background: url("images/pic4.png") no-repeat;
    background-size: cover;
    padding: 80px 0;
  }

  .imgBox {
    width: 640px;
    height: 360px;
    margin: 0 auto;
    background: url("images/pic1.png") no-repeat;
    background-size: cover;

    mask-image: linear-gradient(to right, transparent, #000);
    -webkit-mask-image: linear-gradient(to right, transparent, #000);
  }
</style>

demo

  • 线性渐变 demo
css 复制代码
mask-image: linear-gradient(to right, transparent, #e73d3d 10%, #68cf3f 90%, transparent);
-webkit-mask-image: linear-gradient(to right, transparent, #e73d3d 10%, #68cf3f 90%, transparent);
  • 径向渐变 demo
css 复制代码
mask-image: radial-gradient(circle at center, #fff 30%, transparent 70%);
mask-size: 100% 100%;
mask-repeat: no-repeat;
-webkit-mask-image: radial-gradient(circle at center, #fff 30%, transparent 70%);
-webkit-mask-size: 100% 100%;
-webkit-mask-repeat: no-repeat;
相关推荐
梨想橙汁1 小时前
Webpack 快速上手:前端工程化、打包原理、从零搭建基础项目
前端·webpack·前端工程化
zhanghaha13141 小时前
HTML系列教程:12_HTML 表格 <table> 零基础详解
前端·html
Betterme1 小时前
Vue3 竞态请求防护组合式hooks:useLatestRequest 设计思路
前端
_zhourui_h_1 小时前
一个 [ECS] 到底生成了多少代码?EasyECS 最核心的秘密都在这里
前端
用户850869276151 小时前
ecstore新手避坑指南:从零搭建电商项目实战
css
梨想橙汁2 小时前
Git 分支详解:创建切换、合并分支、冲突解决实战
前端·git
雪芽蓝域zzs2 小时前
Vue3 defineProps` / `defineEmits` 是编译器宏,不需要手动 import 导入
前端·javascript·vue.js
whyweplay2 小时前
Elpis:从Json Schema 到 页面
前端·javascript
做萤石二次开发的哈哈2 小时前
海康班班通交互一体机技能接入实战:ISAPI透传+OTAP双协议封装,Web/App/小程序教学管理应用一站生成
前端·物联网·小程序·交互·萤石开放平台·蓝海aiot一站式工作台·aiot开发