Code Monkey home page Code Monkey logo

css-responsive's Introduction

Responsive Web Design

Learning Objectives

  • Explain the difference between a responsive website and a mobile-specific website.
  • Compare mobile-first to desktop-first responsive design
  • Use media queries to adjust styles for different viewport sizes.
  • Identify and use relative units like em, rem, vh, vw, etc..
  • List the different media query values and their conditions.
  • Use the iOS simulator, Safari console, and Chrome Dev tools to debug responsive CSS.

Opening (5 min)

Ultimately we are trying to answer the question:

How do we build web applications and sites for an optimal interaction experience on a multitude of devices?

How we got here

In the beginning, there was no CSS. Every site looked nearly identical. As styling web pages became more common, developers began structuring their pages with table layouts and fixed-width CSS.

Many designers still ask

What are the most common dimensions for a website design?

You tell me! http://screensiz.es (5 min)

You do: Turn and Talk (10 min)

What makes a design fixed? What makes a design fluid? What makes a site responsive?

Check out mediaqueri.es for inspiration.

Mobile Specific Sites (5 min)

One way to create optimal experiences for mobile users is a dedicated mobile site.

You know you're on one when you see m. in the url!

Compare https://m.ups.com with https://ups.com

Avoid these... please.

The Three Components of Responsive Web Design (10 min)

  1. Flexible (or Fluid) Grids
  2. Flexible Images (or Media)
  3. Media Specific Layouts

Flexible Grids

A grid based layout which adjusts with screen size.

Coming soon to a lesson near you.

Flexible Images

We need images that fit their containers.
It turns max-width: 100% is the answer. Most of the time. For any media.

Even as our flexible container resizes, shrinking or enlarging our image, the image’s aspect ratio remains intact.

img,
embed,
object,
video {
  max-width: 100%;
}

Media Queries

One way to adjust the styles depending on the device's size is by using media queries:

body{
  background: papayawhip;
}

@media (max-width: 400px){
  body{
    background: tomato;
  }
}

Using media queries, we can group our css rules according to the size of our expected viewing devises. This media query says, if our viewport is less than 400px, use the following css rules.

The 400px corresponds to the device's viewport. A device's viewport is different from both its screen size and resolution. Check out this article if you're interested in why.

Other possible values include

/* these are most commonly used */
min-width | max-width | min-height | max-height

/* these are possible, but less common */
| width | height
| device-width | min-device-width | max-device-width
| device-height | min-device-height | max-device-height
| aspect-ratio | min-aspect-ratio | max-aspect-ratio | max-device-aspect-ratio
| device-aspect-ratio | min-device-aspect-ratio |
| color | min-color | max-color
| color-index | min-color-index | max-color-index
| monochrome | min-monochrome | max-monochrome
| resolution | min-resolution | max-resolution
| scan | grid

You do: Media Queries (15 min)

Step 1

Working with the example above, create a jsfiddle, codepen, or webpage that includes at least two media queries.

When the viewport is less than 800px wide, make the background yellow. When the viewport is less than 400px wide, make the background green.

Step 2

Try out a few of the properties above. You can combine media queries to get several different results.

i.e. what combination of media queries could produce the following grid as the viewport changes size?

green yellow red
turqouise green purple

Like this.

Media Query Logical Operators

You should have just seen these in your research.

Q. How do we "and" two media queries?

A. The "and" clause

@media (min-width: 700px) and (orientation: landscape) { ... }

Q. How do we "or" two media queries?

A. Comma separated List

@media (min-width: 700px), handheld and (orientation: landscape)

Q. What does the "not" keyword do?

A. Applies to the whole media query and returns true if the media query would otherwise return false (such as monochrome on a color display or a screen width of 600px with a min-width: 700px feature query). See more here

Mobile first vs Desktop first

Q. What is the difference between starting with the smallest viewport and applying styles as the viewport size increases and starting with the largest viewport size as the default?

A. See below.

Q. Do the order of the media queries matter?

A. Yes.

Q. Do media queries affect specificity?

A. No.

Read more

Break (10 min)

Relative units of measurement

So far, we've been working with pixels (absolute unit of measurement) and percentages (relative unit of measurements)

You do: Research the following units (30 min)

Form groups with your tables to research, discuss (15 min), and demo one of the following pair:

  • em and rem
  • vh and vw
  • vmin and vmax
  • ex and ch

You do: Convert the "Craigslist Grid" (15 min)

https://github.com/ga-dc/craigslist_grid

Break (10 min)

You do: 007 Exercise (20 min)

https://github.com/ga-dc/responsive_007

We do: Check it out w/ Chrome dev tools (15 min)

Be sure to include

<meta name="viewport" content="width=device-width">

Mobile Safari introduced the "viewport meta tag" to let web developers control the viewport's size and scale. Many other mobile browsers now support this tag, although it is not part of any web standard. This setting makes the width of the browser’s viewport equal to the width of the device’s screen.

Questions

  1. What are the three components of responsive web design?
  2. How do we "and" two media queries?
  3. What is the name of the design pattern where the initial designs support mobile, then we move to larger devices?

Cheatsheet:

  • Sizes, sizes, and more sizes
  • fluid media: img, embed, object, video { max-width: 1 };
  • @media (max-width: 400px) { ... }
  • min/max-width, min/max-height
  • And: @media (max-width: 400px) and (orientation: portrait) { ... }
  • Or (comma separated): @media (min-width: 700px), handheld and (orientation: landscape) { ... }

Resources

css-responsive's People

Contributors

jshawl avatar robertakarobin avatar

Watchers

James Cloos avatar

Forkers

oduolaakeem

css-responsive's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.