CSS文字隐藏的几种方法

Posted in Web技术 at June 21st, 2010 /

因为自己经验尚浅,demo部分难免会有错漏情况,如发现问题,望大家能指出。

方法列表 demo

1. 毫无保留:display:none

代码片段:

(x)HTML

<p class=”hide_display”>我是打酱油的文本</p>

CSS

/* 暴力隐藏 */
.hide_display{display:none;}

兼容性:

优点:

方便、快捷

兼容性好

缺点:

屏幕阅读器无法阅读

超链接不适用

图片替代文本需要其他标签的背景

影响搜索排名

大量使用容易被认为是SEO作弊

2. 折中选择:overflow:hidden/position:absolute/visibility:hidden

代码片段:

(x)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_position一样*/
.hide_visibility{
visibility:hidden;
position:absolute; /* 脱离文档流 */
margin-left:-32767px;
}

兼容性:

优点:

方便、快捷

不希望屏幕阅读器读取的内容可以使用visibility

缺点:

超链接不适用

图片替代文本需要其他标签的背景

3. 体验损失:text-indent负值

代码片段:

(x)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;
}

兼容性:

优点:

适用超链接的图片替代

缺点:

FF的虚边问题

text-indent容易产生bug

4. 牛皮癣:font-size设零

代码片段:

(x)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,ff不显示文本

适用环境少

5. 强强联合:line-height三倍

代码片段:

(x)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支持 */
}

兼容性:

优点:

兼容性好

超链接和图片替代文本可用

缺点:

使用限制较大,需要定宽高

多一丁点:前置背景遮挡

代码片段:

(x)HTML

<!– 在css无效和css有效图片无效都适用 –>
<a class=”hide_bg” href=”#”><span
class=”front_bg”></span>我是可访问性的化身</a>

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和链接需要用 */
}

兼容性:

优点:

兼容性好

超链接和图片替代文本可用

可访问性强

缺点:

使用限制较大,需要定宽高

代码冗余,需要空标签

另辟蹊径:content:”“

代码片段:

(x)HTML

<!–只有opera支持,按定义只能用在:before 和:after–>
<a class=”hide_ct” href=”#”>也许我才是最合适的,谁知道呢,内容表现分离。只有opera支持</a>

CSS

.hide_ct{ content:&quot;&quot;; }

兼容性:

优点:

简单

缺点:

内容样式分离

不实用

原文:http://caib.me/hide-text/


相关文章:

  1. CSS的浏览器兼容性技巧
  2. 几种常用CSS书写技巧
  3. 探讨CSS样式的优先级问题
  4. 我的DIV+CSS排版经验
  5. 营销型企业网站的特点