按鈕寬度隨文字內容自動調整,不固定寬度,支援多語言、動態內容。傳統「滑動門」技術雖經典,現代 CSS 已有更優雅方案。
.btn {
display: inline-block;
height: 22px;
line-height: 22px;
color: #666;
text-decoration: none;
background: url(btn-bg.png) no-repeat 0 0;
vertical-align: top;
}
.btn i {
display: block;
font-style: normal;
font-size: 12px;
padding: 0 10px;
background: url(btn-bg.png) no-repeat right -22px;
}
/* 狀態變化 */
.btn:hover { background-position: 0 -44px; color: #4c8136; }
.btn:hover i { background-position: right -66px; }
.btn:active { background-position: 0 -88px; }
.btn:active i { background-position: right -110px; }
/* 禁用狀態 */
.btn_disabled,
.btn_disabled:hover {
background-position: 0 -132px;
cursor: default;
}
.btn_disabled i,
.btn_disabled:hover i,
.btn_disabled:active i {
background-position: right -154px;
color: #999;
}
HTML 結構:
<a href="#" class="btn"><i>這是按鈕</i></a>
<a href="#" class="btn"><i>按鈕</i></a>
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 0.5rem 1rem;
font-size: 0.875rem;
font-weight: 500;
line-height: 1.5;
color: #fff;
background-color: #007bff;
border: 1px solid transparent;
border-radius: 0.25rem;
text-decoration: none;
cursor: pointer;
transition: all 0.15s ease-in-out;
white-space: nowrap;
user-select: none;
}
.btn:hover {
background-color: #0069d9;
border-color: #0062cc;
color: #fff;
text-decoration: none;
}
.btn:active,
.btn:focus {
background-color: #0062cc;
border-color: #005cbf;
box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
}
.btn:disabled,
.btn.disabled {
opacity: 0.65;
cursor: not-allowed;
pointer-events: none;
}
/* 尺寸 */
.btn--sm { padding: 0.25rem 0.5rem; font-size: 0.8125rem; }
.btn--lg { padding: 0.75rem 1.5rem; font-size: 1rem; }
/* 樣式 */
.btn--primary { background-color: #007bff; border-color: #007bff; }
.btn--secondary { background-color: #6c757d; border-color: #6c757d; }
.btn--success { background-color: #28a745; border-color: #28a745; }
.btn--danger { background-color: #dc3545; border-color: #dc3545; }
.btn--outline-primary { background: transparent; color: #007bff; }
.btn--outline-primary:hover { background: #007bff; color: #fff; }
/* 狀態 */
.btn--loading { position: relative; color: transparent; }
.btn--loading::after {
content: "";
position: absolute;
width: 1em; height: 1em;
border: 2px solid currentColor;
border-right-color: transparent;
border-radius: 50%;
animation: spin 0.75s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
<button class="btn btn--primary btn--lg">主要大按鈕</button>
<button class="btn btn--outline-secondary">次要外框按鈕</button>
<button class="btn btn--danger btn--loading" disabled>提交中...</button>
<a href="#" class="btn btn--success" role="button">連結樣式按鈕</a>
<button> 或 <a role="button"> 確保語意正確:focus-visible 顯示焦點環(鍵盤導航可見)disabled 屬性而非單純 class 控制禁用狀態aria-busy="true"、aria-live="polite"aria-label 說明用途:root {
--btn-height-sm: 32px;
--btn-height-md: 40px;
--btn-height-lg: 48px;
--btn-radius: 6px;
--btn-font-weight: 500;
--btn-transition: 0.15s ease;
}
.btn {
height: var(--btn-height-md);
border-radius: var(--btn-radius);
font-weight: var(--btn-font-weight);
transition: background var(--btn-transition), border-color var(--btn-transition), box-shadow var(--btn-transition);
}