0

I've got 2 section tags on top of each other. The first contains an image the second just some text.

But for some reason the first is putting a few pixels extra padding at the bottom. Why????

<section class="s1"><img src="something.jpg" width="500" height="500" /></section>
<section class="s2"> Test Test Test </section>

I tried this in the styling

section, img {
    padding: 0;
    margin: 0
}
.s1 {
    background-color: black
}
.s2 {
    background-color: red
}

https://jsfiddle.net/ctscby6z/

Ignore the fact it's a broken image, does the same with a real image too.

Thanks.

Strontium_99
  • 1,771
  • 6
  • 31
  • 52

4 Answers4

1

At a glance this happens when display types are different.

display: block; fixes this.

Tek
  • 2,888
  • 5
  • 45
  • 73
1

The img is inline element. So make display: block for it.

Alex M
  • 2,756
  • 7
  • 29
  • 35
  • Our replies are a couple of seconds apart! Has it always been this way? I thought `img` was block by default all this time... – Tek Jul 12 '16 at 14:28
  • OMG.... How thick am I :) Thanks for that. – Strontium_99 Jul 12 '16 at 14:28
  • @Tek Some browsers render `img` as `inline-block` element, some as `inline`. – Alex M Jul 12 '16 at 14:33
  • @AlexM I haven't found any browser which renders images as `inline-block`s by default. Anyways, `inline-block` replaced elements should behave basically like `inline` replaced elements, so it doesn't matter much. – Oriol Jul 12 '16 at 15:12
1

You should add display: block; to image: https://jsfiddle.net/Mindaugas/jk4yyw48/1/

Mindaugas M.
  • 121
  • 2
  • 6
1

<img> tags by default are displayed as inline block elements, however in an instance like this, you would want to use display:block; instead :

img { display: block }

Example

enter image description here

img {
  display: block;
}
section,
img {
  padding: 0;
  margin: 0
}
.s1 {
  background-color: black
}
.s2 {
  background-color: red
}
<section class="s1">
  <img src="something.jpg" width="500" height="500" />
</section>
<section class="s2">Test Test Test</section>
Community
  • 1
  • 1
Rion Williams
  • 74,820
  • 37
  • 200
  • 327