CSS Lab 2 — Box model

Edit css/styles.css to perform experiments. Use DevTools → Computed box model to read padding / border / margin / content sizes.

Exercise: inspection

Open DevTools → inspect the Computed box

Movie title

Short tagline showing how text flows inside padding.

A paragraph to create height; used to inspect vertical padding and margin.

Read more

Exercise: box-sizing

We have two identical boxes configured with:

.box {
  width: 12.5rem;  /* 200px */
  padding: 1.25rem; /* 20px */
  border: 0.3125rem solid #4b9; /* 5px */
  border-radius: 0.375rem;
  background: #fff;
}
Box A
box-sizing: content-box
width: 12.5rem; padding:1.25rem; border:0.3125rem;
Box B
box-sizing: border-box
width: 12.5rem; padding:1.25rem; border:0.3125rem;

Exercise: width units (px, %, vw)

Compare fixed, relative, and viewport-based widths. Resize the window and watch how they behave.

width: 240px
width: 60%
width: 30vw
width: 20rem

Exercise: min/max-width & wrapping

Keep content readable by limiting line length with min/max-width.

A paragraph with min/max-width

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laudantium possimus quisquam dolore maiores cupiditate repellat numquam ratione illum minima similique. Quis, deleniti. Accusamus, doloribus reiciendis? Perspiciatis culpa eveniet voluptates?

Exercise: height & overflow

Explore how fixed heights can cause overflow and how to control it.

overflow: visible (default)

Long content… Long content… Long content… Long content… Long content…

overflow: hidden

Long content… Long content… Long content… Long content… Long content…

overflow: auto (scrollable)

Long content… Long content… Long content… Long content… Long content…

Exercise: Quirky Margins

Observe vertical margin behavior. Read the short tasks and record what you see.

Adjacent siblings

Box 1 has a margin bottom defined, Box 2 has a margin top defined.

Box 1 (margin-bottom: 40px)
Box 2 (margin-top: 20px)
  1. How large is the gap between Box 1 and Box 2. Is it 60px or 40px or?

Parent & first-child

Margins can behave unexpectedtly in specific edge-cases..

First child (margin-top: 30px)

Exercise: border vs outline

Discover the differences between border and outline.

border: 8px solid
outline: 8px solid

Bonus: Building a “polaroid” frame

Use margins, padding and borders to create a "photo frame".
The end result is shown in this image below:

End result of exercise

A tiny sunset

  1. Use linear-gradient and fixed height to create a "sunset" photo.
  2. Add a border to the photo.
  3. Update the caption by centering the text, decreasing the font-size slightly and adding a margin.
  4. Add a padding to the parent container.