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

The parent-child relationship

2m:28s

While block and inline layouts were part of CSS since its inception, flex layout was introduced much later and describes a box model that is, as its name suggests, flexible when it comes to sizing, and provides more granular options for controlling alignment and spacing.

Grid layout came next and gave developers unprecedented control for laying out content in a two-dimensional context, a true grid system with rows and columns, which had not been possible previously.

These 2 new layout models worked slightly differently than all the previous layout models by creating an explicit parent-child relationship between a container box and its children.

As with all the other layout models, the key property involved is display. A flex formatting context is generated when you apply display: flex to an element, by making it a flex container. All the immediate children within this flex container become its flex items.

Grid works in the same way, where applying display: grid to an element makes it a grid container, and all its immediate children become grid items.

Parent-child relationship

Laying out and sizing block boxes or inline boxes involves applying properties onto the boxes themselves. Block and inline boxes are, in a way, unaware of the boxes around themselves. In order to layout boxes in a specific manner, you would have to adjust each box individually.

For elements using flexbox or grid as their layout model, there is a clear parent-child relationship between the container and its children. For example, properties applied onto a flex container will affect the flex items within it, and the flex items themselves will have an “awareness” of their fellow flex children within the same flex formatting context.

Say we have the following markup:

<div class="container">
<span>A flex item</span>
<p>Another flex item</p>
<div>Flex items are also flex children</div>
<blockquote>
All direct children of the flex container become flex children
</blockquote>
</div>

Without adding any styles of your own, all these different HTML elements have display values set by the user-agent stylesheet. However, once you apply the following CSS:

.container {
display: flex;
}

All the direct child elements within the container div become flex items (or flex children). If you happen to be using Firefox, you can turn on the overlay via Devtools and see how the browser interprets this style rule.

Screenshot of Firefox devtools showing flex items in a container div

The CSS properties applied on the flex container largely allow you to orchestrate how all the flex children behave as a whole, while the CSS properties applied on the flex children are more for individual tweaking of each item within the flex formatting context.

Grid containers and their grid children also have this behaviour. We will examine how to make use of these characteristics to layout a webpage in the section on building web layouts. Meanwhile, the upcoming lessons will introduce the unique characteristics which flex items and grid items possess.