Is <img> element block level or inline level?
Asked Answered
C

10

200

I've read somewhere that <img> element behaves like both. If correct, could someone please explain with examples?

Consultative answered 8/3, 2010 at 16:19 Comment(0)
A
225

It's true, they are both - or more precisely, they are "inline block" elements. This means that they flow inline like text, but also have a width and height like block elements.

In CSS, you can set an element to display: inline-block to make it replicate the behaviour of images*.

Images and objects are also known as "replaced" elements, since they do not have content per se, the element is essentially replaced by binary data.

* Note that browsers technically use display: inline (as seen in the developer tools) but they are giving special treatment to images. They still follow all traits of inline-block.

Aceydeucy answered 8/3, 2010 at 16:21 Comment(9)
I always read that images are inline elements, not inline-block, but it does make sense that they would be inline-block, due to the ability to add width and height.Accouter
This answer is not technically correct. Precisely speaking, img elements are not inline-block but actually inline elements. You can check this in a modern browser by right clicking an image, clicking "Inspect element", then viewing the computed style, which will show display: inline. There is no block context happening inside the tag, so it's not correct to call it inline-block. For more information on replaced inline elements see Quentin's answer and this MDN article.Trackless
@Max that link says nothing about replaced elements being inline.Aceydeucy
@Aceydeucy The link I posted doesn't say that img elements are inline - Google Chrome dev tools shows img elements as being inline. This post is the only place I've found so far that says that they are inline-block instead. Interestingly, I haven't found any authority that says they are inline either. Is how to treat the tag implementation-dependent, maybe?Trackless
@Max According to this, replaced elements are outside the scope of the CSS formatting model. Nothing in the HTML or CSS specs specify that images are inline. So regardless of what the browser says it is, images are treated exactly like they were set to display:inline-block.Aceydeucy
@Aceydeucy Thanks for the research and sorry for the fuss! That seems like it would be useful to add to your answer - I'm only asking since this answer is basically the internet authority for the default display value for img elements (you are at the top for many relevant google searches!) and also since as currently written, your answer and the next one contradict each other. Just trying to help make the picture more clear for other people who are coming from Google like I did. Thanks!Trackless
Also, block isn't just about having a width and height. What is special about block is that there is a newline before and after the element. So inline-block would allow multiple inline elements that "share" the same new line before and after.Himyarite
0 Something interesting happens when you use "content: url(...)" instead of the src attribute. You cannot use width and height anymore, they are ignored (just like for normal inline elements). If you add "display: inline-block", the width and height work again.Tarweed
the requested examples are missingRoentgenology
F
63

An img element is a replaced inline element.

It behaves like an inline element (because it is), but some generalizations about inline elements do not apply to img elements.

e.g.

Generalization: "Width does not apply to inline elements"

What the spec actually says: "Applies to: all elements but non-replaced inline elements, table rows, and row groups "

Since an image is a replaced inline element, it does apply.

Fashoda answered 8/3, 2010 at 16:22 Comment(0)
R
17

IMG elements are inline, meaning that unless they are floated they will flow horizontally with text and other inline elements.

They are "block" elements in that they have a width and a height. But they behave more like "inline-block" in that respect.

Relevant answered 8/3, 2010 at 16:22 Comment(0)
P
7

For almost all purposes think of them as an inline element with a width set. Basically you are free to dictate how you would like images to display using CSS. I generally set a few image classes like so:

img.center {display:block;margin:0 auto;}

img.left {float:left;margin-right:10px;}

img.right  {float:right;margin-left:10px;}

img.border  {border:1px solid #333;}
Perfumery answered 8/3, 2010 at 17:10 Comment(0)
L
3

Whenever you insert an image it just takes the width that the image has originally. You can add any other html element next to it and you will see that it will allow it. That makes image an "inline" element.

Loaf answered 4/4, 2016 at 9:58 Comment(0)
H
0

It's true, they are both - or more precisely, they are "inline block" elements. This means that they flow inline like text, but also have a width and height like block elements.

Hemostat answered 22/4, 2020 at 15:45 Comment(0)
V
-1

<img> is a replaced element; it has a display value of inline by default, but its default dimensions are defined by the embedded image's intrinsic values, like it were inline-block. You can set properties like border/border-radius, padding/margin, width, height, etc. on an image.

Replaced elements : They're elements whose contents are not affected by the current document's styles. The position of the replaced element can be affected using CSS, but not the contents of the replaced element itself.

Referenece : https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img

Vicinage answered 6/2, 2021 at 14:59 Comment(0)
C
-1

The is considered as an inline element because it allows other elements including itself too sit on the same line. It can also have some block features like styling of the width and height. But you can change it by setting the display property of the element in CSS to 'inline-block'. That is: img {display:inline-block;}

Coarsen answered 9/8, 2021 at 15:10 Comment(0)
S
-2

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.

Studious answered 17/1, 2021 at 13:0 Comment(0)
P
-3

is an inline element ..but in css you can change it simply by:- img{display:inline-block;} or img{display:inline-block;} or img{display:inliblock;}

Plio answered 4/1, 2021 at 10:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.