Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why <img> takes a height when it's an inline element? [duplicate]

Tags:

html

css

My browser shows that the <img> tag is an inline tag. Numerous answers in Stack says that span does not accept an height property because it's an inline tag. How can <img> do that ? Why isn't it rather an inline-block element ?

like image 709
Nicolas Zozol Avatar asked Nov 12 '14 14:11

Nicolas Zozol


People also ask

Do inline elements have height?

Inline element properties: The height of an inline element is the height of the content. The width of an inline element is the width of the content. The height and width of an inline element cannot be set in CSS. You cannot set the height and width of block-level elements in CSS.

Why is IMG an inline element?

behaves as an inline-block element as it allows other images in same line i.e. inline and also we can change the width and height of the image and this is the property of a block element. Hence, provide both the features of inline and block elements.

Why wouldn't you include width and height attributes in the IMG tag?

The historical reason to define height/width in tags is so that browsers can size the actual <img> elements in the page even before the CSS and/or image resources are loaded. If you do not supply height and width explicitly the <img> element will be rendered at 0x0 until the browser can size it based on the file.

Are images inline elements?

Paragraphs are block-level elements, which means that they block off a whole line for themselves, and images are inline elements, which means they will automatically be placed next to one another on the same line.


1 Answers

<img> tag is not strictly an inline element, but a inline-replaced element.

In a nutshell, it means that <img> (and other elements, as <video> or, <object> if you still use it), has intrinsic dimensions. So CSS can handle those dimensions (and other properties, such as margins). Because <img> is an inline tag, that is replaced by its own source file (well, it's still an inline element).

Some doc about that :

  • https://developer.mozilla.org/en-US/docs/Web/CSS/Replaced_element
  • http://www.w3.org/TR/REC-html40/struct/objects.html#edef-IMG

Funny fact (I guess) : you can't override (or simply handle) inline-replaced behavior to "normal" inline behavior on those elements with CSS (when it works when you set it to inline-block or block or whatever you want). See this example : http://jsfiddle.net/s8apbbof/

like image 191
enguerranws Avatar answered Sep 22 '22 16:09

enguerranws