Responsive CSS

Tim Carlson
Spring 2024

View of the Day

  • Project Draft 1 overview

  • Q&A (pollev)

  • Flexbox Review (code together!)

  • CSS Review 

  • Media Queries Demo (code together!)

Project Draft 1

The complete HTML & CSS. A static "screenshot" of what your app will look like.

  • Include all of your "pages"!
    • Can include just a single example of a "details" page
    • May need to copy/paste shared elements (navbars) for now
  • Include all sections and components (forms, etc)
    • Skip user login forms; we'll provide that through a library
  • Be clear what the user will "do" with your app -- where do instructions/etc go?

Project Draft 1

Code requirements:

  • Well-written, semantic HTML
  • Significant CSS & styling
    • Must include a flexbox or grid (Bootstrap is okay)
  • Accessible
    • Check your alts and headings!
  • Responsive
    • Must include a meaningful media query!

Project Draft 1

Working as a group:

  • All group members are responsive for all aspects of the project. Projects are graded as a single piece of work.
    • Do not divide up work by language -- everyone needs to contribute to both HTML and CSS
    • Everyone needs to check everything. Don't think of it as "Megha's page" or "Henry's page". It is your group's project
  • Must use git to code collaboratively. We will be checking for commits from all group members.
    • Each individual needs to demonstrate understanding of the material.

Live-server

To Install: "npm install -g live-server"

To run: "live-server ."

  • Allows you to run a local web server on your box
  • Don't have to "refresh" to see changes you are coding
  • --open=<filename> argument opens html file other than 'index.html'.

Sample Code

Q&A

Which of the following are important when designing websites for mobile devices?

Layout: e.g., stacking elements rather than having side by side

Media: having small (or no) images to reduce loading times

Fonts: making text large enough to read

Navigation: e.g., using a "hamburger menu" to take less space

Inputs: making sure buttons/icons are large enough to tap

Content: including mobile-specific content (e.g., tel:links)

Mobile-first design involves...

1. maintaining functionality when moving to limited devices (graceful degradation)

2. adding functionality when moving to super-capable devices (progressive enhancement)

In a mobile-first approach, a site's design and style (read:CSS) should have:

1. "normal" styling for desktops/laptops; media queries for mobile

2. "normal" styling for mobiles; media queries for desktops/laptops

In a CSS file, media queries should be written:

1. at the top of the file

2. in the middle of the file

3. at the bottom of the file

True or False: A media query can contain multiple rules?

1. true

2. false

Demo: Media Queries

Example: Analytics Report from the app I work on (yesterday)

Notice the percentages of mobile/desktop/tablet

Example:  Analytics Reports from the app I work on (yesterday)

Notice broad use of browsers with high percent of chrome and safari

Example:  Analytics Reports from the app I work on (yesterday)

Demo: Media Queries

Media Queries

Mobile web browsers will do some work on their own to adjust to large web pages by shrinking content to fit

  • Page appears "zoomed out"
  • Users enlarge page to read content
  • It works, but it is not ideal

Updating Lecture Code

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

# download new starter code
git pull

# switch back to main branch for coding
git checkout main

# merge in new starter code (use default msg)
git merge starter --no-edit

# code and enjoy!

Get the starter code from the starter branch, but do all of your work on main.

Specifying Viewport

Specifying viewport size and scale keeps the browser from automatically adjusting the size of the screen

 

Must include the appropriate <meta> element in your HTML

Media Queries (CSS file)

Media Queries conditionally apply CSS rules much like an "if" statement

Applying a condition and rules (selectors and properties) that are applied when the condition is true.

If the width > 768px:

then apply these CSS Rules

You need to put full rules (including selector) inside the media query body

Media Queries (CSS file)

Last rule on page wins thus:

  • Media queries specify conditional rules that override more general rules

 

  • "Mobile first" would have general rules replaced with more features as screen size increases

 

 

Media Queries (CSS file)

Basic Recommended Structure:

  • General selectors at top of CSS file with default mobile rules.
  • Use media queries with increasing larger min-width values.

 

If CSS file is organized by section (header, body, footer). It's okay to modify:

  • Section can have General selectors for specific section
  • Media queries specific to section
  • Move down to next section

 

Responsive Flexbox

With CSS media query turns to flexbox

block items, stacked by default

Basic HTML

Will turn to flexbox when width gets larger than 768px

Responsive Flexbox

Could add additional conditions:

At 768px it wraps with 2 columns

 

At 1200px all items will stay on the row if their size is less than with width of the parent

Bootstraps Media Queries breakpoints

More Flexbox Examples

Any other cleanup we should do to this page?
 

Viewport <meta>

Include the viewport meta element to allow your code to control responsive appearances (rather than the browser).

<head>
  <meta charset="utf-8"> <!-- always need this -->
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- more head elements, including <link> ... -->
</head>

Media Queries

/* A Media Query */
@media (min-width: 768px) {
   /* these rules apply ONLY on screens 768px and wider */

   /* a normal CSS rule */
   body {
      font-size: 1.5em;
      background-color: beige;
   }
   
   /* another CSS rule */
   .mobile-call-icon {
      display: none; /* don't show on large displays */
   }
}

Action Items!

  • Read: through Chapter 9

  • Problem Set 03 due Thursday 4/11

  • Problem Set 04 due next week


Next: CSS Frameworks (Bootstrap)

info340sp24-css-responsive

By Tim Carlson

info340sp24-css-responsive

  • 91