Detailed explanation of the difference between inline elements and block-level elements

迷茫
Release: 2017-03-25 10:46:39
Original
1771 people have browsed it

1. The difference between inline elements and block-level elements

1. Inline elements will not occupy the entire line and are arranged in a straight line. They are all on the same line, arranged horizontally;

Block-level elements will occupy one line, arranged vertically.

 2. Block-level elements can contain inline elements and block-level elements; inline elements cannot contain block-level elements.

3. The difference between the attributes of inline elements and block-level elements is mainly due to the box model attributes. The width setting of inline elements is invalid, the height is invalid (line-height can be set), and the margin up and down is invalid. , padding up and down is invalid.

2. Conversion of inline elements and block-level elements

Conversion of inline elements For block elements:display:block;

Block elements are converted into inline elements :display:inline;

##3. Question extension

Question description: Why can the width and height of inline elements such as img and input be set?

Detailed answer:

Elements are the basis of the document structure. In CSS, each element generates a box (box, also translated as "box") that contains the content of the element. But different elements will be displayed differently. For example,

and

are different, and and

are also different. Different types are specified for different elements in the document type definition (DTD), which is one of the reasons why DTD is important to documents.

1. Replaceable and non-replaceable elements

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

a) Replace element

Replacement element means that the browser determines the specific display content of the element based on its tag and attributes.

 For example, the browser will read the image information and display it based on the value of the src attribute of thetag, but if you view the (X)HTML code, you cannot see the actual content of the image; another example Determine whether to display an input box, radio button, etc. based on the type attribute of thetag.

  (X),,

  The browser will display these elements based on their tag type and attributes. Replaceable elements also generate boxes in their display.

b) Non-replaceable elements

Most elements of (X)HTML are non-replaceable elements, that is, their content is directly displayed to the user (such as a browser). For example:

Paragraph content

  Paragraph

is a non-replaceable element, and the text "Paragraph content" is fully displayed.

 2. Display elements

In addition to the classification methods of replaceable elements and non-replaceable elements, elements in CSS 2.1 also have other classification methods: block-level elements (block-level) and inline Element (inline-level, also translated as "inline" element).

 a) Block-level elements

  The most obvious feature of an element that is visually formatted as a block is that it fills the content area of its parent element horizontally by default, and on both its left and right sides Block-level elements are on their own line by default if there are no other elements.

Typical block-level elements are:

,

,

to

, and so on.

  Elements that are floated (float attribute, can float to the left or right) through CSS and set the display attribute to "block" or "list-item" are all block-level elements. .

  But floating elements are special. Due to floating, there may be other elements next to them. And "list-item" (list item

  • ) will generate a dot symbol or numeric sequence in front of it.

    b) Inline elements

    Inline elements do not form a new content block, that is, there can be other elements around them, such as,,, etc., They are all typical inline-level elements.

       Elements whose display attribute is equal to "inline" are all inline elements. Almost all replaceable elements are inline elements, such as,, etc.

    However, the type of elements is not fixed. By setting the display attribute of CSS, inline elements can be changed into block-level elements, and block-level elements can also be changed into inline elements.

    3. Conclusion:

    Replacement elements generally have intrinsic dimensions, so they have width and height, which can be set. For example, if you do not specify the width and height of the img, it will be displayed according to its intrinsic size, which is the width and height of the image when it is saved.

    For form elements, browsers also have default styles, including width and height.

    The above is the detailed content of Detailed explanation of the difference between inline elements and block-level elements. For more information, please follow other related articles on the PHP Chinese website!

  • Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!