Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my HTML5 <picture> have a height outside of its <img> and why does it not contain the <img>?

Tags:

html

css

HERE IS WHAT I DO NOT EXPECT

Strangely, the <picture> appears to have its own location and height below the image itself. Its CSS height is set to auto, so I'm unsure where this height of 18px is coming from.

HERE IS THE EXPECTED BEHAVIOR

The <picture> to contain the <img> within the <picture>. The <picture> to have the same height as what is contained within it in the HTML/DOM, e.g. the <img>.

HERE IS WHAT I'VE DONE TO TRY TO FIX THE PROBLEM

I've tried removing the PictureFill library we're using to ensure the image fills the container, but that's not what is affecting that height.

I've also tried inspecting the DOM to see where the height of <picture> is coming from. There is no padding or margin or height set on this element. The only content is its <source> and <img>, which are standard included elements for a <picture>.

Any help or insight is highly appreciated. Example code on JSBin!

HERE IS THE EXAMPLE CODE

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>

<body>
  <picture content="https://img-dev.evbuc.com/https%3A%2F%2Fs3.amazonaws.com%2Feventbrite-dev%2Fimages%2F10068705%2F149308521389%2F1%2Foriginal.jpg?w=1000&amp;rect=0%2C205%2C3264%2C1632&amp;s=6fa833f7049033ffa33ac3f11ec4433a">
    <source srcset="
					https://img-dev.evbuc.com/https%3A%2F%2Fs3.amazonaws.com%2Feventbrite-dev%2Fimages%2F10068705%2F149308521389%2F1%2Foriginal.jpg?w=480&amp;rect=0%2C205%2C3264%2C1632&amp;s=d3ce62073acf8faef26303df602d98ca 480w,
					https://img-dev.evbuc.com/https%3A%2F%2Fs3.amazonaws.com%2Feventbrite-dev%2Fimages%2F10068705%2F149308521389%2F1%2Foriginal.jpg?w=600&amp;rect=0%2C205%2C3264%2C1632&amp;s=299ee1965b7991d853f1c74ece47a4fc 600w,
					https://img-dev.evbuc.com/https%3A%2F%2Fs3.amazonaws.com%2Feventbrite-dev%2Fimages%2F10068705%2F149308521389%2F1%2Foriginal.jpg?w=800&amp;rect=0%2C205%2C3264%2C1632&amp;s=7b00f35515de1a75308074e0b8531606 800w,
					https://img-dev.evbuc.com/https%3A%2F%2Fs3.amazonaws.com%2Feventbrite-dev%2Fimages%2F10068705%2F149308521389%2F1%2Foriginal.jpg?w=1000&amp;rect=0%2C205%2C3264%2C1632&amp;s=6fa833f7049033ffa33ac3f11ec4433a 1000w,
					https://img-dev.evbuc.com/https%3A%2F%2Fs3.amazonaws.com%2Feventbrite-dev%2Fimages%2F10068705%2F149308521389%2F1%2Foriginal.jpg?w=2000&amp;rect=0%2C205%2C3264%2C1632&amp;s=03b760c29e398261cef276c2bdb0fcb2 2000w
				" sizes="100vw">

      <img class="listing-hero-image js-picturefill-img" data-automation="listing-hero-image" alt="The Cat is in the (Hockey) Bag">
  </picture>
</body>

</html>

HERE IS A SCREENSHOT OF WHAT I AM SEEING

enter image description here

like image 202
jennz0r Avatar asked Jul 13 '16 21:07

jennz0r


People also ask

How do you change the height of an image in HTML?

If your image doesn't fit the layout, you can resize it in the HTML. One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.

Why should you include height and width attributes on an IMG tag?

Adding the height and width attributes to your IMG SRC HTML tag allows the browser to know how much space to leave for an image. Without these values, the browser doesn't initially create a space for the image, which means elements surrounding the image are adjusted after it has loaded.

Which attribute would you use to set the height of an image?

The height attribute specifies the height of an image, in pixels.


2 Answers

The picture element is by default a non-replaced inline element. CSS 2.2 says of such elements:

The height of the content area should be based on the font, but this specification does not specify how.

So the picture element's height is that of one line of text, and nothing to do with the img element it contains.

To fix, just apply this css: picture { display:block; }

like image 116
Alohci Avatar answered Oct 28 '22 16:10

Alohci


The problem seems to be the "picture" element, the problem is due to the alignment of its element (the image).

You just only have to edit you img css instruction:

img {display:block}
like image 35
Dulup Avatar answered Oct 28 '22 16:10

Dulup