WDD 130: Web Fundamentals

HTML: Block versus Inline Elements

Overview

An HTML element is, by default, either a block-level or inline element. Block elements occupy the full width of their container and start on new lines. Inline elements only occupy the space needed for its content and does not start on a new line.

"A Block-level element occupies the entire horizontal space of its parent element (container), and vertical space equal to the height of its contents, thereby creating a "block"." ... "Inline elements are those which only occupy the space bounded by the tags defining the element, instead of breaking the flow of the content."- MDN

Prepare

Carefully ponder, note, and reference the materials provided.

Activity Instructions

In this activity, answer the following questions about HTML elements.

  1. Which of the following HTML elements are inline elements by default?
    <header> <div> <span> <table> <form> <a> <label> <button> <input> <p> <aside> <cite>
    Answers <span> <a> <label> <button> <input> <cite>

    The remaining elements listed are 'block-level' elements by default.

    How are you supposed to figure out which is which? There is nothing wrong with trying them out on your page and, in addition, you should be comfortable looking up details as needed using focused research. For example, you can use the HTML Reference to look up the elements and see if they are block or inline elements.

  2. Go to this code snippet on CodePen and note that the HTML, CSS, and JavaScript are separated into panels.
    Let us focus on the HTML and the block and inline elements that are being used.
    1. In the HTML window, change the second paragraph element to a <div> element. What happened?
      Answer

      Nothing should have changed. It is OK for a <div> block element to contain another <div> element.

    2. Now change the first or outer <div> element to a <p> element so that a paragraph contains a div. However, what happened?
      Answer

      This creates an unwanted result. The paragraph element ends early as by rule, it cannot contain another block-level element. paragraph elements can only contain inline elements.

    3. Take a look at how the inline elements are only as big as their content and display inline if there is room. These inline elements are common. Can you identify them?
      Answers <span> <img> <a> and <button>

Optional Resources