图像蒙版
Image Mask
图像只保留落在自定义形状内的部分。
适用场景
让图片或插画适应带有鲜明轮廓的构图。 剪裁形状最好能呼应主题,并在不同屏幕宽度下检查主体有没有被边缘切掉。
应用示例
相机商店把镜头的实拍样张放在光圈形开口里,旁边列出镜头规格。页面缩窄时重新对齐裁切位置,让画面中的鸟一直留在开口中央。
AI 提示词
给【目标图像】加图像蒙版。硬边形状用 clip-path,柔边透明区域用 mask-image。保持 object-fit 与 object-position 可调,保留图片原本的替代文字。
示例代码
保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。
查看 HTML
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>图像蒙版</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.visual-specimen {
width: min(100%, 290px);
height: 124px;
display: grid;
place-items: center;
position: relative;
isolation: isolate;
}
.masked-art {
position: relative; width: min(100%, 173px); height: 110px;
background: #8ec5c8; overflow: hidden;
clip-path: polygon(25% 0, 75% 0, 100% 42%, 84% 100%, 15% 100%, 0 42%);
}
.masked-art i { position: absolute; top: 15px; right: 32px; width: 37px; aspect-ratio: 1; border-radius: 50%; background: #f8d99c; }
.masked-art i:last-child {
inset: 40% -5% 0; width: auto; aspect-ratio: auto; border-radius: 0; background: #2c6368;
clip-path: polygon(0 80%, 29% 25%, 44% 62%, 66% 0, 100% 81%, 100% 100%, 0 100%);
}
</style>
</head>
<body>
<main class="demo">
<div class="visual-specimen" aria-hidden="true"><div class="masked-art"><i></i><i></i></div></div>
</main>
</body>
</html>UI Skills 原创示例