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

Box alignment

3m:30s

When using Flexbox or Grid for laying out your boxes, you also have a variety of options for adjusting the alignment of the content within those boxes, using the box alignment properties.

For now, they will only apply to display modes that utilise the parent-child relationship. The properties that are applied onto the container will affect all the items within the container, while properties that are applied on the individual item are used for adjustments within that item only.

Box alignment properties will only have a visual effect if there is free space within the box for the content to move around.

The justify-* keyword always aligns in the inline direction or the direction of content flow. So when the text is flowing horizontally from left-to-right and down the page, justify-* will adjust your content along the horizontal axis.

If your content is flowing vertically from top-to-bottom, say when the flex-direction is set to column, then justify-* will adjust your content along the vertical axis instead.

The align-* keyword always aligns in the block direction, which is the direction perpendicular to content flow. Given there are only 2 possible axes, it might make sense to just remember what justify does, since align will always affect the other axis.

Properties for the container

There are 4 properties that can be used on the container:

  • justify-content
  • align-content
  • justify-items* (does not apply in flex formatting contexts)
  • align-items

The justify-content / align-content properties are used to adjust the content within the parent container, provided there is extra space available around the content.

Screenshot of Firefox devtools showing available free space in a flex container

In the layout above, there is additional space on the right side of the flex container, which means we are able to use the justify-content property to control how we want this free space distributed.

The available options are:

  • start
  • center
  • end
  • space-around
  • space-between
  • space-evenly

If you are using Chrome Devtools, you will be able to see icons describing the behaviour of these values if you hover over the icon next to the display property.

As for align-content, it will take effect if there is free space along the cross-axis or block direction. If a flex or grid item does not have an explicit height set, its alignment along the cross-axis or block direction will be stretch, taking up all the free space available.

If you find that setting align-content does nothing for your layout, you might want to check if your items have already taken up all the free space available or not.

In the example below, the grid items’ height is less than the container’s height and you can use the same 6 values as justify-content to control how you want this free space to be distributed in your container.

Screenshot of Safari devtools showing explanation of the align-content property

The align-items property is used to align all the flex items or grid items along its flex line or grid row respectively. As mentioned earlier, without an explicit height set, the default state of the items is stretch.

However, when you set a value for align-items which is anything other than stretch, the item automatically shrink to its content height, and then be aligned to either the start, center or end of the flex line or grid row.

Where align-content is used to manage the overall free space in the entire container, align-items is used to manage the items’ positions in their respective “lanes”.

The justify-items property only applies to grid items because only grid items potentially have space along the inline-axis to “move around”. Flex items are always flush against their adjacent siblings along the main axis, and only have space to move along the cross axis.

Illustration of boxies demonstrating available free space around grid/flex item content

Properties for the item

If you need to adjust individual flex items position along the flex line, you can use align-self. And if you need to adjust individual grid items within the available space in a grid cell, you can use align-self and justify-self.