简体   繁体   English

图内 img 和 figcaption 之间不需要的空间

[英]Unwanted space between img and figcaption inside figure

Why there's white space between an img and figcaption inside figure?为什么图中的 img 和 figcaption 之间有空白? margin and padding of both img and figcaption are 0. img 和 figcaption 的边距和填充都是 0。

 img { width: 200px; } figcaption { background-color: hsla(0, 0%, 0%, .5); }
 <figure> <img src="https://www.gravatar.com/avatar/61af86922698a8cd422f77ae2343d2a5?s=48&d=identicon&r=PG&f=1" /> <figcaption>Hello</figcaption> </figure>

(I'm not asking how to remove this space, I'm asking why it's there.) (我不是问如何删除这个空间,我是问为什么它在那里。)

As image is a inline element it gives extra space at the bottom you can fix it by giving vertical-align由于image是一个inline element它在底部提供了额外的空间,您可以通过提供vertical-align来修复它

 img { width: 200px; vertical-align: middle; } figcaption { background-color: hsla(0, 0%, 0%, .5); }
 <figure> <img src="https://www.gravatar.com/avatar/61af86922698a8cd422f77ae2343d2a5?s=48&d=identicon&r=PG&f=1" /> <figcaption>Hello</figcaption> </figure>

All you need is add a display: block in img tag.您只需要在img标签中添加一个display: 块

 img { width: 200px; background-color: hsla(0, 0%, 0%, .5); display: block; }
 <figure> <img src="https://www.google.com/images/srpr/logo11w.png" /> <figcaption>Hello</figcaption> </figure>

The space is there due to Line-Height of the font present in figcaption element.由于 figcaption 元素中存在字体的行高,因此存在空间。 Set the line-height of the figcaption the same as its font-size.将 figcaption 的 line-height 设置为与其字体大小相同。 Or lesser.或者更少。

something like就像是

figcaption {
    line-height:4px;
}

jsFiddle Link jsFiddle 链接

Link explaining how Line-Height works http://joshnh.com/2012/10/12/how-does-line-height-actually-work/链接解释 Line-Height 的工作原理http://joshnh.com/2012/10/12/how-does-line-height-actually-work/

Hope this helps.希望这可以帮助。

In HTML the images will effectively act like words.在 HTML 中,图像将有效地像单词一样。

You could use CSS.你可以使用 CSS。 Setting display:block, or float:left on the images will let you have define your own spacing and format the HTML however you want but will affect the layout in ways that might or might not be appropriate.在图像上设置 display:block 或 float:left 将使您可以定义自己的间距并根据需要设置 HTML 格式,但会以可能或可能不合适的方式影响布局。 Add this CSS:添加这个CSS:

img {
   width: 200px;
    display:block;
}

FIDDLE小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM