Skip to Main Content

Cheat Codes for Advanced Layout in LibGuides: Flex

Basics

Add to HTML

In the container, add class="flex". To adjust the layout you can also add other class attributes within this class, based on what you need. See Options below.

Best Used When...

Flexbox is particularly good at distributing space and aligning content. It's best used when:

  • You have a number of similar elements that you want automatically aligned within the box, OR
  • You want columns to be the same height and don't mind varied widths, OR
  • You have an item(s) you want centered horizontally.

Flex Container Properties

Flex Direction

Flex direction defines which direction the container should stack the flex items.

Options for Direction
Our CSS Classes Flex items will:
default (no additional class) line up as a row
flex-direction-column line up as a column

Wrap

The default is for flex items to continue in the same row indefinitely, but this can make the content overflow your LibGuides box. If you have a lot of content that causes the content to overflow its box, then you need to set flex-wrap.

Flex-Wrap option
Wrap Setting Browser Will:
default (no additional class) keep listing flex items in the same direction regardless of whether it fits in the box
wrap break the items into multiple lines

Justify Content

These options tell the browser how to distribute space between and around content items along the main-axis of a flex container. (The default main-axis is the x-axis or horizontal. If you changed the flex-direction to column, then the main-axis becomes the y-axis or vertical.)

Options for distributed spacing
Distributed Alignment Flex Items Will:
space-between touch the right and left edges of container and divide the remaining space evenly
space-around space elements evenly with half-sized space on either end
space-evenly space elements evenly with equal space around them
Options for packed spacing
Packed Alignment Flex Items Will:
center cluster in the center of the container
justify-start cluster items at the start of the container
justify-end cluster items at the end of the container

space-between

    <div class="flex space-between">
      <div><p>Lorem ipsum... </p></div>

      <div><p>Morbi...</p></div>

      <div><p>Vivamus...</p></div>
    </div>

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean purus enim, volutpat ac dolor et, sollicitudin vehicula felis. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Cras arcu turpis, maximus sit amet blandit quis, tempor id nisi. Vivamus leo mi, porttitor egestas ligula eget, dictum dignissim libero. Donec aliquam semper eros, sit amet feugiat justo ultricies ut. Quisque euismod vestibulum elementum. Morbi vestibulum eros sit amet neque viverra, ut tincidunt erat mollis. Donec suscipit at diam nec imperdiet.

Morbi porta ipsum id dolor convallis mollis. Duis at enim erat. Aliquam commodo vulputate turpis. Nullam eget quam ultrices lectus tempus pulvinar. Maecenas sagittis pretium nibh, quis faucibus ante scelerisque ac. Nullam lobortis, nulla non eleifend varius vehicula.

Vivamus pulvinar tellus eu sapien dignissim lobortis. Integer vestibulum diam sed nunc sodales feugiat. tristique nisl, ornare aliquam turpis.

Spacing

Add Spacing and Padding

    <div class="flex space-between item-padding-2 item-margin-1">
      <div><p>Lorem ipsum... </p></div>

      <div><p>Morbi...</p></div>

      <div><p>Vivamus...</p></div>
    </div>

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean purus enim, volutpat ac dolor et, sollicitudin vehicula felis. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Cras arcu turpis, maximus sit amet blandit quis, tempor id nisi. Vivamus leo mi, porttitor egestas ligula eget, dictum dignissim libero. Donec aliquam semper eros, sit amet feugiat justo ultricies ut. Quisque euismod vestibulum elementum. Morbi vestibulum eros sit amet neque viverra, ut tincidunt erat mollis. Donec suscipit at diam nec imperdiet.

Morbi porta ipsum id dolor convallis mollis. Duis at enim erat. Aliquam commodo vulputate turpis. Nullam eget quam ultrices lectus tempus pulvinar. Maecenas sagittis pretium nibh, quis faucibus ante scelerisque ac. Nullam lobortis, nulla non eleifend varius vehicula.

Vivamus pulvinar tellus eu sapien dignissim lobortis. Integer vestibulum diam sed nunc sodales feugiat. tristique nisl, ornare aliquam turpis.

Item Options

Options that Affect All Children of the Flex Container
Option Add the following as a class... In this 
flex list with no bullets flex ul
padding for each child

item-padding-1
item-padding-2
item-padding-3

container
margin around each child

item-margin-1
item-margin-2
item-margin-3

container
border around each child item-border-black-1
item-border-black-2
item-border-gray-1
item-border-gray-2
 
container
Top Bottom