刘益民中文网

营业时间:8:00 - 22:00

使DIV图片之间不产生空白距离

九月 21, 2007, Posted by cike at 2:04 下午

中国杨树网设计杨树图片时遇到一个问题,正好在ajaxbbs.net上看到作者提到,抄过来收藏,这个问题是这样的,无论怎么设置边框,在图片的下方都会有一空隙,我的是div包一个IMG

在无忧上看到一个网友问这个问题,偶也比较感兴趣,楼主代码是这样写的:

<div style=”width:240px;height:315px;line-height:100%;”>
<img src=”1.gif” border=”0″ height=”56″ width=”240″ />
<img src=”1.gif” border=”0″ height=”56″ width=”240″ />
<img src=”1.gif” border=”0″ height=”56″ width=”240″ />
<img src=”1.gif” border=”0″ height=”56″ width=”240″ />
</div>

这样看到的效果就是每个图片之间有小段空白。

有朋友贴出解决代码如下:

<div style=”width:240px;height:315px;line-height:100%;”>
<img src=”1.gif” border=”0″ height=”56″ width=”240″ /><img src=”1.gif” border=”0″ height=”56″ width=”240″ /><img src=”1.gif” border=”0″ height=”56″ width=”240″ /><img src=”1.gif” border=”0″ height=”56″ width=”240″ /><img src=”1.gif” border=”0″ height=”56″ width=”240″ /></div>

即删除每个<img>标记之间的换行符,将所有标记连接在一起,这样问题解决了,但降低了代码的可读性,显然不是一种完美的解决办法。

最后hbjswj大哥给出了一种比较好的办法.全部代码如下:

<style>
img {float:left;} /*注意这个地方*/
</style>
<div style=”width:240px; height:315px; line-height:100%;”>
<img src=”1.gif” border=”0″ height=”56″ width=”240″ />
<img src=”1.gif” border=”0″ height=”56″ width=”240″ />
<img src=”1.gif” border=”0″ height=”56″ width=”240″ />
<img src=”1.gif” border=”0″ height=”56″ width=”240″ />
<img src=”1.gif” border=”0″ height=”56″ width=”240″ />
</div>

为每个img增加了float:left,这就比较完美的解决了这个问题

20080831更新的:以上是07年9月实践中查阅资料总结,针对本例现在再补两种做法

img{vertical-align:bottom}

img{display:block}

Currently have 1 Comment

  1. shylc 说:

    非常感谢,问题解决了,豁然开朗!

    cike 于 2008-8-31 16:45:53 回复

    这是07年写的文章,现在有新玩法了,我更新一下文章

Leave a Reply








Scroll to the top