简体   繁体   English

下空间<img>标签

[英]Space under <img> tag

I have this annoying space under my picture for no reason.我的照片下面无缘无故有这个烦人的空间。 I'm using www.getskeleton.com as the framework.我使用 www.getskeleton.com 作为框架。

HTML code HTML代码

<div class="four columns bottom">
  <div class="box">
    <img src="images/picture.png" title="" alt="">
  </div>
</div>

CSS code CSS代码

.box{
  border: 1px solid #bfbfbf; /* ---- Border OUTSIDE*/
}

这里有图片下方的空间

Although I'm sure this has since been resolved, I believe none of these answers are correct (or at least, the link from the "accepted" answer is dead). 虽然我确信这已经解决了,但我相信这些答案都不正确(或者至少,“接受”答案的链接已经死了)。

The way to deal with this spacing issue (and why it isn't set in util libraries like normalize I'm not sure) is vertical alignment of the image. 处理这个间距问题的方法(以及为什么它没有在util库中设置,例如normalize我不确定)是图像的垂直对齐。 This'll solve it for HTML pages when using the HTML 5 doctype. 当使用HTML 5 doctype时,这将解决HTML页面问题。 Oddly, when using eg, HTML 4.01 doctype, images will not exhibit this errant space below behaviour. 奇怪的是,当使用例如HTML 4.01 doctype时,图像不会在行为下面显示这个错误的空间。

The fix: 修复:

img {
  vertical-align: top;
}

I hope that helps someone who may have run into this problem. 我希望能帮助那些可能遇到这个问题的人。

Edit: Some extra info I noticed after writing this and subsequently researching why normalize doesn't set alignment on the img tag; 编辑:写完这篇文章之后我注意到的一些额外信息,然后研究为什么normalize没有在img标签上设置对齐; the default alignment for images is vertical-align: baseline; 图像的默认对齐方式是vertical-align: baseline; - that's the behaviour which introduces the space underneath. - 这是引入下面空间的行为。 Normalize's author believes this behaviour is consistent cross-browser, and so has decided not to 'normalize' this. Normalize的作者认为这种行为是跨浏览器的一致性,因此决定不对此进行“规范化”。 I suppose that makes sense if you wanted text sitting next to an image to align properly with any subsequent lines of text. 我想如果你希望文本坐在图像旁边与任何后续文本行正确对齐,那就更有意义了。 Some people also prefer to use vertical-align: middle as opposed to top to address this issue - so you can vary this as you wish. 有些人还喜欢使用vertical-align: middle而不是top来解决这个问题 - 所以你可以随意改变它。

However, regarding baseline alignment, in the case where I had an image that was so big that it was higher than the line-height, I'd probably be floating it or some other behaviour anyway... but there you go. 然而,关于基线对齐,如果我的图像太大而且高于线高,我可能会浮动它或其他一些行为......但是你去了。

I've used the vertical-align stuff for a while now without any incident. 我已经使用了垂直对齐的东西一段时间没有任何事故。 But as always, do ensure you test for any repercussions for images no longer being aligned to the baseline. 但与往常一样,请确保测试不再与基线对齐的图像的任何影响。

Try this: 试试这个:

.box img {
    display: block;
    padding: 0px;
    margin: 0px;
}

Try this: .box { font-size: 0; 试试这个:.box {font-size:0; } }

Your image need to be floated. 您的图像需要浮动。 Try this: 试试这个:

    #yourimage{
        float: left;}

As mentioned, more information would help a lot but i have no doubt that it is padding that is causing the border to go out of the image, reason put very simply being 如上所述,更多的信息会有很大的帮助,但我毫不怀疑它是填充,导致边框走出图像,原因很简单就是

margin pushes outside the element
padding pushes inside the element

as it were. 原样。

Fix then: 然后修复:

.box {
    padding-bottom: 0px;
}
//to be sure that the image doesn't have any padding, even though OP said the .box img fix didn't help
.box img {
    margin-bottom: 0px;
}

It's an age old quirk - the whitespace from your line formatting is causing the gap. 这是一个古老的怪癖 - 你的行格式化的空白导致了这个空白。 Add <br /> after the image. 在图像之后添加<br />

Try this尝试这个

.box{
    display:flex
}

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

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