iOS 6 時代的擬物化返回按鈕(左尖右圓、漸變、陰影、內凹)曾是移動端設計標配。雖然扁平化潮流已取代它,但「用 CSS 畫不規則形狀」的思維訓練價值永存。本文拆解如何用**一個標籤 + 一個偽元素**,通過旋轉、縮放、裁切,構建出完美的三角箭頭狀按鈕。
按鈕高 27px,正方形邊長 27 / √2 ≈ 19.09 → 取 19px(或 20px 微調)。
旋轉基點 transform-origin: 0 0(左上角),先 scaleX(0.8) 再 rotate(45deg)(注意順序:後寫先執行)。
主體漸變:linear-gradient(top, #5bbfd8, #449fb6)(垂直)
箭頭漸變:linear-gradient(-45deg, #5bbfd8, #449fb6)(逆時針 45°,與旋轉後方向一致)
.btn-back {
font: 14px/27px Arial, Helvetica, sans-serif;
color: #fff; text-decoration: none;
position: relative; display: inline-block; margin-top: 11px;
}
/* 主體按鈕:左邊框移除、左圓角、z-index 蓋住箭頭右半邊 */
.btn-back span {
display: inline-block; height: 27px; line-height: 27px;
padding: 0 10px 0 5px;
background: linear-gradient(to bottom, #5bbfd8, #449fb6);
border: 1px solid #2c96b2; border-left: none;
border-radius: 2px 5px 5px 2px;
text-shadow: 0 -1px 0 rgba(0,0,0,.4);
margin-left: 12px; position: relative; z-index: 2;
}
/* 箭頭偽元素 */
.btn-back::before {
content: ''; display: inline-block;
width: 20px; height: 20px;
background: linear-gradient(-45deg, #5bbfd8, #449fb6);
border: 1px solid #2c96b2;
transform-origin: 0 0;
transform: scaleX(0.8) rotate(45deg);
border-radius: 3px 2px; /* 微調圓角匹配視覺 */
position: absolute; left: 13px; top: -1px;
}
<a href="/" class="btn-back"><span>首頁</span></a>
| 方案 | 請求數 | 體積 | 可維護性 | 縮放清晰度 |
|---|---|---|---|---|
| 圖片切圖 | 1+ | 2-5 KB | 修改需重切 | 定尺寸,放大模糊 |
| CSS3 純代碼 | 0 | < 500 B | 改顏色/尺寸改數值 | 向量無限縮放 |
box-shadow 難以完全還原transform/gradient,需降級或放棄::before 旋轉 45° 定位)核心心法:**用矩形變換構建非矩形**,而非死記形狀代碼。掌握 transform-origin、scale、rotate 組合,你就擁有了「CSS 繪圖筆」。