简体   繁体   English

是<img>元素块级还是内联级?

[英]Is <img> element block level or inline level?

I've read somewhere that <img> element behaves like both.我在某处读到<img>元素的行为类似于两者。 If correct, could someone please explain with examples?如果正确,有人可以用例子解释一下吗?

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*.在 CSS 中,您可以将元素设置为display: inline-block以使其复制图像的行为*。

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. * 请注意,浏览器在技术上使用display: inline (如开发人员工具中所见),但它们对图像进行了特殊处理。 They still follow all traits of inline-block .它们仍然遵循inline-block所有特征。

An img element is a replaced inline element . img元素是被替换的内联元素

It behaves like an inline element (because it is), but some generalizations about inline elements do not apply to img elements.它的行为就像一个内联元素(因为它是),但是一些关于内联元素的概括不适用于img元素。

eg例如

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.由于图像是替换的内联元素,因此它确实适用。

IMG elements are inline, meaning that unless they are floated they will flow horizontally with text and other inline elements. IMG 元素是内联的,这意味着除非它们是浮动的,否则它们将与文本和其他内联元素一起水平流动。

They are "block" elements in that they have a width and a height.它们是“块”元素,因为它们具有宽度和高度。 But they behave more like "inline-block" in that respect.但在这方面,它们的行为更像是“内联块”。

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.基本上,您可以使用 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;}

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.你可以在它旁边添加任何其他 html 元素,你会看到它会允许它。 That makes image an "inline" element.这使得图像成为“内联”元素。

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.这意味着它们像文本一样内联流动,但也像块元素一样具有宽度和高度。

<img> is a replaced element; <img>是被替换的元素; 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.默认情况下,它的显示值为 inline,但其默认尺寸由嵌入图像的内在值定义,就像 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.使用 CSS 可以影响被替换元素的位置,但不会影响被替换元素本身的内容。

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

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'.但是您可以通过将 CSS 中元素的 display 属性设置为 'inline-block' 来更改它。 That is: img {display:inline-block;}即: img {display:inline-block;}

behaves as an inline-block element as it allows other images in same line ie 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.因此,同时提供内联元素和块元素的特性。

是一个内联元素..但是在 css 中你可以简单地改变它:- img{display:inline-block;} 或 img{display:inline-block;} 或 img{display:inliblock;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM