WDD 130: Web Fundamentals

W04 Learning Activity: CSS Flex

Overview

Responsive webpages are pages that automatically adjust their layout, content, and design elements to provide an optimal viewing experience across different screen sizes and devices, from desktop computers to tablets to mobile phones.

The flex CSS display property makes creating and controlling responsive layouts easy, especially in a single horizontal or vertical container. Flexbox is a one-dimensional layout method for laying out items in rows or columns. Items flex to fill additional space and shrink to fit into smaller spaces.

Grid layouts, which will be discussed in another activity, are also used for creating responsive webpages. Flexbox layout is most appropriate for the components of a page, and small-scale layouts, while the Grid layout is intended for larger scale layouts.

Prepare

CSS Flexbox makes vertically centering a block of content inside its parent easier. The children of a flex container work together to fill the available, designed space.

Key Concepts

The following example shows how these could be applied to a container to arrange its children in a horizontal row, centered horizontally and vertically.

.flex-container {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

View and play around with a flexbox layout yourself. Open your course repository in VS Code, and browse to the week04 folder. Then, open, review, and play around with flexbox-layout.html and flexbox.css in the styles subfolder (try different values and see the results).

Watch the following video to learn how to use Flexbox to create a menu:

View and work around with a flex menu yourself. In the week04 folder, open the flexbox-menu.html and styles/flexbox-menu.css . Change the values in these files and observe the changes in the page.

Activity Instructions

  1. Complete the following online tutorial on CSS Flexbox (24 Levels): Flexbox Froggy
    Screenshot of flexboxfroggy.com Level 22 of 24
    Screenshot of Flexbox Froggy Level 22

Optional Resources