16

I have two images that I gave borders to (1px solid black), but in my browser they are rendered with a weird white-space - kind of like an involuntary padding.

The white space is on one side only, but depending on the zoom it can change the side of the border (e.g. suddenly the "pseudo-padding" is no longer on the right, but on the top), or even on two sides at the same time.

.img img {
  width: 100%;
  vertical-align: middle;
  display: block;
  border: 1px solid black;
}
<section class="imgrid">
  <div class="img">
    <img src="http://via.placeholder.com/500x500" />
  </div>
  <div class="img">
    <img src="http://via.placeholder.com/500x500" />
  </div>
</section>

That's about it in terms of relevant code. The images are 500px squares (so 500x500).

Edit: This has nothing to do with the suggested 'similar question' here. I have already tried vertical align before I created this question. I have added width: 100% and vertical-align: middle to the snippet to illustrate this.

The problem looks like so:

on the right enter image description here

at the bottom enter image description here

caramba
  • 21,963
  • 19
  • 86
  • 127
Joe
  • 221
  • 1
  • 3
  • 7
  • 1
    Possible duplicate of [Image inside div has extra space below the image](https://stackoverflow.com/questions/5804256/image-inside-div-has-extra-space-below-the-image) – caramba Aug 03 '17 at 11:31
  • 1
    No, not it. Thanks, though. – Joe Aug 03 '17 at 11:41
  • 4
    @caramba Please remove the "possible duplicate" OR prove to me how the answer to my question is provided in your suggestion. – Joe Aug 03 '17 at 11:55
  • I don't see any white space...post a image and also which browser you are using? – Ritesh Nair Aug 03 '17 at 12:01
  • Try using a CSS reset stylesheet. You can also try using `* { margin: 0; padding: 0; box-sizing: border-box; }` at the top of your CSS as a quick test. Browsers may add extra styling to your page. E.g. Chrome adds a 8px margin to the body tag. – Gorka Hernandez Aug 03 '17 at 12:01
  • Hello, thanks for your suggestions! I am using Chrome Version 59.0.3071.115, and I have already reset the padding and margin, also using box-sizing: border-box! I am out of answers for this absurd phenomenon :/ – Joe Aug 03 '17 at 12:04
  • I didn't downvote but thought it could be a duplicate. Those links are just here to help. They don't have anything to do with the downvotes. – caramba Aug 03 '17 at 12:12
  • Can you please also show a image of the "involuntary padding". I'm also using chrome and don't see any. – caramba Aug 03 '17 at 12:14
  • Well then I am sorry. I am just frustrated with people downvoting for no apparent reason - it's not helping anyone. I will provide you with an image, sure: http://i.imgur.com/0hH5R3B.png - on the right. http://i.imgur.com/eYt8aUK.png - at the bottom. Thanks! – Joe Aug 03 '17 at 12:19
  • Same problem if you look here: https://jsfiddle.net/0t4vLky7/ ? – caramba Aug 03 '17 at 12:21
  • 1
    Yes. What the hell is wrong with my browser if I am the only one getting that problem O.o – Joe Aug 03 '17 at 12:22
  • Does it occur with no zoom? Like if no zoom. On OSX press CMD+SHIFT+0 – caramba Aug 03 '17 at 12:24
  • Not sure what no zoom is (other than the obvious not having zoomed in). I am not using zoom and it still creates this horrible space that's there out of nowhere... – Joe Aug 03 '17 at 12:28
  • So if I zoom in after a few steps this can happen. Not sure if it's a browser bug or a monitor/graphic card bug. No zoom === not zoomed in && not zoomed out . So if no zoom I don't see it. Most people zooming have issues with the eyes so they want see it :) – caramba Aug 03 '17 at 12:39
  • Here's a screenshot with the chrome inspector/devtools (whatever it's called) http://i.imgur.com/t7rWGR2.png - I think that kind of shows what the problem is, but I still do not understand it – Joe Aug 03 '17 at 12:41
  • In my case it was only visible on QHD monitor. – Brackets Jan 16 '21 at 11:18
  • I hope this helps and and align with what you are experiencing [https://stackoverflow.com/questions/5804256/image-inside-div-has-extra-space-below-the-image](https://stackoverflow.com/questions/5804256/image-inside-div-has-extra-space-below-the-image) – Shironekomaru Aug 25 '21 at 00:50
  • I've used 0.5px instead of 1px for border width. – Laxmikant Bhumkar May 17 '22 at 14:52

10 Answers10

10

Pretty sure if you're seeing this you have a 4k screen set to something other than 100% scaling.

I have two 4k screens, one set to 100% (my laptop screen) and another set to 125% (because it is physically bigger so needs scaling).

Here's a couple images with borders on my 4k laptop screen. The scale factor is 100% so everything is clean:

enter image description here

And then moved over to my external screen. This has DPI setting of 125% (in Windows). In the browser it's 100% scaling, but it's the DPI setting that causes this effect:

enter image description here

With a circle the effect is worse and you end up with a shape that isn't even completely round.

enter image description here

Clearly with 125% scaling a one pixel line will be scaled to 1.25 pixels, which to requires either sub pixel rendering (anti-aliasing) or just snapping to the closest pixel. It seems they don't consider image + 1 pixel border as a single entity and rounding errors are what ultimately gives this behavior.

The fix of setting the background color to the same as the border color sort of works, but you end up with this:

enter image description here

I think the important thing to realize is that it won't affect most people.

Another possible solution is to add an absolute div on top the same width and height and set the border on that. Make sure box-sizing: border-box is set. That will ensure the border is exactly on top of the image. However I don't recommend this in general because it will quickly become unmanageable.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
6

I have the same problem (using Chrome) and came across this thread.

None of these answers helped me, but I found that adding a background-color that matches the border color to the img element resolved the problem for me:

img {
    border: 5px solid #000;
    background-color: #000;
}
Ferngummy
  • 61
  • 1
  • 3
5

I had the same issue on Chrome, I had to make sure the browser was on 100% zoom and that fixed it for me. Chrome likes to round px measurements without your consent, and that can lead to weird styles when you zoom in or out.

JW Geertsma
  • 857
  • 3
  • 13
  • 19
Kyle Peck
  • 51
  • 1
  • 2
4

none of them worked for me in Chrome under Windows, but setting margin: -1px; on the img did the trick

Paul Pacurar
  • 145
  • 12
2

I experienced the same problem developing a website when noticed some of the images had a tiny gap underneath.

The box on the left has the fix applied, the right one not: Strange Gap Phenomenom

I found a "fix" only for Chrome:

Adding vertical-align: -webkit-baseline-middle; to the img element seemed to remove it.

Odilio
  • 33
  • 7
2

Try this :

<style>
    * {
        margin: 0;
    }
</style>

This should solve your problem.

Ibrar Khan
  • 429
  • 3
  • 7
1

If I'am not wrong adding width: 100%; to image will solve the issue.

Saravanan I
  • 1,229
  • 6
  • 9
1

Margin and padding should remove the white border that you see I hope this helps!

img {
  padding:0px;
  margin:0px ;
  width: 100%;
  height:auto;
  vertical-align: middle;
  display: block;
  border: 1px solid black;
}
twinaholic
  • 115
  • 1
  • 11
  • Still looks messed up in my browser. Which is weird, since I am using a not-so-old version of Chrome... Thanks, anyways! – Joe Aug 03 '17 at 12:20
  • can you take a screen shot of your screen for us to see.. and try using Google chrome developer tools ("Ctrl+Shift+I")to see where that white padding is coming from. @Joe – twinaholic Aug 03 '17 at 12:23
  • http://i.imgur.com/vvCxFlA.jpg - no padding at all, no margins either. I made sure of that – Joe Aug 03 '17 at 12:26
  • I noticed different css then what you posted here on the question, would it possible if you can post you css for us to really understand and see your css code, Thanks. – twinaholic Aug 03 '17 at 12:31
  • I have the same problem with the CSS I posted here. And I don't see why it's happening, there's nothing telling the browser to create padding or anything – Joe Aug 03 '17 at 12:35
  • use my css code aand also you have (.img img {}) , remove the .img @Joe – twinaholic Aug 03 '17 at 12:37
  • .img img is intentional. .img is the class I used for the divs in which I have the images (img). – Joe Aug 03 '17 at 12:38
  • I understand but as an experiment, lets try it out, .img is the class but img refers to the actual img @Joe – twinaholic Aug 03 '17 at 12:42
  • If I remove the specifications on the .img then the whole thing becomes different. – Joe Aug 03 '17 at 12:50
  • well the next step i would say is for you to post your full project css and html like that i can see if maybe something else i causing the white border and or if its just your browser, and i know your frustrated this kind of stuff has happened to me in the past, it just takes time well figure it out! – twinaholic Aug 03 '17 at 13:00
  • please approve or vote my answer if i was able to help, if not please let me know what problem is still persisting. – twinaholic Aug 18 '17 at 16:40
0

I hope this help and align with what you are experiencing right now: Image inside div has extra space below the image

Explanation: why is there a gap under the image? The gap or extra space under the image isn't a bug or issue, it is the default behaviour. The root cause is that images are replaced elements (see MDN replaced elements). This allows them to "act like image" and have their own intrinsic dimensions, aspect ratio.... Browsers compute their display property to inline but they give them a special behaviour which makes them closer to inline-block elements (as you can vertical align them, give them a height, top/bottom margin and padding, transforms ...).

This also means that:

<img> has no baseline, so when images are used in an inline formatting context with vertical-align: baseline, the bottom of the image will be placed on the text baseline. (source: MDN)

As browsers by default compute the vertical-align property to baseline, this is the default behaviour. The following image shows where the baseline is located on text:

Source Image

According to this thread there are different ways to fix this gap:

  • Set the vertical-align property of the image to vertical-align: bottom; vertical-align: top; or vertical-align: middle;
  • Set the display property of the image to display:block; -- This is the most effective way so far.
  • Setting line-height:0; or font-size:0;
Shironekomaru
  • 361
  • 1
  • 6
  • 15
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/29656602) – Lin Du Aug 25 '21 at 01:31
  • @slideshowp2 done putting some references according to the same thread mentioned. – Shironekomaru Aug 25 '21 at 02:16
0

You can try fixing it with a shadow instead. This removed the white space for me on both my 4k montior and my FullHD laptop.

-moz-box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.45);
-webkit-box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.45);
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.45);
Entalist
  • 93
  • 9