2

This is a very simple example. It displays a gap between all 4 images. Doesn't matter if they are in the same div or not.

<div>
  <img src="http://lorempixel.com/400/200">
  <img src="http://lorempixel.com/400/200">
</div>
<div>
  <img src="http://lorempixel.com/400/200">
  <img src="http://lorempixel.com/400/200">
</div>

div {
  margin: 0;
  padding: 0;
}
img {
  margin: 0;
  padding: 0;
}
<div>
  <img src="http://lorempixel.com/400/200">
  <img src="http://lorempixel.com/400/200">
</div>
<div>
  <img src="http://lorempixel.com/400/200">
  <img src="http://lorempixel.com/400/200">
</div>

Codepen

enter image description here

Where are these gaps coming from and how do I get rid of them?

I have tested in Chrome, Opera, Firefox and Safari.

VXp
  • 11,598
  • 6
  • 31
  • 46
bskool
  • 2,068
  • 2
  • 17
  • 24

4 Answers4

4

The reason is the inline-elements (<img>). You can do multiple things. One would be to set font-size on the image container to 0.

div.img {
  clear:both;
  font-size:0;
}
div.img img {
  display:inline;
  float:left;
}
<div class="img">
  <img src="https://lorempixel.com/400/200">
  <img src="https://lorempixel.com/400/200">
</div>
<div class="img">
  <img src="https://lorempixel.com/400/200">
  <img src="https://lorempixel.com/400/200">
</div>

Another solution using flexbox (more recommended):

div.img {
  display:flex;
  flex-direction:row;
  align-items:start;
}
<div class="img">
  <img src="https://lorempixel.com/400/200">
  <img src="https://lorempixel.com/400/200">
</div>
<div class="img">
  <img src="https://lorempixel.com/400/200">
  <img src="https://lorempixel.com/400/200">
</div>

And one more solution with CSS grid:

div.img {
  display:grid;
  grid-template-columns:auto auto;
}
<div class="img">
  <img src="https://lorempixel.com/400/200">
  <img src="https://lorempixel.com/400/200">
</div>
<div class="img">
  <img src="https://lorempixel.com/400/200">
  <img src="https://lorempixel.com/400/200">
</div>
Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
2

Simply use display: flex for this to the parent div like below

div {
  display: flex;
}
<div>
  <img src="http://lorempixel.com/400/200">
  <img src="http://lorempixel.com/400/200">
</div>
<div>
  <img src="http://lorempixel.com/400/200">
  <img src="http://lorempixel.com/400/200">
</div>
VXp
  • 11,598
  • 6
  • 31
  • 46
Bhuwan
  • 16,525
  • 5
  • 34
  • 57
1

Updated

Just put float: left to the <img> and the problem is solved

The img is a block element that's why the gap between them if use float to the img then it will solve your issue.

To break the images as per the image just add clear: both; to the

div {
  margin: 0;
  padding: 0;
  clear: both;
}
img {
  margin: 0;
  padding: 0;
  float:left;
}
<div>
  <img src="http://lorempixel.com/400/200">
  <img src="http://lorempixel.com/400/200">
</div>
<div>
  <img src="http://lorempixel.com/400/200">
  <img src="http://lorempixel.com/400/200">
</div>

Codepen

Hope this is helpful.

Jithin Raj P R
  • 6,667
  • 8
  • 38
  • 69
0

make use of "style=float:left" and style="line-height:0px;" will resolve issue . try below it resolve issue of bottom an side space too

<div style="line-height:0px;">
  <img src="http://lorempixel.com/400/200" style="float:left;">
  <img src="http://lorempixel.com/400/200">
</div>
<div >
  <img src="http://lorempixel.com/400/200" style="float:left;">
  <img src="http://lorempixel.com/400/200">
</div>
VXp
  • 11,598
  • 6
  • 31
  • 46
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263