1 of 39

Responsive Design

Layout, UI and UX

for all your users

Progressive Web Apps

Progressive Web Apps

2 of 39

What is responsive design

  1. Combination of
    1. Fluid grids
    2. Flexible images
    3. Media queries

Progressive Web Apps

3 of 39

Why “responsive design”?

    • Different users
    • Different devices
    • Different data needs

Progressive Web Apps

4 of 39

Progressive Enhancement

HTML

HTML

+

CSS

HTML

+

CSS

+

Javascript

If CSS supported

If JavaScript supported

Progressive Web Apps

5 of 39

Sizing elements matters

Progressive Web Apps

6 of 39

PWA golden rule

Never make users scroll horizontally

Progressive Web Apps

7 of 39

Viewport meta tag

<meta name="viewport" content="width=device-width, initial-scale=1">

Progressive Web Apps

8 of 39

max-width

img {

max-width: 100%;

}

img.foo {

width: 300px;

}

Progressive Web Apps

9 of 39

Relative sizing is not the (full) solution

One relative-sized layout for all

10%

80%

10%

Too big on desktop, too small on mobile

Progressive Web Apps

10 of 39

Enter media queries

Can use media queries to select layouts

Progressive Web Apps

11 of 39

Media queries example

/* elements in common for phones and tablets */

@media (max-width: 480px) {

/* layout for phones */

}

@media (max-width: 720px) {

/* layout for tablets */

}

Progressive Web Apps

12 of 39

Mobile-first design

Progressive Web Apps

13 of 39

Media Queries: Select layout by device

  • 320, 480px: phone
  • 768px: tablet
  • 992px: laptop
  • 1200px: widescreen

Progressive Web Apps

14 of 39

Revisiting responsive design

  • One stream of content
  • Find the best layout for devices and viewport sizes
  • Manipulate blocks of content for the needs of the layout

Progressive Web Apps

15 of 39

Match breakpoints to content

  • Start small
  • Add major breakpoints
  • Add minor breakpoints if necessary
  • Optimize for reading: 70–80 characters per line

Progressive Web Apps

16 of 39

Content-based media queries

  1. Understand your users
  2. Understand your target devices
  3. Design content
  4. Use media queries to select layouts

Progressive Web Apps

17 of 39

Mobile first media queries

/* small */

@media (min-width: 480px) {

/* medium */

}

@media (min-width: 720px) {

/* large */

}

Progressive Web Apps

18 of 39

You can also use JavaScript

var min = '(min-width: 25em)';

if (window.matchMedia(min).matches) {

/* include something */

}

Progressive Web Apps

19 of 39

...and don't forget calc()

img.thumb {

margin-right: 10px;

max-width: 400px;

width: calc((100% - 10px) / 2);

}

img.thumb: last-of-type {

margin-right: 0;

}

Progressive Web Apps

20 of 39

Content first

Progressive Web Apps

21 of 39

Manipulating content

  • Reorder
  • Reposition
  • Replace
  • Remove (last resort)

Progressive Web Apps

22 of 39

Keep it as simple as possible

Help users get to content

Progressive Web Apps

23 of 39

Content choice

  • Images, e.g. different crop or different image
  • Video, e.g. different resolution

Progressive Web Apps

24 of 39

Layout

Progressive Web Apps

25 of 39

The common way...

  • 12-column grid: Bootstrap/MDL/...
    • Fixed breakpoints and fixed sized per device family
    • CSS classes set block widths in columns
  • Pros: easy, well-understood
  • Cons: data usage, fixed widths
    • Works poorly on some devices, content

Progressive Web Apps

26 of 39

Resources

Resources

Progressive Web Apps

27 of 39

Lab Overview

  1. Set the visual viewport and style the page with CSS
  2. Add breakpoints to your content
  3. Use a grid layout
  4. Use Flexbox

Progressive Web Apps

28 of 39

Extra Credit: Flexbox

Progressive Web Apps

29 of 39

CSS Flexbox

  • Flexible sizing and alignment
  • Element reordering
  • Better performance than floats

Progressive Web Apps

30 of 39

CSS Flexbox: good support

Progressive Web Apps

31 of 39

CSS Flexbox: centering

.container {

display: flex;

height: 300px;

}

.child {

height: 100px;

margin: auto;

width: 100px;

}

Progressive Web Apps

32 of 39

Layouts for different viewports: 1

.container {

display: flex;

flex-flow: row wrap;

}

.child1, .child2, .child3, .child4, .child5 {

width: 100%;

}

Progressive Web Apps

33 of 39

Layouts for different viewports: 2

@media (min-width: 600px) {

.child2, .child3, .child4, .child5 {

width: 50%;

}

}

Progressive Web Apps

34 of 39

Layouts for different viewports: 3

@media (min-width: 800px) {

.container {

margin-left: auto;

margin-right: auto;

width: 800px;

}

.child1 { width: 60%; }

.child2 { width: 40%; }

.child3, .child4, .child5 { width: 33.33%; }

}

Progressive Web Apps

35 of 39

CSS Flexbox for ordering: 1

.container {

display: flex;

flex-flow: row wrap;

}

.child1, .child2, .child3 {

width: 100%;

}

Progressive Web Apps

36 of 39

CSS Flexbox for ordering: 2

@media (min-width: 600px) {

.child1 {

order: 2;

width: 60%;

}

.child2 {

order: 1;

width: 40%;

}

}

Progressive Web Apps

37 of 39

CSS Flex: more

  • flex-direction
  • justify-content
  • align-items

Progressive Web Apps

38 of 39

CSS Grid

  • Oriented to two-dimensional layout
  • Works with CSS Flexbox
  • Optimized for UI design
  • Separation of layout and content

Progressive Web Apps

39 of 39

CSS Grid: get ready

Progressive Web Apps