Home > Web Front-end > Front-end Q&A > Is img in html5 a block-level element?

Is img in html5 a block-level element?

青灯夜游
Release: 2021-12-16 16:15:56
Original
3158 people have browsed it

html5 img is not a block-level element. img is an inline element and is also a replacement element. It has built-in width and height attributes, so the img tag can set the width and height. img is an inline replacement element. Height, width, padding, and margin are all available, and the effect is equal to that of block elements.

Is img in html5 a block-level element?

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

<img /> Basic use of tags

Browser support

  • All major browsers support<img> Tag

Tag definition and usage instructions

  • <img> Tag defines an image in an HTML page
  • <img> The tag has two required attributes: src and alt
  • It is highly recommended to use the alt attribute on every image in development. This way, even if the image cannot be displayed, the user can still see some information about what is missing. And for people with disabilities, the alt attribute is often the only way they know what the image is about

<img />What exactly is it

<img /> Is it an inline element or a block-level element?

  • <img /> The tag does not occupy an exclusive line, so it is an inline element, which is no problem

Since it is an inline element, why can the width and height be set?

  • This question will lead to the following part. <img /> The label is a replacement element and has built-in width and height attributes, so It can be set, see below for detailed explanation.

Definition of elements

From the characteristics of the element itself, it can be divided into non-replaceable elements and replaceable elements

Element-related MDN explanation

Irreplaceable element

  • (X)HTML Most elements are non-replaceable elements, that is, their content is directly displayed to the user (such as a browser)
  • For example: <h1>I am the title</h1>

Replaceable elements

  • The browser determines the specific display content of the element based on its tags and attributes
  • For example, the browser will read the image information and display it based on the value of the src attribute of the <img> tag. However, if you view the (X)HTML code, you cannot see the actual content of the image; and For example, depending on the type attribute of the <input> tag, it is decided whether to display an input box or a radio button, etc.
  • (X)<img>, < in HTML input>,