คัตเอาต์รูปทรงต่างๆ

ฟีเจอร์บนเว็บ มาสก์

ภาพรวม

การมาสก์ CSS ช่วยให้คุณตัดองค์ประกอบเป็นรูปร่างที่กำหนดเองได้ เช่น การเพิ่มรอยบากลงในการ์ดหรือการสร้างเส้นขอบที่มีรูปร่าง เมื่อรวมรูปร่างสำหรับเลย์เอาต์ที่ซับซ้อน ให้เลือกกลยุทธ์การมาสก์ตามประเภทเนื้อหาที่องค์ประกอบมี ดังนี้

กลยุทธ์การมาสก์ เหมาะสำหรับ ผลกระทบของข้อความ
การมาสก์ SVG ขององค์ประกอบโดยตรง รูปภาพ ไอคอน รูปทรงต่างๆ ที่ใช้ตกแต่ง รูปทรงที่ซับซ้อน ไม่แนะนำ (อาจครอบตัดข้อความ)
การมาสก์ SVG ขององค์ประกอบที่อยู่ติดกัน การ์ดที่มีข้อความ เนื้อหาสำคัญ ข้อความยังคงอ่านได้ครบถ้วน
Pure CSS Gradients รูปทรงเรขาคณิตแบบเรียบง่าย ไม่แนะนำ (อาจครอบตัดข้อความ)

การใช้งาน

วิธีใช้คัตเอาต์รูปทรงต่างๆ

การใช้มาสก์ SVG

มาสก์ SVG ช่วยให้คุณกำหนดรูปร่างที่ลบออกจากหรือเพิ่มลงในพื้นที่ที่มองเห็นได้โดยใช้การเติมสีขาว (แสดง) และสีดำ (ซ่อน)

ความสว่างเทียบกับการมาสก์อัลฟ่า: มาสก์ SVG จะมีค่าเริ่มต้นเป็นโหมดความสว่าง ซึ่งเป็นเหตุผลที่เราใช้ fill="white" เพื่อแสดงพื้นที่และ fill="black" เพื่อตัดพื้นที่ออก หากต้องการใช้ความโปร่งใส (ช่องอัลฟ่า) ของ SVG แทนสี คุณสามารถตั้งค่า mask-type: alpha; ใน CSS หรือ mask-type="alpha" ในองค์ประกอบ <mask> ของ SVG

การมาสก์ SVG ขององค์ประกอบโดยตรง

เมื่อไม่มีข้อความภายในองค์ประกอบที่ต้องกังวล (เช่น อวาตาร์โปรไฟล์ รูปภาพผลิตภัณฑ์ หรือภาพประกอบ) คุณสามารถใช้มาสก์ SVG กับองค์ประกอบได้โดยตรง

การใช้ maskContentUnits="objectBoundingBox" ภายในมาสก์ SVG ช่วยให้มั่นใจได้ว่ามาสก์จะปรับขนาดตามความกว้างและความสูงขององค์ประกอบที่ใช้โดยอัตโนมัติ

<!-- 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 ขององค์ประกอบที่อยู่ติดกัน

ใช้มาสก์กับองค์ประกอบที่อยู่ติดกันแทนองค์ประกอบระดับบนที่มีข้อความ วิธีนี้ช่วยให้มั่นใจได้ว่าข้อความจะไม่ถูกตัดและยังคงอ่านได้

วิธีแก้ปัญหานี้คือจัดโครงสร้างคอมโพเนนต์เป็นเลย์เอาต์ 2 ส่วน ดังนี้

  1. คอนเทนเนอร์ข้อความที่ปลอดภัยและไม่ได้ปกปิด (.card-body) สำหรับเนื้อหาที่อ่านได้ทั้งหมด
  2. เปล่าที่ใช้ตกแต่งdiv (.card-accent) ซึ่งวางอยู่ข้างๆ ซึ่งจะได้รับมาสก์ SVG ที่กำหนดเอง

การกำหนดสีพื้นหลังเดียวกันให้กับทั้ง 2 ส่วนจะทำให้ทั้ง 2 ส่วนรวมกันเป็นคอมโพเนนต์รูปทรงที่กำหนดเองเพียงชิ้นเดียว

<!-- 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 Gradient เดียวสำหรับการตัดภาพแบบง่าย

เมื่อต้องการเพียงแค่การตัดรูปทรงเรขาคณิตแบบง่าย (เช่น รอยบากด้านบนครึ่งวงกลม รอยเว้าด้านข้าง หรือการตัดแนวทแยงตรง) คุณไม่จำเป็นต้องเขียนหรืออ้างอิง SVG ภายนอก แต่คุณสามารถกำหนดการไล่ระดับสีแบบรัศมีหรือแบบเส้นตรงของ CSS ได้โดยตรงภายในพร็อพเพอร์ตี้ mask-image

<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 ตกแต่งที่อยู่ติดกันจะยังคงเป็นสี่เหลี่ยมผืนผ้าทึบที่ไม่มีมาสก์ เนื่องจากใช้สีพื้นหลังเดียวกันกับคอนเทนเนอร์ข้อความ การ์ดทั้งหมดจะแสดงผลเป็นคอนเทนเนอร์สี่เหลี่ยมผืนผ้ามาตรฐานที่ดูสะอาดตา
  • การเพิ่มประสิทธิภาพแบบก้าวหน้า: การเก็บเลเยอร์ข้อความไว้นอกมาสก์ทั้งหมดจะช่วยรับประกันว่าเนื้อหาจะยังคงอ่านได้ มีโครงสร้าง และเข้าถึงได้ในเบราว์เซอร์รุ่นเก่า