# Using CSS Flexbox to reflow content

## Metadata

css

sufficient

## Description

The objective of this technique is to be able to present content without introducing a horizontal scroll bar at a width equivalent to 320 CSS pixels, or a vertical scroll bar at a height equivalent to 256 CSS pixels for text intended to scroll horizontally. This is done by using layout techniques that adapt to the available viewport space.

Flexbox layouts define layout regions that reflow as needed to display the region on the screen. Although the exact layout therefore varies, the relationship of elements and the reading order remains the same when done right. This is an effective way to create designs that present well on different devices and for users with different zoom preferences.

The basic principles of flexbox layouts are to:

1. Define the size of layout regions using flexbox properties and media queries for specific viewport sizes, so they enlarge, shrink or wrap in the available space and respond to zoom levels;
2. Position the layout regions in the flexbox container as a row of adjacent flexbox items, which may wrap to new rows as needed in much the same way as words in a paragraph wrap.

Flexbox has the possibility to cause a keyboard navigation disconnect by using the order and reverse properties. The CSS Flexible Box Layout module warns against resequencing content logic as they cause incorrect source ordering and are non-conforming.

## Examples

### Example 1: Medium complex flexbox layout in HTML and CSS

The following medium complex example uses HTML and CSS to create a flexbox layout. The layout regions adjust their size as the viewport is adjusted. When the total viewport width matches the width defined via media queries, columns wrap to be positioned below, rather than beside each other or vice versa.

The zoom level can be increased to 400% without requiring scrolling in more than one direction. This particular example uses percent sizes for the flex items by using the "flex-basis" property and are laid out in source order.

      <!DOCTYPE html>
      <html lang="en">
        <head>
        <meta charset="UTF-8">
        <title>Using CSS Flexbox for Reflow</title>
        <style>

        /* Reflow Styling */

        .row {
          width: 100%;
          display: flex;
          flex-flow: row wrap;
        }

        .row-nested {
          width: calc(100% + 2rem);
          margin: 0 -1rem 1rem -1rem;
        }

        .col {
          padding: 1rem;
          flex: 0 1 100%;
        }

        @media all and (min-width: 576px) {
          .col-panel {
            flex: 0 1 50%;
            padding-bottom: 0.25rem;
          }
        }

        @media all and (min-width: 992px) {
          main {
            flex: 0 1 66.333333%;
          }
          aside {
            flex: 0 1 33.333333%;
            margin-top: 0;
          }
        }
        </style>
        </head>
        <body class="row">
          <header class="col">
            ...
          </header>
          <main class="col">
            ...
            ...
            <div class="row row-nested">
              <div class="col col-panel">
                ...
              </div>
              <div class="col col-panel">
                ...
              </div>
            </div>
          </main>
          <aside class="col">
            ...
          </aside>
          <footer class="col">
            ...
          </footer>
        </body>
      </html>

Working example: Using CSS Flexbox for Reflow

## Tests

### Procedure

1. Display the web page in a user agent capable of 400% zoom and set the viewport dimensions (in CSS pixels) to 1280 wide and 1024 high.
2. Zoom in by 400%.
3. For content read horizontally, check that all content and functionality is available without horizontal scrolling.
4. For content read vertically, check that all content and functionality is available without vertical scrolling.

If the browser is not capable of zooming to 400%, you can reduce the width of the browser proportionally. For example, at 300% zoom the viewport should be sized to 960px wide.

### Expected Results

#3 and #4 are true.

## Related Techniques

- Grid reflow technique

## Related resources

- CSS Flexible Box Layout Module Level 1
- MDN Flexible Box Layout
- CSS Tricks Guide to Flexbox