CSS Properties & Flexbox

Joel Ross
Autumn 2022

View of the Day

  • Q & A (PollEv)

  • Quick Fonts Demo (code together!)

  • CSS Layouts (PollEv)

  • Flexbox Demo (code together!)

  • CSS Class Naming [if time]

START WORKING ON
PROBLEM SET 03 EARLY!

It's a bigger problem set (3 problems!) and will take you longer than the previous one. Don't put it off and fall behind! Start tonight!

Q&A / "Review" Poll

How to pick a selector

Some heuristics

  1. Main rule: be as general as you can, but as specific as you need to be
  2. "Will this rule apply to all tags of this type?" if not, add a meaningful class to the element
  3. Are the elements in this part of the page different from others? Use a descendant selector
  4. Do you want to "call out" an element with style? Add a class
  5. Rules usually don't have more than 2-3 selectors
  6. Primarily use class names for styling, not id attributes
  7. You never need to add more specificity to an id selector

Updating Lecture Code

# switch to main branch to get new starter code
git checkout main

# download new starter code
git pull --no-edit

# switch back to section branch for coding
# (section-a or section-b)
git checkout section-X

# merge in new starter code
git merge main --no-edit

# code and enjoy!

Do all your work on the "section" branch, using the main branch only for starter code.

It is also possible to specify an online font that the browser will download and display.

Fonts

Fonts are installed "per computer", so not every computer has the same fonts (which is why you set a default with font-family).

Fonts are installed "per computer", so not every computer has the same fonts (which is why you set a default with font-family).

p {
  font-family: 'Helvetica', 'Arial', sans-serif;
}

Use this font

If first isn't available,
use this

If nothing else,
use this style of font-face

Font Units

All browsers have a "default" font size--generally 16px. We use relative font sizing in case of variations.

Note that units are "measurement units" (think: inches)

em relative to the parent font size By default 2em = 32px
But if the parent's font-size was 20px, then 2em = 40px
rem relative to the root (body's) font size of 16px 2rem = 32px usually
% relative to parent font size or dimension, can use for width or height if parent width is 300px,
then 50% = 150px
px absolute measurement (do not use for fonts) 16px = 16px

Inline vs. Block

You can use the display property to set whether an element is block or inline (or something else).
Choose elements based on semantics, not appearance!

AVOID USING
float AND position PROPERTIES!

Box Model

Apply spacing to elements by manipulating the size of their "box".

Demo: Flexbox

CSS Class Names

There are only two hard problems in computer science: cache invalidation and naming things - Phil Karlton

CSS Class Names

Goals when naming classes (or anything):

  1. Understandability: explain what kind of styling is being applied
     

  2. Modifiability: make it easier to change styling later

Semantic Class Names

Name CSS classes based on the semantic meaning (purpose) of the element they are styling.

<div class="forum-post">...</div>

<nav class="side-nav">...</div>

<img class="avatar-icon">...</div>

<article class="breaking-news">...</article>
/* can use descendant selectors for more detail */
.forum-post img { ... }

.side-nav ul a { ... }

Modular Class Names

Name CSS classes based on the (single) styling they apply. Combine multiple classes to style elements.

<div class="font-large text-red bg-secondary">...</div>

<img class="small rounded shadow">...</div>
.font-large { 
  font-size: 2em; 
  line-height: 1.4em;
}
.bg-secondary { background: #bbb; }

img.small { width: 140px; }
.rounded { border-radius: 50%; }

A Naming Schema: BEM

<div class="block__element--modifier">
<form class="form form--theme-xmas form--simple">
  <input class="form__input" type="text">
  <input 
     class="form__submit form__submit--disabled" 
     type="submit" />
</form>

The "part" of the page
navbar

An element in that block
tab

Flags or types; differentiators
selected

<div class="navbar">
  <div class="navbar__tab">A tab</div>
  <div class="navbar__tab--selected">Selected Tab</div>
</div>

Action Items!

  • Read: through Chapter 8

  • Problem Set 03 due Friday

    • ​​​GET STARTED TONIGHT!

Next: Responsive Design & Media Queries

Lab: project design work - small grade item for participating!

info340au22-css-flexbox

By Joel Ross

info340au22-css-flexbox

  • 443