简体   繁体   中英

Space under <img> tag

I have this annoying space under my picture for no reason. I'm using www.getskeleton.com as the framework.

HTML code

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

CSS code

.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. This'll solve it for HTML pages when using the HTML 5 doctype. Oddly, when using eg, HTML 4.01 doctype, images will not exhibit this errant space below behaviour.

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; the default alignment for images is 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. 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.

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; }

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.

Try this

.box{
    display:flex
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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