lecture04
lecture04
● Example 1: Click here for an example of poor HTML tags, layout, and accessibility (try resizing
the page). The “old days” of layout.
○ Inspect?: table, table, table, table…
● Example 2: This is the official HealthCare.gov webpage from 2018, where all kinds of users rely
on for health care information.
○ Some visually-impaired users need larger font sizes on the screen.
○ What happens to the search bar when you increase the font size?
● As a user, have you ever left a website (that may be useful) because of the layout or accessibility?
● There's a lot of very cool research on verifying page layout!
The Goal: Clean Layout, Responsive Design
Today, we'll learn the fundamentals of various layout techniques to go from the left initial
product (no layout CSS) to the final product (to the right, link TBD)
Starting with Building Blocks
Question: What does this look like on a webpage by default?
(Boxes)
<div id="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
Why are we playing with boxes?
When learning CSS layout, you'll find there are many ways to layout your pages.
"Boxes" are great to practice with for comparing different layout strategies and better
understanding the box model.
We are also working with text inside of each div to demonstrate block vs. inline layout.
Add overflow: auto; to make the parent of a floating element contain the floating
element.
And that’s a wrap on float :)
This is not an exhaustive introduction to float. There is SO much more to learn
about float as well as some other good use cases for float as well. However, our
focus for layout will be on the box model and using flex. If you’d like to learn more
about float post on Ed, ask in OHs or go to WPL.
Distributing Boxes Evenly in a Container
How could we distribute boxes across box container evenly (equal space between each box)?.
display: flex;
● makes an element a "flex container", items inside automatically become "items" - by default,
starts as a row
justify-content: flex-end; (flex-start, space-around,...)
● indicates how to space the items inside the container along the main axis
align-items: flex-end; (flex-start, center, baseline,...)
● indicates how to space the items inside the container along the cross axis
flex-direction: row; (column)
● indicates whether the container flows horizontally or vertically (default row)
flex-wrap: wrap; (no-wrap, ...)
● indicates whether the container's children should wrap on new lines
Basic properties for the flex container
There are also cases when you will need to add flex properties to flex items rather than the flex
container
flex-grow: <number>
● Defines a proportional value to determine whether a flex items can grow (what amount of
the available space inside the container it should take up).
flex-basis: 20%; (3em, 50px,...)
● indicates the default size of an element before the extra space is distributed among the
items
align-self: flex-end; (flex-start, center, stretch,...)
● indicates where to place this specific item along the cross axis
Back to the boxes
Exercise: distribute boxes across box container evenly (equal space between each box)
using flex.
solution
Exercise: Responsive Page Layout - Wrapping
Set boxes to wrap when box container gets too small in the browser so that they keep their
square widths (what happens when you shrink the browser width in the previous
exercise?).
solution
Extra Practice (for flex masters): more fancy stuff
solution
From Boxes to a “Real” Example
How can we use these different layout methods in pages with components like header,
main, footer? What about side-by-side sections? Inline navigation with lists?
What columns & rows exist in the cafe page?
What are the parent "containers" distributing items in ● body (column with 3 children)
a row/column? ● #top-bar header (row with 2
children)
● #item-container (row with 5
children)
● footer (column with 3 children)
<div id="item-container">
<article>...</article>
<article>...</article>
<article>...</article>
<article>...</article>
<article>...</article>
</div>
#item-container Solution
Third Flex Container: The #top-bar
<header id="top-bar">
<h1>...</h1>
<nav>...</nav>
</header>
● This is a bit of a trickier one, so it's good to do it last. We want to make it a flex row so we can get a
nice distribution of space between the h1 and the nav.
● We'll also make the #top-bar a sticky nav bar, so it sticks to the top when you scroll down!
● With careful planning, we can combine different layout techniques like display: flex; and position:
sticky.
● Let's take a look more closely at the CSS position property.
positioning Elements
position: static
● Default value. Elements render in order, as they appear in the document flow
position: fixed
● Puts an element at an exact position within the browser window
position: absolute
● Puts an element at an absolute position based on the location of the element's parent container
position: relative
● Makes children positioned relative to the parent container
● Handy for sticking a footer to the bottom of a page, for example
position: sticky
● A "hybrid" - toggles between relative and fixed depending on scroll position