alt 屬性關鍵字堆砌嫌疑,搜尋引擎更重視真實文字。/ * HTML * /
Buy Widgets
/ * CSS * /
h1 {
width: 300px;
height: 50px;
background: url('buy-widgets.png') no-repeat;
overflow: hidden; /* 關鍵:隱藏超出區塊的文字 */
}
h1 span {
position: absolute;
left: -9999px; /* 將文字推出視窗外 */
/* 或使用 text-indent: -9999px; 於 h1 上 */
}
.ir {
/* 現代圖片替換通用類 */
display: block;
overflow: hidden;
text-indent: 100%; /* 文字縮進超出容器 */
white-space: nowrap; /* 防止換行 */
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
text-indent: 100% 比 -9999px 更安全(避免超長文字導致瀏覽器繪製巨大區塊)。overflow: hidden 徹底隱藏文字。<svg><text>Buy Widgets</text></svg>,矢量縮放、可樣式化、無障礙。display: none 或 visibility: hidden 隱藏文字,螢幕閱讀器將不會朗讀。aria-label 或 <title>(SVG)加強語意。圖片替換文字是「特殊字體無法作為 Web Font 時」的過渡方案。現代專案優先使用 Web Fonts / SVG / @font-face,若必須用圖片替換,請採用 text-indent: 100%; overflow: hidden; white-space: nowrap; 改良版,兼顧 SEO、無障礙與效能。