當元素 width: 0; height: 0 時,四條邊框在角落以 45° 相交,形成四個三角形。透過控制邊框顏色與寬度,可構建任意方向三角形。
.triangle {
width: 0; height: 0;
border: 50px solid transparent; /* 統一寬度,透明底色 */
border-top-color: #f00; /* 向下三角形:顯示上邊框顏色 */
border-right-color: #0f0; /* 向左三角形 */
border-bottom-color: #00f; /* 向上三角形 */
border-left-color: #000; /* 向右三角形 */
}
<div class="perspective-outer">
<div class="perspective-inner">內容區域</div>
<div class="perspective-r"></div> /* 右側透視面 */
<div class="perspective-b"></div> /* 底部透視面 */
</div>
.perspective-outer {
position: relative;
width: 170px; /* 內容寬 150px + 右透視 20px */
height: 140px; /* 內容高 120px + 底透視 20px */
}
.perspective-inner {
position: relative; /* 建立堆疊上下文,確保內容在透視面之上 */
z-index: 1;
width: 150px;
height: 120px;
border: 1px solid #f60;
background: #fff;
}
/* 右側透視面:左邊框 = 透視深度,上邊框 = 底部透視高度 */
.perspective-r {
position: absolute;
right: 0; top: 0;
width: 0; height: 100px; /* 內容高 120px - 底透視 20px */
border-left: 20px solid #000; /* 透視深度:20px */
border-top: 20px solid #fff; /* 底部透視高度:20px (匹配父級背景) */
border-right: 0; border-bottom: 0;
}
/* 底部透視面:左邊框 = 右側透視深度,上邊框 = 透視深度 */
.perspective-b {
position: absolute;
bottom: 0; left: 0;
width: 150px; /* 外層寬 170px - 右透視 20px */
height: 0;
border-left: 20px solid #fff; /* 右側透視深度填充 */
border-top: 20px solid #000; /* 透視深度:20px */
border-right: 0; border-bottom: 0;
}
@mixin perspective-box($width, $height, $depth: 20px, $color: #000, $bg: #fff) {
position: relative;
width: $width + $depth;
height: $height + $depth;
> .content {
width: $width; height: $height;
background: $bg;
position: relative; z-index: 1;
@content;
}
&::before, &::after { /* 偽元素替代額外 div */
content: "";
position: absolute;
width: 0; height: 0;
}
&::before { /* 右側面 */
right: 0; top: 0;
height: $height;
border-left: $depth solid $color;
border-top: $depth solid $bg;
}
&::after { /* 底部面 */
bottom: 0; left: 0;
width: $width;
border-left: $depth solid $bg;
border-top: $depth solid $color;
}
}
/* 使用 */
.card { @include perspective-box(200px, 150px, 15px, #333, #fff); }
.modern-perspective {
perspective: 500px; /* 觀察者距離 */
perspective-origin: center; /* 觀察點位置 */
}
.perspective-card {
width: 200px; height: 150px;
transform-style: preserve-3d; /* 保持 3D 空間 */
transform: rotateY(-15deg) rotateX(10deg); /* 旋轉產生透視 */
box-shadow:
20px 20px 0 -5px #333, /* 模擬右側面 */
20px 20px 0 #333, /* 模擬底部面 */
10px 10px 30px rgba(0,0,0,.3);
}
/* 更真實:真實 3D 面 */
.real-3d-card {
transform-style: preserve-3d;
}
.real-3d-card__face {
position: absolute;
width: 100%; height: 100%;
backface-visibility: hidden;
}
.real-3d-card__front { transform: translateZ(10px); background: #fff; }
.real-3d-card__right { transform: rotateY(90deg) translateZ(10px); background: #333; width: 20px; }
.real-3d-card__bottom { transform: rotateX(-90deg) translateZ(10px); background: #333; height: 20px; }
| 維度 | Border 三角形透視 | CSS 3D Transforms |
|---|---|---|
| 瀏覽器支援 | IE6+ (極佳) | IE10+ (需前綴) |
| 代碼複雜度 | 高 (手動幾何計算) | 低 (聲明式) |
| 視覺真實度 | 偽 3D (2D 模擬) | 真 3D (透視投影、光照) |
| 動畫性能 | 佈局重排 (差) | GPU 加速 (優) |
| 內容交互 | 正常文檔流 | 3D 空間內交互複雜 |
| 適用場景 | 靜態裝飾、兼容舊環境 | 交互式 3D、動畫、遊戲 UI |
.perspective-inner 需 overflow: hidden 防止內容滲出透視面clip-path 或 3D Transformscalc() 或視口單位aria-hidden="true"Border 三角形透視是 CSS 幾何繪圖的經典技巧,展示了開發者在受限條件下的創造力。現代項目首選 CSS 3D Transforms + GPU 加速,僅在極舊環境維護時使用 Border 方案。理解其幾何本質,有助於掌握 CSS 視覺渲染的底層邏輯。