CS 318: Lab 22 – Flexbox
Dear students,
Today we hit up the new Flexbox system of CSS. In short, Flexbox allows us to control the distribution, alignment, order, and expansion of a bunch children within their parent. It replaces many of the hacks of float
and Javascript to achieve certain layouts.
We’re going to dive right into the lab today.
Here’s your TODO list:
- Would you like another workday on Monday? Please be working in
prototype2
and have made progress sinceprototype1
.
See you next time!
Lab
Our goal today is to use Flexbox to create two layouts that would be either impossible or absurdly difficult to orchestrate without it.
First create a lab22
directory. Add a partners.html
and include your first names and last initials.
Tiles
In this first exercise, we’ll create a gallery of images all of different sizes but which nicely interlock. Like this:
Follow these steps to produce your gallery:
- Write all HTML in
tiles.html
and CSS intiles.css
. - Set all elements’
box-sizing
property toborder-box
. - Create a
div
with anid
ofgallery
. - Nest inside
div
a bunch ofimg
elements. Instead of downloading enough images to fill the screen, use Lorem Picsum. Read the directions on the site and set thesrc
attributes of yourimg
elements to load in images of width 200 and varying heights. Use a different height value for each image. - We’re done in the HTML. The rest is all CSS. Take a moment to appreciate that the HTML can be some simple and unconcerned with the layout.
- Have
gallery
fill the browser window usingfixed
positioning. Each edge ofgallery
should match up to the corresponding edge of the browser. - When
gallery
has more content than fits in the browser window, the content gets cut off. Setoverflow
so you can scroll through the excess content. - Round off the corners of each image.
- Add some shadowing behind each image.
- Add some margin around each image.
- To arrange the images within
gallery
, we want it to be a flex-container. Set itsdisplay
property toflex
. - We want the images to fill in columns. Set the
flex-direction
accordingly. Once a column is full, we want to wrap the images over into the next column. Setflex-wrap
accordingly. - The columns are probably ragged, meaning that they don’t all have the exact same height. We can fix this! A flex-item can be made to expand to fill its flex container. Set the
flex-grow
property on the images to some non-zero value. (Usually we set this with the shorthandflex
property.) If all the children of a flex-container have the sameflex-grow
, they will all expand at the same rate when there’s extra space. - The step you just completed scales up the images vertically, but not horizontally. Non-uniform scaling causes images to appear distorted. Set the
object-fit
property of the images tocover
, which renders the images so they fill their box completely without violating their aspect ratio. We did something similar with background images in a previous lab. - When an image is hovered, scale it up.
- Animate the scaling with a CSS animation.
Blog
In this second exercise, we’ll create a prototype of a standard blog layout that has three columns with a header and a footer:
This particular layout is sometimes called the holy grail—because a lot of sites have pursued it, and it has been hard to achieve with standard CSS. But then Flexbox came along.
Your blog will look something like this:
Follow these steps to produce your blog:
- Write all HTML in
blog.html
and CSS inblog.css
. - Set all elements’
box-sizing
property toborder-box
. - Create a
header
for the top, adiv
with anid
ofmiddle
for the middle row, andfooter
. I suggest you temporarily give them distinctive background colors so that you can see the structure of your layout as you tweak the flex properties. - Add a title in the header and a social media icon in the footer. Add padding as needed. But not margin. The background color doesn’t appear behind the margin.
- Zero out the margin on body to remove the gap around your page.
- We want to distribute out these three elements across
body
, so makebody
a flex-container. Have it layout its flex-items in a column. - To ensure that the footer always gets shoved to the bottom of the page, set the
min-height
ofbody
to100vh
. When you have little content, this will keep the footer from awkwardly rising up from the bottom of the page. - Have
middle
be the only element that expands to fill the extra space of the flex-container. Setflex-grow
to 1, while the header and footer remain at the default of 0. - In
middle
, add adiv
with anid
ofleft
, amain
, and adiv
with anid
ofright
. Give them each distinct background colors so that you can see what’s going on. Add some dummy content so the elements don’t collapse. - We want
middle
to distribute its child elements in Flexbox-ish way, so it too must be a flex-container. Set itsdisplay
andflex-direction
properties. - Only
main
should expand. Set theflex
properties accordingly. - As shown in the video, add some more reasonable blog-like content to
left
,main
, andright
. Center what’s inleft
. Box what’s inright
. Add fake posts inmain
. - When the page shrinks below a certain size, we want the layout to be entirely vertical. We can do this with just one property tweak. Switch the
flex-direction
fromrow
tocolumn
using a media query. - Ensure that the layout is still aesthetically pleasing even when the media query kicks in. The contents of
left
should be centered cleanly. The boxes inright
should expand to fill the page.
Publish and Validate
Commit and push your work. Verify that all pages have been uploaded by visiting https://USERNAME.github.io/cs318/lab22
in your browser.
Validate your pages with the W3C Validator. Paste in the URLs to your pages, one at a time, and address all concerns raised by the validator. Fix your changes on the local machine, commit, and push until the validator reports no errors or warnings.