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.
- Open, review, and render the following example application of using CSS Flex – flexbox-layout.html and flexbox.css files in the week04 folder.
- Video Demonstration: ▶️ CSS Flexbox Basics [ 3:01 minutes ]
- Open, review, and render the following example application of using CSS Flex in a navigation bar – Example: Open the flexbox-menu.html and flexbox-menu.css files in the week04 folder.
- Video Demonstration: ▶️ Using CSS Flexbox on a Menu [ 1:33 minutes ]
Key Concepts
- Flex containers and flex items: The container is the parent and has the
display: flex;declaration. The items are the direct children of the container and can be manipulated using various flex properties. - Flex direction: The
flex-directionproperty defines the direction in which the flex items are placed in the flex container. It can be set torow,row-reverse,column, orcolumn-reverse. - Justify content: The
justify-contentproperty aligns the flex items along the main axis (horizontally in a row, vertically in a column). It can be set to values likeflex-start,flex-end,center,space-between, orspace-around. - Align items: The
align-itemsproperty aligns the flex items along the cross axis (vertically in a row, horizontally in a column). It can be set to values likeflex-start,flex-end,center,baseline, orstretch. - Place items: The
place-itemsproperty is a shorthand foralign-itemsandjustify-items. It can be used to control the alignment of items within a grid container.
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:
Link: ▶️ Using CSS Flexbox on a Menu [ 1:33 minutes ]
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
- Complete the following online tutorial on CSS Flexbox (24 Levels): Flexbox Froggy
Screenshot of Flexbox Froggy Level 22
Optional Resources
- Flexbox – MDN