將元素寬高設為 0,給予粗邊框,不同顏色邊框交接處形成斜角,即可得到三角形:
.triangle {
width: 0; height: 0;
border-width: 50px;
border-color: #f00 #0f0 #00f #000;
border-style: solid;
}
<div class="perspective-outer">
<div class="perspective-r"></div> /* 右側透視面 */
<div class="perspective-b"></div> /* 底部透視面 */
<div class="perspective-inner">內容區</div>
</div>
.perspective-outer {
position: relative;
width: 170px; /* 內容寬 150px + 右透視 20px */
height: 140px; /* 內容高 120px + 底透視 20px */
}
.perspective-inner {
width: 148px; height: 118px;
border: 1px solid #f60;
background: #fff;
}
.perspective-r, .perspective-b {
position: absolute; width: 0; height: 0;
}
.perspective-r {
right: 0; height: 100px; /* 內容高 120px - 底透視 20px */
border-left: 20px solid #000; /* 右透視厚度 */
border-top: 20px solid #fff; /* 底透視厚度(透明) */
}
.perspective-b {
bottom: 0; width: 150px; /* 外層寬 170px - 右透視 20px */
border-left: 20px solid #fff; /* 右透視厚度(透明) */
border-top: 20px solid #000; /* 底透視厚度 */
}
| 參數 | 對應屬性 | 視覺效果 |
|---|---|---|
| 透視角度 | .perspective-r 的 border-top / .perspective-b 的 border-left 寬度 | 越厚角度越平緩 |
| 透視距離 | .perspective-r 的 border-left / .perspective-b 的 border-top 寬度 | 越厚立體感越強 |
| 透明邊框色 | 父容器背景色(例中為白) | 消除多餘三角形顏色 |
border-radius 於 .perspective-inner 可做圓角透視卡片。box-shadow 疊加更真實陰影(現代瀏覽器)。transition 可做互動式立體翻轉。