I think this issue is common and picked it up here in SO itself, but could not find how to solve this.
Problem:
When you resize the window, you will notice that the height of the two images will differ by 1px at times (that is expected when browser adjusts the dimesions).
How do I 'fix' this UI issue? I know I can do that by using a flexbox
. But I guess there is a better solution. Could you guys jump in?
table{
width:100%;
border-collapse: collapse;
}
img{
display: block;
width: 100%;
}
<table>
<tr>
<td><img src="http://placehold.it/100x100"/></td>
<td><img src="http://placehold.it/100x100"/></td>
</tr>
</table>
or even here when I use display: table
:
.wrapper{
width:100%;
display: table;
}
.wrapper div{
display: table-cell;
}
img{
display: block;
width: 100%;
}
<div class="wrapper">
<div><img src="http://placehold.it/100x100"/></div>
<div><img src="http://placehold.it/100x100"/></div>
</div>
Edit: The issue not there in Firefox browser but exists in Chrome.
Note that the issue is not there when I use a flexbox
:
body{
margin: 0;
}
.wrapper{
width:100%;
display: flex;
}
.wrapper div{
flex: 1;
}
img{
display: block;
width: 100%;
}
<div class="wrapper">
<div><img src="http://placehold.it/100x100"/></div>
<div><img src="http://placehold.it/100x100"/></div>
</div>
or using floats and inline-blocks:
body{
margin: 0;
}
.wrapper{
width:100%;
display: block;
}
.wrapper div{
display: inline-block;
float: left;
width:50%;
}
.wrapper:after{
content: '';
display: inline-block;
clear:both;
}
img{
display: block;
width: 100%;
}
<div class="wrapper">
<div><img src="http://placehold.it/100x100"/></div>
<div><img src="http://placehold.it/100x100"/></div>
</div>
try this responsive image grid code http://codepen.io/mlegg10/pen/AXZGox change my img src= to whatever your code will be
That's because of Sub-Pixel Problems.
Each image takes 50% of the container. For example, if the container is 100px wide, each image will be 50px wide.
But the width of container could be an odd number of pixels, e.g. 101px. Then there are three reasonable possibilities:
Each choice has its downsides, but nowadays browsers seem to prefer the first option. However, in this case, the images have an intrinsic aspect ratio, so different widths will produce different heights, and then the 1px gap is created horizontally instead of vertically.
It seems Firefox detects than, and thus makes the smaller image as tall as the other one, breaking the aspect ratio. Chrome prefers to enforce the aspect ratio.
There is no way to change this. It's completely implementation dependent:
Since chrome doesn't seem to play very well with .5px values.
Here is a javascript solution to make the width always an even number, thus the height will also be even:
https://jsfiddle.net/hhmsqtz6/1/
Just thought of a different approach to this which might meet your needs... instead of worrying about forcing the image sizes, you can vertically-align everything to the top, and then hide the bottom 1px of the wrapper div by adding a 1px high pseudo-element which is the same color as the background. This will solve the visual aspect of having images 1px off from each other. It will also hide the bottom 1px of the images even if they are properly aligned, but depending on your images, this may not be a big deal at all.
fiddle: https://jsfiddle.net/w4ktweuo/1/