understack
understack

Reputation: 11580

Is <img> element block level or inline level?

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

Upvotes: 202

Views: 236823

Answers (10)

Fortunecode
Fortunecode

Reputation: 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;}

Upvotes: -1

Ritu Raj Shrivastava
Ritu Raj Shrivastava

Reputation: 79

<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

Upvotes: -1

Ayushgupta
Ayushgupta

Reputation: 1

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.

Upvotes: -2

ritik sharma
ritik sharma

Reputation: 1

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;}

Upvotes: -3

Modaser Sadat
Modaser Sadat

Reputation: 9

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.

Upvotes: 0

Quentin
Quentin

Reputation: 943615

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.

Upvotes: 63

Avatar
Avatar

Reputation: 76

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.

Upvotes: 3

DisgruntledGoat
DisgruntledGoat

Reputation: 72550

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.

Upvotes: 226

Montana Flynn
Montana Flynn

Reputation: 299

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;}

Upvotes: 7

Robusto
Robusto

Reputation: 31883

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.

Upvotes: 17

Related Questions