Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Essential CSS
Lesson Plan
  1. Introduction
    1. Welcome to Essential CSS
    2. Getting started with the course
    3. What are file formats?
    4. Web standards
    5. What is HTML?
    6. What is CSS?
    7. Devtools, your indispensable best friend
  2. CSS Key Concepts
    1. Some background about CSS
    2. CSS Syntax and Terminology
    3. Selectors
    4. Cascade and Specificity
    5. Origin and Importance
    6. Inheritance
    7. How browsers handle CSS
  3. Basic Mental Model
    1. Everything on the web is a box
    2. The box model
    3. How big is your box?
    4. The relationship between px and em
    5. The `box-sizing` property
    6. The `display` property
    7. Formatting contexts
    8. Block boxes
    9. Inline boxes
    10. What is inline-block?
  4. Flexbox and Grid
    1. The parent-child relationship
    2. Sizing in Flexbox
    3. Flex directions
    4. Basic grid concepts
    5. Setting up a grid
    6. Placing grid items
    7. Box alignment
  5. Building web layouts
    1. Arranging your boxes on the web
    2. Positioning schemes
    3. Floating boxes
  6. Typography
    1. Making your text look good
    2. What is a font?
    3. Typesetting with CSS

Positioning schemes

3m:48s

How does the browser determine the layout of the boxes it generates? There are a number of factors involved:

  1. box dimensions and type (which we covered extensively in the previous section)
  2. positioning scheme
  3. relationships between elements in the document tree
  4. external information (e.g. viewport size, intrinsic dimensions of images etc.)

In this lesson, we will take an in-depth look at how positioning schemes work. If you had been following the lessons in order, you would have come across the term “normal flow” a few times by now. It is the laying out of boxes, one after another, either in a block formatting context or an inline formatting context.

Conceptually, a box can be in normal flow, floated or out of flow. The value of the position property determines which positioning scheme the browser will use to calculate the position of the box.

Any value other than static will make the box a positioned box, and allow the box’s position to be offset from various starting points depending on the positioning scheme used.

Static

The initial or default value for position is static. Boxes with this value are simply laid out according to their formatting context. They are considered in normal flow.

Boxes in normal flow will have an influence on the size and positioning of its sibling boxes and ancestor boxes. For example, under normal circumstances, the parent container will expand to accommodate the boxes of content within it.

Relative

A relative positioned box is laid out in normal flow as well, similar to that of a static positioned box. But it can be offset from its given position using the offset properties of top, right, bottom and left. The offset will not affect the positioning or sizing of the adjacent boxes around it though.

Screenshot showing a relatively positioned box offset 20px from the top and 25px from the left

The flow of the boxes adjacent to the offset box only takes into account the original position of the box when it was first laid out, and not it’s offset position.

Sticky

A sticky positioned box similar to a relatively positioned box, and still remains in normal flow. However, it has some behaviours that distinguish it when scrolling is involved.

Unless you have very little content, most of the time, you will need to scroll down to access all the content on your page. And even if you have little content on your page, scrolling might occur if you resize your browser window to a small size as well.

When there is scrolling, it implies that there is content that is outside the viewport that you have to scroll to get to. With sticky positioning, it is possible to have content stay within the viewport instead of scrolling off-screen.

An offset value must be set on the box you want to be sticky. In the above example, the offset value for the sticky box was set to top: 0px, hence when the scroll starts, the sticky box remains “stuck” to the top of the viewport while the other boxes go out of the screen.

Absolute

An absolutely positioned box is taken out of normal flow altogether. This is in contrast with the previous positioning schemes, where all the boxes flow one after another.

Once out of flow, the box no longer has any impact on the size or position of its siblings and ancestors. Instead, it’s position and size is in relation to its absolute positioning containing block.

This containing block is the nearest positioned ancestor of the absolutely positioned box. If there are no positioned boxes on the page, then this defaults to the body element.

Screenshot showing an absolutely positioned box overlapping in-flow content

Because the box is now out of flow, it can overlap in-flow content or other absolutely positioned boxes. It’s size and positioning is solely determined relative to its containing block. If there are no offset values set, it will default to the upper left corner of its immediate parent.

Fixed

A fixed positioned box has the same behaviour as an absolutely positioned box, except that its reference point for positioning and sizing is always the viewport.

Screenshot showing a fixed position box remaining in the same position regardless of scroll

Regardless of how content is scrolled or how the viewport is resized, the fixed position box will always remain in the same position relative to the offset values in reference to the viewport.