Reminders
On the format for the quizzes
Questions?
More CSS and intro CSS Flex
CP1 due on Tuesday, locks on Thursday
Supplemental video about Friday lecture is up here
Section is where you go to get practice -- important to attend!
Keep asking questions in lecture
Administered through Gradescope
Will release after lecture on Wednesday and you'll have 24 hours to complete.
These are open-note, but not open internet.
5 points, 1 per question.
Covers the previous 1.5 week's worth of content (HTML and CSS). May reference that day's lecture content, but not in-depth.
Short answers, which will be capped at ~ 2 sentences.
Remember we will drop one of the five quizzes at the end of the quarter.
What is one reason it might be useful to associate multiple selectors with one rule in your CSS? What is one reason it might be a bad idea?
Associating multiple selectors with one style cuts down on code redundancy. If the style for those individual selectors diverges later, then a new rule will have to be created anyway.
A demo
Classification | Description | Example |
---|---|---|
Type Selector | Selects an element type | p |
Class Selector | Selects all elements with the given class attribute value | .koala-face |
ID Selector | Selects the element with the unique id attribute value | #scientific-name |
We can also combine selectors:
.bordered
selects all elements with the bordered class.h2.bordered
selects only h2 elements with the bordered class.How the browser represents the hierarchy of a page - very useful when thinking about selectors!
We'll return to this when we introduce JavaScript, where we can dynamically access/modify element "nodes" in the DOM tree.
For those of you interested in accessibility on the web, there is also a related concept known as Accessibility Tree
How to select the highlighted elements?
Using ID selector: #container
How to select the highlighted elements?
Using a class selector: .column
Using a combinator selector: #container > div
How to select the highlighted elements?
Using a class and descender combinator selector: .column div
Using a class and child combinator selector: #container > div > div
How to select the highlighted elements?
Grouping elements: #container, .column
An example of poor HTML tags, layout, and accessibility (try resizing the page). The "old days" of layout.
Inspect?: table,table,table,table, ....
This is the official HealthCare.gov webpage in 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!
Today, we'll learn the fundamentals of various layout techniques to go from...
These are what we expect you to focus on, roughly in order of prioritization
Question: What does this look like on a webpage by default?
<div id="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
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.
In practice, it's useful to:
A way to remove elements from the normal document element flow, usually to get other elements to "wrap" around them.
Can float left
, right
(no center
)
Question: How would we make the boxes into a row instead of a column?
One possible solution: "Float" the boxes left or right. Another solution: Set display: inline;
What happened when we just add float: left?
Hint: Use overflow: auto
to ensure container adjusts height if contents "overflow"
outside of the borders.
Extra Practice: Center the box container on the page and change width to 75%.
Can we float? Unfortunately, can't center float element cleanly.
Idea: Use block property that the container spans width of the page. But if the container is 75% of the page width... where does that extra spacing come from?
For block elements that have a specified width, you can center
them by setting left and right margins to auto
.
How could we distribute boxes across box container evenly (equal space between each box)?.
... what should we do about the margins of the boxes?
... what value do we put?
... how many screen sizes will we try on?
display
PropertyThe display property specifies the display behavior (the type of rendering box) of an element. The four values you most often will see are:
inline
: Displays an element as an inline element, spanning the width/height of its content.
Any height and width properties will have no effect.block
: Displays an element as a block element. It
starts on a new line, and takes up the width of the parent container.none
: The element is completely removed.flex
: Displays an element as a block-level flex container.grid
: Displays elements in a 2-dimensional grid.Flexbox is a set of CSS properties for aligning block level content.
Flexbox defines two types of content - "containers" and "items".
Anything directly nested inside of a flex container becomes a flex item.
Various properties on the container can be set to determine how its items are laid out.
display: flex;
flex-direction: row; (column)
justify-content: flex-end; (flex-start, space-around,...)
align-items: flex-end; (flex-start, center, baseline,...)
flex-wrap: wrap; (no-wrap, ...)
It is less common to add flex properties to flex items, but on occasion you will need to.
flex-grow: <number>
flex-basis: 20%; (3em, 50px,...)
align-self: flex-end; (flex-start, center, stretch,...)
Exercise: Distribute boxes across box container evenly (equal space between each box).
Use justify-content
to control flex item spacing on the main axis
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?).
Use flex-wrap
to control whether items should move to a new row or not
Layout boxes into two 3-box columns using flex (use screenshot with given details). Note: don't rely too much on previous CSS solutions - you'll need to change the HTML slightly as well to get the columns grouped)
#container
to 500px and center the columns
vertically#container.
Now you know enough about flex to go from...
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 are the parent "containers" distributing items in a row/column?
body
(column with 3 children)#top-bar
header (row with 2 children)#item-container
(row with 5 children)footer
(column with 3 children)We'll take an "outside-in" approach, starting with the body
Demo
For the body, we know we want a column.
We already get a column layout from the default block display
for header
, main
, footer
.
But by default, these elements will have a height defined by their contents. This will result in whitespace at the bottom of the page.
We can use flex to control the distribution of the body's children to fill the entire page!
Many page layouts desire a full viewport height (vh) with a footer at the bottom.
To set the body to be 100% of the view port height, use the vh size unit.
Next, we can use flex-grow: 1
on the child element of the body flex container to have that child fill any remaining whitespace
(the default for flex-grow for all items is 0). Let's make the main
child fill the rest of the whitespace of the parent.
#item-container
This is the div
that holds all of the product article
"cards".
It would be nice to have some control over their distribution, and wrap them when the screen gets smaller.
<div id="item-container">
<article>...</article>
<article>...</article>
<article>...</article>
<article>...</article>
<article>...</article>
</div>
HTML
What's type of flex container is #item-container
? (Demo)
#item-container
Solution<header id="top-bar">
<h1>...</h1>
<nav>...</nav>
</header>
HTML
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
adn 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 postion: sticky
.
Let's take a look more closely at the CSS position
property.
position: static
position: fixed
position: absolute
position: relative
position: sticky
Another good explanation is here
position: sticky
for a header/footerA sticky element toggles between relative and fixed depending on the scroll position - is fixed when a given offset position (e.g. top of 0) is met in the viewport
See the Pen Sticky Examples by Melissa Hovik (@mehovik) on CodePen.
Especially if you're watching a recording, write your question on PollEverywhere and I'll answer them at the start of next lecture.
Either on your phone or computer, go to PollEv.com/robcse