CIS195 Web Authoring 1: HTML

CSS Flexbox

 

Topics by Week for the Ten-Week Term
1. Intro to HTML 6. Layout with CSS
2. More HTML, file paths 7. CSS Grid and Flexbox
3. Site structure and navigation 8. HTML Forms
4. Formatting with CSS 9. Multimedia
5. Midterm, Project Propposal 10. Tables, Project Completion
11. Final

 

Contents

Introduction

Flexbox is used to control the layout of elements in one dimension, either horizontally or vertically. This is one of the main differences from the CSS grid, which controls layout in two dimensions.

Defining a Flexbox Container

To use flexbox, you first define a flexbox container using this CSS property: display: flex
You may then place elements inside the flexbox container. For example:

This flexbox container isn't doing anything very interesting yet other than positioning the paragraphs horizontally. (They would normally be stacked vertically.) Here is how the elements are arranged (with borders added to make the elements visible).

Paragraph 1

Paragraph 2

Paragraph 3

 

Setting the Flexbox Direction

By default the flexbox arranges the elements inside it horizontally. The CSS property flex-direction controls the direction of the flexbox. The options for the value are:

Example of flex-direction: column:

This is rendered in the browser like this:

Paragraph 1

Paragraph 2

Paragraph 3

With flex-direction set to column-reverse it looks like this:

Paragraph 1

Paragraph 2

Paragraph 3

 

Flexbox Alignment Properties

These properties can be used to further control the positioning of elements inside a flexbox:

align-items

Aligns the flex items in a direction at right angles to the container direction (cross-axis).

(This property can also be used on CSS grid containers.)

Value options:

 

align-content

Aligns the flex items in a direction at right angles to the container direction.
This property determines the spacing between lines, while align-items determines how a group of items are aligned within the container. When there is only one line, this property has no effect. One way to get multiple lines is by applying the property flex-wrap:wrap;.

justify-content

Used to align the flexbox items in a row. This has no effect when the flex-direction is column.

Value options:

 

Flexbox Flow Properties

Flex Item Properties

These are properties you can apply to individual items within a flexbox container.

References


Creative Commons License Web Authoring Lecture Notes, written by Brian Bird in are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.