Session 06 - CSS, Part 4 - including Grid

Harvard Extension School  
Fall 2024

Course Web Site: https://cscie12.dce.harvard.edu/

Topics

  1. CSS Grid
  2. Responsive Page Layouts
  3. Responsive Page Layouts
  4. Responsive Images
  5. CSS Custom Properties
  6. Other CSS expressions
  7. HTML5 Boilerplate

Session 06 - CSS, Part 4 - including Grid, slide1
Main topics, slide2
Contrasting CSS Grid and CSS Flex, slide3
CSS Grid, slide4
Grid Examples, slide5
Grid Examples, slide6
Grid - Lines and Areas, slide7
Grid Example, slide8
Grid Content Example - Solar System, slide9
Responsive Page Layouts, slide10
Classic Responsive Design Example, slide11
Breakpoints and CSS Media Queries, slide12
What about breakpoints?, slide13
Responsive Page Layouts, slide14
A Responsive Example, slide15
Responsive Images, slide16
CSS Custom Properties, slide17
Custom Properties, slide18
Other CSS expressions, slide19
HTML5 Boilerplate, slide20

Presentation contains 20 slides

Main topics

Contrasting CSS Grid and CSS Flex

Grid

Flex

CSS Grid

Grid Terms - Lines, Cells, Areas

Lines
grid line

Cells
grid cell

Areas
grid area


Column and Row Line Numbers
grid line numbers

Grid Examples

Grid Examples

Grid - Lines and Areas

grid

grid

Grid Example

Grid Content Example - Solar System

main div.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  column-gap: 2rem;
  row-gap: 2rem;
}

grid

Responsive Page Layouts

Site is "responsive" to different devices.

devices

Classic Responsive Design Example

responsive wide screen

responsive medium screen

responsive small screen

Breakpoints and CSS Media Queries

@media CSS Media Query


/* rules for small screens and all screens */
body {
  background-color: #005bbb;
  color: #ffd500;
  font-family: helvetica, sans-serif
}
h1 { text-align: center; font-size: 3rem;}
/* only for widths of 768px and wider
   yes, we could have used 48em as a unit
*/
@media (min-width: 768px) {
  /* specific rules go here */
  body {
    background-color: #ffd500;
    color: #005bbb;
  }
}

What about breakpoints?

With that said....

Breakpoints defined in certain approaches are:

Responsive Page Layouts

CSS media query:


/* "compact" (no query needed) */


/* "medium" screens */
@media (min-width: 600px) { /* specific rules go here */ }

/* "expanded" screens */
@media (min-width: 840px) { /* specific rules go here */ }

/* "large" screens */
@media (min-width: 1200px) { /* specific rules go here */ }

/* "extra large" screens */
@media (min-width: 1600px) { /* specific rules go here */ }

A Responsive Example

Small

small

Large

large

Responsive Images

Deliver the appropriately sized image using srcset and sizes attributes in the img element.

srcset and sizes

<img src="images/gnp.jpg"
alt="Glacier National Park"
srcset="images/gnp-600.jpg 600w,
images/gnp-900.jpg 900w,
images/gnp-1200.jpg 1200w,
images/gnp-1500.jpg 1500w"
sizes="(min-width: 1500px) 1400px,
       (min-width: 1200px) 1100px,
       (min-width: 900px) 800px,
       (min-width: 600px) 500px,
       900px"/>

Demos

CSS Custom Properties

The problem: keeping "DRY" (don't repeat yourself) in CSS can be hard impossible.

CSS Custom Properties provides a way forward!

section {
    --spacing: 1.2rem;
    padding: var(--spacing);
    margin-bottom: var(--spacing);
}

Steps:

Custom Properties

Properties cascade too -- so properties defined in "html" will be available throughout, but they can be overriden by redefining at another level (e.g. "footer")

Sometimes you see ":root" selector used for global custom properties, but that is essentially the same as "html"

See the Pen First @media query by David Heitmeyer (@dpheitmeyer) on CodePen.

Other CSS expressions

HTML5 Boilerplate

HTML5 Boilerplate -- this isn't a full-fledged endorsement to use this always for every project, but it is worth considering what's part of this boilerplate and why it is there. And, it very well might be directly helpful!

html5 boilerplate