利用邊框顏色對比模擬 3D 立體感:
background-color 微調增強真實感<div id="navigation">
<ul>
<li><a href="#">11</a></li>
<li><a href="#">22</a></li>
<li><a href="#">33</a></li>
<li><a href="#">44</a></li>
</ul>
</div>
#navigation a:link, #navigation a:visited {
margin-right: 0.2em;
padding: 0.2em 0.6em;
color: #A62020;
background: #FCE6EA;
border-top: 1px solid #FFF; /* 高光 */
border-left: 1px solid #FFF; /* 高光 */
border-bottom: 1px solid #717171; /* 陰影 */
border-right: 1px solid #717171; /* 陰影 */
text-decoration: none;
}
#navigation a:hover {
border-top: 1px solid #717171; /* 反轉:變陰影 */
border-left: 1px solid #717171;
border-bottom: 1px solid #FFF; /* 反轉:變高光 */
border-right: 1px solid #FFF;
}
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D 仿實體按鈕導航</title>
<style>
* { box-sizing: border-box; }
body { font: 14px/1.5 system-ui, sans-serif; padding: 2rem; background: #f5f5f5; }
.btn-nav { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-3d {
--bg: #fce6ea;
--fg: #a62020;
--highlight: #fff;
--shadow: #717171;
--depth: 4px;
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 60px;
padding: 8px 16px;
font: inherit;
font-weight: 600;
color: var(--fg);
text-decoration: none;
background: var(--bg);
border: none;
border-radius: 6px;
cursor: pointer;
/* 3D 效果核心 */
box-shadow:
0 1px 0 var(--highlight) inset,
0 -1px 0 var(--shadow) inset,
0 var(--depth) 0 var(--shadow),
0 calc(var(--depth) + 2px) 4px rgba(0,0,0,0.15);
transform: translateY(0);
transition: all 0.1s cubic-bezier(0.4, 0, 0.2, 1);
user-select: none;
-webkit-tap-highlight-color: transparent;
}
.btn-3d:hover {
background: color-mix(in srgb, var(--bg) 85%, black);
transform: translateY(-1px);
box-shadow:
0 1px 0 var(--highlight) inset,
0 -1px 0 var(--shadow) inset,
0 calc(var(--depth) + 1px) 0 var(--shadow),
0 calc(var(--depth) + 3px) 6px rgba(0,0,0,0.2);
}
.btn-3d:active,
.btn-3d.pressed {
background: color-mix(in srgb, var(--bg) 70%, black);
transform: translateY(var(--depth));
box-shadow:
0 1px 0 var(--shadow) inset,
0 -1px 0 var(--highlight) inset,
0 1px 2px rgba(0,0,0,0.3) inset;
}
.btn-3d:focus-visible {
outline: 2px solid var(--fg);
outline-offset: 2px;
}
/* 不同顏色變體 */
.btn-3d.primary { --bg: #dbeafe; --fg: #1e40af; --highlight: #fff; --shadow: #93c5fd; }
.btn-3d.success { --bg: #dcfce7; --fg: #166534; --highlight: #fff; --shadow: #86efac; }
.btn-3d.warning { --bg: #fef9c3; --fg: #854d0e; --highlight: #fff; --shadow: #fde047; }
.btn-3d.danger { --bg: #fee2e2; --fg: #991b1b; --highlight: #fff; --shadow: #fca5a5; }
</style>
</head>
<body>
<nav class="btn-nav" aria-label="主導航">
<a href="#" class="btn-3d">首頁</a>
<a href="#" class="btn-3d primary">產品</a>
<a href="#" class="btn-3d success">服務</a>
<a href="#" class="btn-3d warning">價格</a>
<a href="#" class="btn-3d danger">聯絡</a>
</nav>
<script>
// 鍵盤支援:Space/Enter 觸發按下狀態
document.querySelectorAll('.btn-3d').forEach(btn => {
btn.addEventListener('keydown', e => {
if (e.key === ' ' || e.key === 'Enter') {
e.preventDefault();
btn.classList.add('pressed');
}
});
btn.addEventListener('keyup', e => {
if (e.key === ' ' || e.key === 'Enter') {
btn.classList.remove('pressed');
}
});
// 觸控支援
btn.addEventListener('touchstart', () => btn.classList.add('pressed'), { passive: true });
btn.addEventListener('touchend', () => btn.classList.remove('pressed'));
});
</script>
</body>
</html>
使用 --bg, --fg, --highlight, --shadow, --depth 定義設計語彙,變體只需覆寫變數。
background: color-mix(in srgb, var(--bg) 85%, black); /* 懸停變深 15% */
background: color-mix(in srgb, var(--bg) 70%, black); /* 按下變深 30% */
無需預先計算十六進位色碼,瀏覽器原生運算。
| 層級 | 用途 | 正常 | 懸停 | 按下 |
|---|---|---|---|---|
| 1 | 內部上高光 | 0 1px 0 var(--highlight) inset | 同 | 0 1px 0 var(--shadow) inset (反轉) |
| 2 | 內部下陰影 | 0 -1px 0 var(--shadow) inset | 同 | 0 -1px 0 var(--highlight) inset (反轉) |
| 3 | 外部立體厚度 | 0 4px 0 var(--shadow) | 0 5px 0 var(--shadow) | 0 4px 0 var(--shadow) (被 translateY 抵消視覺) |
| 4 | 外部投影 | 0 6px 4px rgba(0,0,0,.15) | 0 8px 6px rgba(0,0,0,.2) | 0 1px 2px rgba(0,0,0,.3) inset |
transform: translateY() 啟用 GPU 加速,不觸發佈局重排translateY(4px) 配合 4px 厚度,視覺上按鈕「完全按下」:focus-visible:鍵盤導航時顯示明確焦點環aria-pressed:切換型按鈕需標記狀態role="button":若使用 <div> 模擬按鈕(不建議)touchstart/touchend 同步視覺回饋:root {
--btn-height: 40px;
--btn-radius: 8px;
--btn-depth: 4px;
--btn-font: 600 14px/1 system-ui;
--shadow-umbra: rgba(0,0,0,0.15);
--shadow-penumbra: rgba(0,0,0,0.1);
--shadow-ambient: rgba(0,0,0,0.05);
}
.btn {
height: var(--btn-height);
border-radius: var(--btn-radius);
font: var(--btn-font);
box-shadow:
0 1px 0 var(--highlight) inset,
0 -1px 0 var(--shadow) inset,
0 var(--btn-depth) 0 var(--shadow),
0 calc(var(--btn-depth) + 2px) 4px var(--shadow-umbra);
}