Source: reddit.com/r/ProgrammerHumor
Moral of the story: CSS has a notorious reputation (mostly pre-flex), but it is important and you bring a lot of value to a full-stack team if you can figure out responsive layout (with good practices).
Meanwhile... you can now appreciate some CSS in-real-life: r/css_irl
By the end of this week, you should be able to:
How to cite images?
It's called Cascading Style Sheets because the properties of an element cascade together in this order:
<link>
tag)<style>
tag in the page header)style
attribute of an HTML element)Reminder: You should never use 3. or 4. to accomplish styles in your webpages for this course.
body { font-family: sans-serif; background-color: yellow; }
p { color: red; background-color: aqua; }
a { text-decoration: overline underline; }
h2 { font-weight: bold; text-align: center; }
A styled paragraph. Previous slides are available on the web site.
When multiple styles apply to an element, they are inherited
A more tightly-matching rule can override a more general inherited rule
Not all properties are inherited (notice link's color above)
body { color: green; }
p, h1, h2 { color: blue; font-style: italic; }
h2 { color: red; background-color: yellow; }
This paragraph uses the first style above
When two styles set conflicting values for the same property, the latter style takes precedence
A tree-shaped structure built out of all of the HTML elements in a page.
The browser uses the DOM defined by your HTML page to apply CSS properties and precedence.
Next week, we'll see how the DOM is used with JavaScript to attach behavior to different page elements (and to add/remove elements to the page dynamically!)
<html>
<head>
<title> ... </title>
</head>
<body>
<h1> ... </h1>
<div>
<p> ... </p>
</div>
</body>
</html>
The DOM is important to have as a model when thinking about layout. Many techniques rely on the idea of "parents" and "children" elements, and how they are laid out relative to another.
Visualizing the document tree can save you a lot of headache as you're writing CSS to achieve desired layout.
These are what we expect you to focus on, roughly in order of prioritization
Our toy page (starter HTML here):
<div id="boxes-container">
<div>0</div>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
Question: What does this look like on a webpage by default?
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 working with text inside of each div to additionally demonstrate the difference between block vs. inline layout.
In practice, it's useful to:
Solution (click source code for HTML/CSS):
Make the outermost container have a 5px border radius on all sides with 10px of spacing separating its top/bottom borders from the borders of the inner boxes.
Make boxes into a row instead of a column
One possible solution: "Float" the boxes left or right.
What happened when we just add float: left?
Hint: Use overflow: auto
to ensure container adjusts height if
contents "overflow" outside of the borders.
Exercise 5: 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
.
Exercise 6: Distribute boxes across box container evenly (equal space between each box).
... what should the margins be set to now?
display: flex
on the parent item, then:
flex-direction
if you want to override the default row
layout to column
, row-reverse
, or
column-reverse
justify-content
and
align-items
to distribute the child elements
flex-wrap
to wrap children cleanly in a
constrained-width parent container
flex-basis
on the items to specify how much space of the container each
thing should take up
Exercise 6: Distribute boxes across box container evenly (equal space between each box).
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?).
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)
How can we use these different layout methods in pages with components like header, main, footer? What about side-by-side sections? Inline navigations with lists?
You can use all of these strategies to accomplish page layout methods. For responsive design, it is best to prioritize default block/inline or flex layout. But there are some cases where we want to be specific with the position of our elements, such as when we have a fixed nav bar or footer.
display
PropertyThe display property specifies the display behavior (the type of rendering box) of an element. The four types you most often will see are:
inline
: Displays an element as an inline element.
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 whole width of a page. none
: The element is completely removed. flex
: Displays an element as a block-level flex container