剪裁圖片

網頁功能: 遮罩

總覽

CSS 遮罩可讓您將元素裁剪成自訂形狀,例如在卡片中加入凹口或建立形狀框線。組合形狀來建立複雜版面配置時,請根據元素包含的內容類型,選擇遮罩策略:

遮蓋策略 支援的裝置 文字影響
直接元素 SVG 遮罩 圖片、圖示、裝飾性形狀、複雜形狀 不建議 (可能會裁剪文字)
相鄰元素 SVG 遮罩 含文字和重要內容的資訊卡 文字仍清晰可讀
Pure CSS Gradients 簡單幾何形狀 不建議 (可能會裁剪文字)

導入作業

如要導入形狀挖孔:

使用 SVG 遮罩

SVG 遮罩可讓您使用白色 (顯示) 和黑色 (隱藏) 填色,定義要從可見區域中減去或加入的形狀。

亮度與 Alpha 遮罩:SVG 遮罩預設為「亮度」模式,因此我們使用 fill="white" 顯示區域,並使用 fill="black" 剪裁區域。如要使用 SVG 的透明度 (Alpha 色板) 而非顏色,可以在 CSS 中設定 mask-type: alpha;,或在 SVG <mask> 元素上設定 mask-type="alpha"。

直接元素 SVG 遮罩

如果元素內沒有文字需要考量 (例如個人資料顯示圖片、產品圖片或插圖),可以直接將 SVG 遮罩套用至元素。

在 SVG 遮罩中使用 maskContentUnits="objectBoundingBox",可確保遮罩會根據套用元素的寬度和高度自動縮放。

<!-- 1. Define the mask in SVG (hidden from view) -->
<svg width="0" height="0" style="position: absolute;" aria-hidden="true">
  <defs>
    <!-- objectBoundingBox makes the mask scale from 0 to 1 along the element's borders -->
    <mask id="splat-mask" maskContentUnits="objectBoundingBox">
      <!-- Organic Bezier path defining an artistic paint splat shape -->
      <path d="M 0.5 0.05 C 0.58 0.05, 0.58 0.21, 0.67 0.18 C 0.76 0.15, 0.79 0.08, 0.85 0.15 C 0.91 0.22, 0.83 0.32, 0.89 0.38 C 0.95 0.44, 1.03 0.45, 0.99 0.55 C 0.95 0.65, 0.82 0.62, 0.8 0.72 C 0.78 0.82, 0.89 0.92, 0.8 0.97 C 0.71 1.02, 0.64 0.88, 0.55 0.93 C 0.46 0.98, 0.44 1.06, 0.35 1.01 C 0.26 0.96, 0.31 0.81, 0.22 0.79 C 0.13 0.77, 0.01 0.88, 0.01 0.77 C 0.01 0.66, 0.16 0.62, 0.14 0.52 C 0.12 0.42, -0.02 0.39, 0.03 0.29 C 0.08 0.19, 0.23 0.27, 0.29 0.19 C 0.35 0.11, 0.3 0.01, 0.4 0.01 C 0.5 0.01, 0.42 0.05, 0.5 0.05 Z" fill="white" />
    </mask>
  </defs>
</svg>

<!-- 2. Apply the mask to the image element -->
<img src="avatar.jpg" alt="User Profile" class="shaped-avatar">

<style>
.shaped-avatar {
  width: 200px;
  height: 200px;
  object-fit: cover;
  
  /* Apply the SVG mask ID with standard and webkit-prefixed properties */
  -webkit-mask-image: url(#splat-mask);
  mask-image: url(#splat-mask);
}
</style>

相鄰元素 SVG 遮罩

請將遮罩套用至相鄰元素,而非包含文字的父項元素。這可確保文字不會遭到裁剪,且仍可閱讀。

為解決這個問題,請將元件結構分為兩部分:

  1. 安全、未遮蓋的文字容器 (.card-body),用於所有可讀內容。
  2. 旁邊放置空白裝飾 div (.card-accent),用於接收自訂 SVG 遮罩。

只要為這兩個部分指派相同的背景顏色,就能在視覺上合併成單一自訂形狀的元件。

<!-- 1. Define the SVG mask (hidden from view) -->
<svg width="0" height="0" style="position: absolute;" aria-hidden="true">
  <defs>
    <!-- Use objectBoundingBox to make the mask scale with the element -->
    <mask id="accent-stencil" maskContentUnits="objectBoundingBox">
      <!-- Fill the entire area with white (fully visible) -->
      <rect width="1" height="1" fill="white" />
      <!-- Draw a black shape to cut out a concave curve. Extending the path to x=1.1 guarantees it fully clears the right edge, preventing subpixel lines -->
      <path d="M 1.1,0 C 0.4,0.1 0.4,0.9 1.1,1 L 1.1,0 Z" fill="black" />
    </mask>
  </defs>
</svg>

<!-- 2. Create the unified card layout -->
<div class="unified-card">
  <!-- The text-bearing element remains unmasked and perfectly rectangular -->
  <div class="card-body">
    <h3>Premium Membership</h3>
    <p>Get exclusive weekly updates on modern web standards and premium UI designs.</p>
  </div>
  <!-- The empty accent element next to it is masked to form the custom shape -->
  <div class="card-accent"></div>
</div>

<style>
.unified-card {
  display: flex;
  width: 400px;
}

.card-body {
  flex: 1;
  background-color: #1e293b; /* Elegant slate color */
  color: #f8fafc;
  padding: 24px;
  border-top-left-radius: 16px;
  border-bottom-left-radius: 16px;
}

.card-accent {
  width: 60px;
  background-color: #1e293b; /* Same background color so they merge visually without seams */
  border-top-right-radius: 16px;
  border-bottom-right-radius: 16px;

  /* Reference the SVG mask */
  -webkit-mask-image: url(#card-accent-mask);
  mask-image: url(#card-accent-mask);
}
</style>

使用單一 CSS 漸層建立簡單的鏤空效果

如果只需要簡單的幾何圖形切口 (例如半圓形頂端凹口、側邊凹口或直角斜切),則不需要編寫或參照外部 SVG。您可以改為直接在 mask-image 屬性中定義 CSS 放射狀或線性漸層。

<div class="gradient-masked-card">
  <h3>Notched Coupon Card</h3>
  <p>This card uses a pure CSS radial gradient to cut out a semi-circular notch along its top edge.</p>
</div>

<style>
.gradient-masked-card {
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  color: white;
  padding: 40px 24px 24px 24px; /* Extra top padding ensures content clears the notch */
  border-radius: 16px;
  text-align: center;

  /* radial-gradient places a circle at 50% (horizontal center) and 0% (top edge), cutting out a 20px transparent notch */
  -webkit-mask-image: radial-gradient(circle at 50% 0%, transparent 20px, black 21px);
  mask-image: radial-gradient(circle at 50% 0%, transparent 20px, black 21px);
}
</style>

備用策略

如果瀏覽器不支援 mask-image 或前置版本:

  • 形狀圖片:元素會正常降級,顯示為一般矩形元素,並採用預設備用樣式。
  • 造型卡片:相鄰的裝飾性 .card-accent 元素會維持實心、未遮蓋的矩形。由於背景顏色與文字容器相同,因此整個資訊卡會呈現為標準、簡潔的矩形容器。
  • 漸進增強:將文字圖層完全保留在遮罩外,可確保內容在舊版瀏覽器上仍能完整閱讀、結構化且可存取。