1.1 Overview
1.1. Overview
This section is not normative.
Flex layout is superficially similar to block layout. It lacks many of the more complex text- or document-centric properties that can be used in block layout, such as floats and columns. In return it gains simple and powerful tools for distributing space and aligning content in ways that web apps and complex web pages often need. The contents of a flex container:
can be laid out in any flow direction (leftwards, rightwards, downwards, or even upwards!)
can have their display order reversed or 'order|rearranged' at the style layer (i.e., visual order can be independent of source and speech order)
can be laid out linearly along a single (main) axis or wrapped into multiple lines along a secondary (cross) axis
can “flex” their sizes to respond to the available space
can be aligned with respect to their container or each other on the secondary (cross)
can be dynamically collapsed or uncollapsed along the main axis while preserving the container’s cross size
Here’s an example of a catalog where each item has a title, a photo, a description, and a purchase button. The designer’s intention is that each entry has the same overall size, that the photo be above the text, and that the purchase buttons are aligned at the bottom, regardless of the length of the item’s description. Flex layout makes many aspects of this design easy:
The catalog uses flex layout to lay out rows of items horizontally, and to ensure that items within a row are all equal-height. Each entry is then itself a column flex container, laying out its contents vertically.
Within each entry, the source document content is ordered logically with the title first, followed by the description and the photo. This provides a sensible ordering for speech rendering and in non-CSS browsers. For a more compelling visual presentation, however, order is used to pull the image up from later in the content to the top, and align-self is used to center it horizontally.
An auto margin above the purchase button forces it to the bottom within each entry box, regardless of the height of that item’s description.
#deals { display: flex; /* Flex layout so items have equal height */ flex-flow: row wrap; /* Allow items to wrap into multiple lines */ } .sale-item { display: flex; /* Lay out each item using flex layout */ flex-flow: column; /* Lay out item's contents vertically */ } .sale-item > img { order: -1; /* Shift image before other content (in visual order) */ align-self: center; /* Center the image cross-wise (horizontally) */ } .sale-item > button { margin-top: auto; /* Auto top margin pushes button to bottom */ }
<section id="deals"> <section class="sale-item"> <h1>Computer Starter Kit</h1> <p>This is the best computer money can buy, if you don’t have much money. <ul> <li>Computer <li>Monitor <li>Keyboard <li>Mouse </ul> <img src="images/computer.jpg" alt="You get: a white computer with matching peripherals."> <button>BUY NOW</button> </section> <section class="sale-item"> … </section> … </section>