<div> 疊加模擬 3D 按鈕凹凸質感<a> 承載連結功能,<span> 承載樣式<div id="menu">
<!-- 按鈕 1 -->
<div class="cond">
<div class="conc">
<div class="conb">
<div class="cona">
<div class="black">
<a class="switch" href="#nogo">
<span class="white">
<span class="blackc" title="Item one">ITEM ONE</span>
</span>
</a>
</div>
</div>
</div>
</div>
</div>
<!-- 按鈕 2-5 結構相同,僅文字不同 -->
<div class="cond">...</div>
<div class="cond">...</div>
<div class="cond">...</div>
<div class="cond">...</div>
</div>
#menu {
display: block;
position: relative;
background: #edebdc;
width: 112px;
padding: 25px;
border: 1px solid #000;
margin: 0 auto;
}
五層 div 依序疊加,每層寬高遞增 2px,邊框顏色漸層:
| Class | 寬/高 | 邊框顏色 | 背景 | 視覺角色 |
|---|---|---|---|---|
| .cond | 111×31px | 上/左 #d5d3ca、下/右 #d5dc3a | #fff | 最外層陰影 |
| .conc | 109×29px | 上/左 #9c9b95 | #f7f6ef | 深陰影層 |
| .conb | 107×27px | 上/左 #b8b7af | #f7f6ef | 中陰影層 |
| .cona | 105×25px | 上/左 #d5d3ca | #f7f6ef | 亮面層 |
| .black | 102×22px | 全 #000 | 透明 | 內邊框 |
.white {
position: absolute;
width: 100px; height: 20px;
color: #fff; background: #fff;
border-top: 1px solid #ffdc56;
border-right: 1px solid #957704;
border-bottom: 1px solid #937603;
border-left: 1px solid #ffdc56;
text-align: center; overflow: hidden;
}
.blackc {
position: absolute; top: 0; left: 0;
width: 98px; height: 18px;
color: #000; font: bold 11px Verdana;
border-top: 1px solid #ffd42e;
border-right: 1px solid #caa205;
border-bottom: 1px solid #c9a105;
border-left: 1px solid #ffd42e;
background: #fc0; line-height: 15px;
text-align: center; overflow: hidden;
}
#menu a.switch:hover .white {
border-bottom: 1px solid #ffdc56;
border-left: 1px solid #957704;
border-top: 1px solid #937603;
border-right: 1px solid #ffdc56;
}
#menu a.switch:hover .blackc {
border-bottom: 1px solid #ffd42e;
border-left: 1px solid #caa205;
border-top: 1px solid #c9a105;
border-right: 1px solid #ffd42e;
background: #eb0; line-height: 17px;
}
原理:將「亮邊」與「暗邊」對調,視覺上從「凸起」變為「凹陷」。
<style>
.btn-3d {
display: inline-block;
padding: 8px 20px;
font: bold 13px/1.5 Verdana, sans-serif;
color: #333;
text-decoration: none;
background: linear-gradient(#fffdf0, #f0e6c0);
border: 1px solid #c9b896;
border-radius: 4px;
box-shadow:
0 1px 0 #fff inset, /* 內部高光 */
0 -1px 0 #e0d8b8 inset, /* 內部陰影 */
0 2px 2px rgba(0,0,0,0.2), /* 外部投影 */
0 4px 0 #d4c8a8, /* 底部厚度 */
0 5px 0 #c4b898,
0 6px 6px rgba(0,0,0,0.3);
transition: all 0.1s ease;
margin: 5px;
}
.btn-3d:hover {
background: linear-gradient(#fffdf0, #e8dcb8);
box-shadow:
0 1px 0 #fff inset,
0 -1px 0 #d8c8a0 inset,
0 1px 1px rgba(0,0,0,0.2),
0 2px 0 #d4c8a8,
0 3px 0 #c4b898,
0 4px 4px rgba(0,0,0,0.2);
transform: translateY(1px); /* 微微下沉 */
}
.btn-3d:active {
background: linear-gradient(#f0e6c0, #fffdf0); /* 反轉漸層 */
box-shadow:
0 1px 0 #e0d8b8 inset,
0 -1px 0 #fff inset,
0 0 2px rgba(0,0,0,0.3) inset;
transform: translateY(3px); /* 完全按下 */
}
</style>
<a class="btn-3d" href="#item1">ITEM ONE</a>
<a class="btn-3d" href="#item2">ITEM TWO</a>
<a class="btn-3d" href="#item3">ITEM THREE</a>
| 指標 | 原版多層 DIV | 現代 Box-shadow |
|---|---|---|
| HTML 結構 | 6 層巢狀 DIV | 單一 <a> |
| 維護性 | 調整需改多層數值 | 集中在單一規則 |
| 效能 | 多元素渲染、重排 | GPU 加速、單元素 |
| 語意化 | 大量無語意 DIV | 純粹連結語意 |
| 無障礙 | 需額外 ARIA | 原生連結可聚焦 |
@media (max-width: 480px) {
#menu { width: 100%; padding: 15px; }
.btn-3d { width: 100%; text-align: center; box-sizing: border-box; }
}