Chapter 7. Bridging HTML and CSS

Block Element vs. Inline Element

Block Element vs. Inline Element
Tag:

Understanding Block elements (or Block-level elements) and Inline elements (or Inline-level elements) is very important, especially for layout design. The key differences between these two types of element categories are:

  1. Default width
  2. Line break
  3. Sizing and spacing
  4. Position alignment
  5. Nesting

Block elements

Block elements have the following key characteristics.

  1. A block element stretches out from the left end to the right end as a default setting.
  2. A block element always starts on a new line.
  3. You can set size (height and width) and spacing (padding and margin).
  4. For position alignment, you cannot use text-align and vertical-align. An easier way to align the positions of block elements is by using the display property or margin: auto which will be explained later.
  5. You can nest both block elements and inline elements under a block element.

The illustration below shows how block elements are displayed by default.

Block-Element-vs-Inline-Element

Inline elements

Inline elements have the following key characteristics.

  1. The default width is determined by the length of the content
  2. There is no line break between inline elements. You cannot set the height and width for inline elements.
  3. You can set the margin and padding horizontally, but you cannot set the margin and padding vertically. (Technically, you can set the padding vertically; however, the layout will be broken if you set the padding vertically.)
  4. For position alignment, you can use text-align and vertical-align.
  5. You can nest only inline elements (see the illustration below).

Block-Element-vs-Inline-Element

The illustration below shows how inline elements are displayed by default.

Block-Element-vs-Inline-Element

<a> tag to nest block elements

The <a> tag can be an exception for the inline element's nesting rule. The <a> tag can be used to nest block elements when you want to add a hyperlink to a component such as a card that consists of multiple block elements.

Block element vs. inline element summary

The differences between those two types of elements come from the roles of elements. Block elements are typically used for layout design while inline elements are often used within a paragraph.

Block-Element-vs-Inline-Element

Starting from HTML 5, you can change the type of elements using the display property. Also, HTML5 introduced another type of element category called inline-block element. We'll explain these new concepts later in this course.

IdeaHTML 5 Content Models

The two-element categories (inline element and block element) have been used before HTML 5. In HTML 5, a more granular element categorization is formally defined. The categorization is called Content Models. It has the following seven categories:

  • Metadata content
  • Flow content
  • Sectioning content
  • Heading content
  • Phrasing content
  • Embedded content
  • Interactive content

Unlike block element and inline element categorization, Content Models are not mutually exclusive. One element can have multiple content categories like shown in the illustration below.

Block-Element-vs-Inline-Element

More detailed explanations are available on the HTML Living Standard and W3C websites.

HTML Living Standard: Content Models

W3C: HTML 5 Content Models

Idea<img> as an inline element

The <img> tag is considered an inline element. If we follow the definition, we should not be able to set the height and width for images; however, setting the height and width is, in fact, possible. The <img> tag is an exception to that rule.

Block-Element-vs-Inline-Element

This aspect is the same for some form elements such as <button> and <input>. You can set the width and height for these elements as well. As HTML 5 uses the new Content Models, there are several exceptions like these now.

Understanding Block elements (or Block-level elements) and Inline elements (or Inline-level elements) is very important, especially for layout design. The key differences between these two types of element categories are:

  1. Default width
  2. Line break
  3. Sizing and spacing
  4. Position alignment
  5. Nesting

Block elements

Block elements have the following key characteristics.

  1. A block element stretches out from the left end to the right end as a default setting.
  2. A block element always starts on a new line.
  3. You can set size (height and width) and spacing (padding and margin).
  4. For position alignment, you cannot use text-align and vertical-align. An easier way to align the positions of block elements is by using the display property or margin: auto which will be explained later.
  5. You can nest both block elements and inline elements under a block element.

The illustration below shows how block elements are displayed by default.

Block-Element-vs-Inline-Element

Inline elements

Inline elements have the following key characteristics.

  1. The default width is determined by the length of the content
  2. There is no line break between inline elements. You cannot set the height and width for inline elements.
  3. You can set the margin and padding horizontally, but you cannot set the margin and padding vertically. (Technically, you can set the padding vertically; however, the layout will be broken if you set the padding vertically.)
  4. For position alignment, you can use text-align and vertical-align.
  5. You can nest only inline elements (see the illustration below).

Block-Element-vs-Inline-Element

The illustration below shows how inline elements are displayed by default.

Block-Element-vs-Inline-Element

<a> tag to nest block elements

The <a> tag can be an exception for the inline element's nesting rule. The <a> tag can be used to nest block elements when you want to add a hyperlink to a component such as a card that consists of multiple block elements.

Block element vs. inline element summary

The differences between those two types of elements come from the roles of elements. Block elements are typically used for layout design while inline elements are often used within a paragraph.

Block-Element-vs-Inline-Element

Starting from HTML 5, you can change the type of elements using the display property. Also, HTML5 introduced another type of element category called inline-block element. We'll explain these new concepts later in this course.

IdeaHTML 5 Content Models

The two-element categories (inline element and block element) have been used before HTML 5. In HTML 5, a more granular element categorization is formally defined. The categorization is called Content Models. It has the following seven categories:

  • Metadata content
  • Flow content
  • Sectioning content
  • Heading content
  • Phrasing content
  • Embedded content
  • Interactive content

Unlike block element and inline element categorization, Content Models are not mutually exclusive. One element can have multiple content categories like shown in the illustration below.

Block-Element-vs-Inline-Element

More detailed explanations are available on the HTML Living Standard and W3C websites.

HTML Living Standard: Content Models

W3C: HTML 5 Content Models

Idea<img> as an inline element

The <img> tag is considered an inline element. If we follow the definition, we should not be able to set the height and width for images; however, setting the height and width is, in fact, possible. The <img> tag is an exception to that rule.

Block-Element-vs-Inline-Element

This aspect is the same for some form elements such as <button> and <input>. You can set the width and height for these elements as well. As HTML 5 uses the new Content Models, there are several exceptions like these now.

Tag: