Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为什么图片下面总是多几个像素空白? #193

Open
TieMuZhen opened this issue May 7, 2022 · 0 comments
Open

为什么图片下面总是多几个像素空白? #193

TieMuZhen opened this issue May 7, 2022 · 0 comments
Labels

Comments

@TieMuZhen
Copy link
Owner

TieMuZhen commented May 7, 2022

问题效果如下:

<div class="father">
    <img src='https://www.baidu.com/img/bdlogo.gif'>
</div>

image

因为图片默认是行内元素,行内元素默认是baseline对齐的,和底部会有一段距离。
image
如上图,绿色线标出了toptext-topmiddlebaselinetext-bottombottom相对应的位置。

当div的高度和文字的高度是一样的时候,设置vertical-align属性为toptext-toptext-bottombottom,效果是一样的。

vertical-align属性为toptext-top时候,图片顶部和线对齐

vertical-align属性为middle时候,图片中间和线对齐

vertical-align属性为baselinetext-bottombottom时候,图片底部和线对齐

知道了img表现同文字一样, 我们就能有很多解决办法

一、img就不是inline元素了, 不用像文字那样需要留底

img {
  display: block;
}

二、把img的父元素设为font-size: 0, 字体不占位了, 因此也不会留底, 同理还有line-height: 0;

.box {
  font-size: 0;
}

三、修改verticle-align: baseline;

这是最佳的解决方法, 因为这问题就是由verticle-align: baseline;引起的

img {
  vertical-align: top / middle / bottom;
}
@TieMuZhen TieMuZhen added the CSS label May 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant