Dr. Greg Bernstein
Updated September 14th, 2021
Flexbox Froggy “a game where you help Froggy and friends by writing CSS code!”
Experiment with these are you read these notes:
From MDN flexbox0.html, container is the section
element
From me: physics.html, container is the p
element holding the links to other pages.
From me: VerticalTides.html container is the body
element, uses main axis, cross axis, and item properties
From MDN:
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.
From CSS Tricks:
The main idea behind the flex layout is to give the container the ability to alter its items’ width/height (and order) to best fill the available space (mostly to accommodate to all kind of display devices and screen sizes). A flex container expands items to fill available free space, or shrinks them to prevent overflow.
From MDN: “The following simple layout requirements are either difficult or impossible to achieve with such tools, in any kind of convenient, flexible way:”
Example from MDN, three articles
within a section
, default (no flex):
Applying display: flex
to the section
containing the article
s:
From MDN:
From MDN:
From MDN:
display
and flex-direction
display: flex;
on the container element to enable flex layout.flex-direction: property
, where property = row
, row-reverse
, column
or column-reverse
flex-direction
is row
justify-content
From CSS-Tricks:
This defines the alignment along the main axis. It helps distribute extra free space left over when either all the flex items on a line are inflexible, or are flexible but have reached their maximum size.
justify-content
values IFrom CSS-Tricks:
flex-start
(default): items are packed toward the start lineflex-end
: items are packed toward to end linecenter
: items are centered along the linespace-between
: items are evenly distributed in the line; first item is on the start line, last item on the end linejustify-content
values IIFrom CSS-Tricks:
space-around
: items are evenly distributed in the line with equal space around them.space-evenly
: items are distributed so that the spacing between any two items (and the space to the edges) is equal.justify-content
example Ispace-around
justify-content
example IIspace-between
justify-content
example IIIcenter
align-items
From CSS-Tricks:
This defines the default behavior for how flex items are laid out along the cross axis on the current line.
align-items
valuesFrom CSS-Tricks:
start
: cross-start margin edge of the items is placed on the cross-start lineend
: cross-end margin edge of the items is placed on the cross-end linecenter
: items are centered in the cross-axisbaseline
: items are aligned such as their baselines alignstretch (default)
: stretch to fill the container (still respect min-width/max-width)align-items: start
align-items: end
align-items: center
flex-wrap
and flex-flow
From CSS-Tricks:
flex-wrap
property.flex-wrap
values: nowrap
, wrap
, wrap-reverse
flex-flow
is a shorthand flex-direction
and flex-wrap
properties, which together define the flex container’s main and cross axes.row
nowrap
.align-content
From CSS-Tricks align-content:
This aligns a flex container’s lines within when there is extra space in the cross-axis, similar to how justify-content aligns individual items within the main-axis.
Note: this property has no effect when there is only one line of flex items.
order
and flex-grow
From CSS-Tricks:
order
: “By default, flex items are laid out in the source order. However, the order property controls the order in which they appear in the flex container.”
flex-grow
: “This defines the ability for a flex item to grow if necessary. It accepts a unitless value that serves as a proportion. It dictates what amount of the available space inside the flex container the item should take up.”
flex-shrink
and flex-basis
From CSS-Tricks:
flex-shrink
: This defines the ability for a flex item to shrink if necessary.flex-basis
: This defines the default size of an element before the remaining space is distributed. It can be a length (e.g. 20%, 5rem, etc.) or a keyword.align-self
From CSS-Tricks:
This allows the default alignment (or the one specified by align-items) to be overridden for individual flex items.
Note: The default alignment is the alignment set on the container.
<nav>
near top and <footer>
at bottom with extra space in betweenSee file VerticalTides.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Berkeley Tides</title>
</head>
<body>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="activities.html">Activities</a></li>
<li><a href="membership.html">Membership</a></li>
<li class="active"><a href="tides.html">Tides</a></li>
<li><a href="login.html">Login</a></li>
</ul>
</nav>
<main>
<header>
<h1>Berkeley Tides</h1>
</header>
<p>Information about tides in the Berkeley South sailing basin. To be furnished...</p>
</main>
<footer>
<p>🐳 © 2020 Grotto Networking! 🐬</p>
</footer>
</body>
</html>
body {flex-direction: column; align-items: center;}
body {/* previous stuff */ justify-content: space-between; height: 92vh;}
nav
to Leftnav {align-self: start;}
See file VerticalTides.css
/* Only those parts relevent to the flexbox demo */
body {
display:flex;
flex-direction:column;
align-items:center;
background-color:#c1e2ff;
margin: 1em;
justify-content: space-between; /* Vertical space between */
height: 92vh; /* Set height for vertical spacing */
}
nav {
align-self:start; /* over ride the containers "centering" */
background-color: white;
max-width: 10em;
padding: 0.1em;
border:aqua solid;
border-radius: 1em;
}