Box Model

Each HTML element can be seen as a container with visual behavior following the so called HTML Box Model, defining the rectangular region occupied by an HTML element.
A box is geometrically associated to three main visible attributes
Note that the width and height of a box correspond to the dimension of its content (excluding padding and margin)
pictures/box_model.png

Display

Websites contain a large variety of visual elements (text, images, etc.) which follows different behaviors. In some cases, the box itself has a specific meaning, and its dimension may be directly specified by the user. In other cases, the box itself has no meaning without internal content, thus its behavior is defined by its content.
These different behaviors are related to three main display styles: inline, inline-block, and block. (more information on layout).

Block

Block elements elements that spans an entire line.
Containers of texts such as titles <h1>, ..., <h6>, as well as paragraphs <p> have block display by default. You can force any element to have an block display in css using
.selector {
  display: block;
}
Example:
<h1> A Title </h1> 
<h2> A sub-title </h2>
<p> In this paragraph, the tag <strong>strong</strong> is set to have a display: block;</p>

Inline-block

Inline-block elements are rectangular box from which size (width, height), margin, padding can be fully defined by the user.
Images have inline-bloc display by default. And you can force any element to have an inline display in css using
.selector {
  display: inline;
}
Example:
  • rem. By default, the inline-block box that are on the same line are positioned vertically such that their baseline (bottom of standard text line, and bottom of image) are aligned. This property can be modified with vertical-align.

Inline

Inline is used to display element with a text-like behaviors.
    • Text height depends on the font, and the space between two lines can be set via line-height instead.
Tags highlighting words with some properties such as <a>, <em>, <strong> have by default inline display behavior. ex.
Links have <a href="https://www.w3schools.com/cssref/pr_class_display.asp"> inline display</a>. 
<em> Emphasized </em>, or <strong> strong </strong> highlighted words have also inline display.
<a target="_blank" href="."> Strong <strong>links</strong> are neasted inline elements. </a>
Example:
You can force any element to have an inline display in css using
.selector {
  display: inline;
}

Neasted blocks

The following example shows the behavior of neasted boxes depending on their respective display. The page contains: A set of button allows to change the type of display of the container, and the elements. A checkbox also allows to force the width of the container to be set at 40% of the page width. Wepbage

Exercise 1

Consider the following HTML
<p>Some Text</p>
Set the following CSS code to create a visible box around the paragraph.
p {
  display: inline-block;

  /* set dimension of the box */
  width: 120px;
  height: 100px;
  padding-top: 20px;

  /* Set color and border */
  background-color: cyan;
  border: 2px solid blue;
  border-radius: 10px;
}
Adapt the HTML code, and CSS padding, and margin of the paragraph element to have the following result:

Exercise 2

Consider the following HTML body
<section>
  <p> A </p>
  <p> B </p>
  <p> C </p>
</section>
<footer>
  <p> D </p>
  <p> E </p>
  <p> F </p>
</footer>
pictures/exercise.png
When appropriate, don't forget to avoid code duplication in factorizing common properties within the same rule.
Tips: