table设置背景图片,不能100%显示

在开发中发现了下面的情况:

(1) 如果文件存放为.jsp文件后缀的形式,代码如下所示(index.jsp):

<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>让表格百分之一百显示</title>
</head>

<body style="background:#9C9;">
 <table cellpadding="0" cellspacing="0" style="width:100%;" >
 <tr>
 <td height="8" width="17px;" ><img src="img/bg-header_l.gif" width="17" height="8" /></td>
 <td style="background:url(img/bg-header_c.gif) repeat-x;"></td>
 <td height="8" width="17px;" ><img src="img/bg-header_r.gif" width="17" height="8" /></td>
 </tr>
 </table>
</body>
</html>

 

程序运行后,页面中显示的效果如下图所示:

该效果出现了两处不对的地方:

(1) 中间的td的背景并没有铺满整个td;

(2) 整个table宽度并没有撑满整个屏幕。

百思不得其解,非常郁闷!!后台尝试给table加上border="0",效果仍然如上图所示。把0改为1,发现table撑满了整个屏幕,并且td也被背景铺满,如下图,不过表格的边框不是我所要的。

 

发现上述方法不能从根本上解决问题。

然后又对照了一下table中的3个td,发现第二个td中没有内容,于是尝试在第二个td中填入内容,代码如下所示:

<table cellpadding="0" cellspacing="0" style="width:100%;" >
 <tr>
 <td height="8" width="17px;" ><img src="img/bg-header_l.gif" width="17" height="8" /></td>
 <td style="background:url(img/bg-header_c.gif) repeat-x;">第二个td添加内容</td>
 <td height="8" width="17px;" ><img src="img/bg-header_r.gif" width="17" height="8" /></td>
 </tr>
 </table>

 

运行代码后的效果基本符合最终的结果,运行效果如下:

 

下面再将代码稍微调整一下即可,完整代码如下:

 1 <%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8" language="java" %>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 3 <html xmlns="http://www.w3.org/1999/xhtml">
 4 <head>
 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 6 <title>让表格百分之一百显示</title>
 7 </head>
 8 
 9 <body style="background:#9C9;">
10  <table cellpadding="0" cellspacing="0" style="width:100%;" >
11  <tr>
12  <td height="8" width="17px;" ><img src="img/bg-header_l.gif" width="17" height="8" /></td>
13  <td style="background:url(img/bg-header_c.gif) repeat-x; font-size:0px;">&nbsp;</td>
14  <td height="8" width="17px;" ><img src="img/bg-header_r.gif" width="17" height="8" /></td>
15  </tr>
16  </table>
17 </body>
18 </html>

 

最终效果如下图所示:

 

ps: 以上问题,出现自360安全浏览器、QQ浏览器。 firefox、ie8 、chrome、opera中没有出现这样情况。另外,将上面的代码存储为html后缀的形式用最初的代码也没有问题,非常奇怪。

代码下载:程序源代码

转载于:https://www.cnblogs.com/hailu/archive/2012/11/05/tablebackground.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值