图片和图片间有间隙-基线与幽灵空白


1. 左右图片有间隔

<body style="background-color:blue">

<img width="200px" height="200px" src="https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/c995d143ad4bd113fceedf775bafa40f4bfb0557.jpg" alt="" srcset="">

<img width="200px" height="200px" src="https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/c995d143ad4bd113fceedf775bafa40f4bfb0557.jpg" alt="" srcset="">

</body>

原因:这是上面代码inline-block元素和空格结合的结果,父级元素把空格当做一个字符处理。

解决:要把空格去掉,如下:

<body style="background-color:blue">

<img width="200px" height="200px" src="https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/c995d143ad4bd113fceedf775bafa40f4bfb0557.jpg" alt="" srcset=""><img width="200px" height="200px" src="https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/c995d143ad4bd113fceedf775bafa40f4bfb0557.jpg" alt="" srcset="">

</body>

2. 上下图片有空格

<body style="background-color:blue">
    <div>
        <img width="200px" height="200px"
            src="https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/c995d143ad4bd113fceedf775bafa40f4bfb0557.jpg"
            alt="" srcset="">
    </div>

    <img width="200px" height="200px"
        src="https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/c995d143ad4bd113fceedf775bafa40f4bfb0557.jpg"
        alt="" srcset="">
    </div>
</body>

原因:图片默认的基线对齐会在底部留有一定空隙(我们称其为幽灵空白)。

解决:

  1. div设置 font-size:0;

  2. img设置 display:block;

  3. div设置 display:flex;

  4. img设置 vertical-align: middle/top/bottom;


文章作者: iamfugui
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 iamfugui !
评论
  目录