最近整理了一份 CSS 文字隱藏的 demo,歸納出幾種方法,本想找出一種最完美的方案放進自己的程式碼片段,無奈到最後卻陷入不斷推翻結論的境地。因為需要考慮的應用場景與元素實在太多——撇開瀏覽器不談,不同的應用終端、不同的標籤結構,各自的最佳方案都不一樣。與其奢望在工作上「偷懶」,不如平日多鞏固基礎知識,需要權衡時才能游刃有餘。
由於自己經驗尚淺,demo 難免有錯漏,若發現問題,還望大家指正。
HTML:<p class="hide_display">我是打醬油的文本</p>
CSS:.hide_display{display:none;}

優點:方便快捷、相容性好。
缺點:螢幕閱讀器無法閱讀、不適用於超鏈接、圖片替代文本需其他標籤的背景、影響搜索排名,大量使用還容易被視為 SEO 作弊。
HTML:<span class="hide_overflow">我是一號打醬油的文本</span> <p class="hide_position">我是二號打醬油的文本</p> <p class="hide_visibility">我是三號打醬油的文本</p>
CSS:
.hide_overflow{ height:0px; overflow:hidden; display:block;/* 行內元素需要 */ float:left; /* 脫離文檔流 或者position:absolute;*/ }
.hide_position{ position:absolute; left:-32767px; }
.hide_visibility{ visibility:hidden; position:absolute; /* 脫離文檔流 */ margin-left:-32767px; }

優點:方便快捷;不希望被螢幕閱讀器讀取的內容可用 visibility。
缺點:不適用於超鏈接、圖片替代文本需其他標籤的背景。
HTML:
<p class="hide_tex"><a href="#">我是打醬油的超鏈接一號</a></p>
<a class="hide_tex_span" href="#"><span>我是打醬油的超鏈接二號</span></a>
<!--全英文字符在IE下不能被隱藏 -->
<input class="hide_tex_input" type="submit" />
<input class="hide_tex_input" type="submit" />
<button class="hide_tex_input">我是打醬油的文本btn</button>
CSS:
.hide_tex a, .hide_tex_span{ text-indent:-32767px; display:block; /* text-indent適用塊狀元素中行內元素和文本節點 */ /* 演示需要 */ width:200px; height:20px; margin-left:300px; background-color:#ccc; /* outline:none; 不建議隱藏outline,鍵盤流選手無法操作 */ }
.hide_tex_input{ text-indent:-32767px; width:200px; height:50px; display:block; }

優點:適用於超鏈接的圖片替代。
缺點:Firefox 有虛邊問題,text-indent 容易產生 bug。
HTML:<p class="hide_fs">我是擦不掉牛皮癬</p> <button class="hide_fs">我是打醬油的文本btn</button> <input class="hide_fs" type="submit" />
CSS:.hide_fs{ font-size:0; }

優點:方便快捷。
缺點:瀏覽器表現不一致(IE、Safari 有最小字號,Chrome 最小 12px,Firefox 不顯示文本),適用環境少。
HTML:
<p class="hide_lh"><a href="#">目前為止被正常人類普遍接受的疑似最完美隱藏文字方案,傳說是tommy發明的</a></p>
<!-- line-height在ff3.6的button無法隱藏文本,需要配合其他方式 -->
<button class="hide_lh_btn">我是打醬油的文本btn</button>
<!-- line-height在ff3.6、opera的input無法隱藏文本,需要配合其他方式 -->
<input class="hide_lh_btn" type="submit" />
<!-- 目前比較完善的方案 -->
<input class="hide_lh_btn_final" type="submit" />
CSS:
/* 設定高度,利用行高將文本撐到容器可視範圍外 */
.hide_lh a, .hide_lh_btn{ width:200px; height:20px; overflow:hidden; line-height: 60px;/* 行高最好設3倍或以上,chrome容易後漏 */ display:block;/* 行內元素需要 */ /* 演示需要 */ background-color:#ccc; margin-left:300px; }
.hide_lh_btn_final{ width:200px; height:20px; overflow:hidden; line-height:60px; display:block; font-size:0px; /* opera和ff支持*/ }

優點:相容性好,超鏈接與圖片替代文本可用。
缺點:使用限制較大,需要固定寬高。
HTML:<a class="hide_bg" href="#"><span class="front_bg"></span>我是可訪問性的化身</a>(在 CSS 無效、CSS 有效但圖片無效時都適用)
CSS:
.hide_bg{ width:200px; height:20px; position:relative; display:block;/* 行內元素需要 */ }
.hide_bg .front_bg{ background:url('bg_text.png') no-repeat; /* 背景內容等於或大於容器大小 */ position:absolute; /* 絕對定位,不影響文本 */ left:0px; top:0px; width:200px;/* 與父元素等寬高 */ height:20px; display:block; /* cursor:pointer; ie6和鏈接需要用 */ }

優點:相容性好,超鏈接與圖片替代文本可用,可訪問性強。
缺點:使用限制較大,需要固定寬高;程式碼冗餘,需要空標籤。
HTML:<a class="hide_ct" href="#">也許我才是最合適的,誰知道呢,內容表現分離。只有opera支持</a>(按定義只能用在 :before 和 :after)
CSS:.hide_ct{ content:""; }

優點:簡單。
缺點:內容樣式分離、不實用。