Web Development

Dr Derek Bridge
School of Computer Science & Information Technology
University College Cork

Lecture Objectives

  • learn the principles of responsive layout
  • learn how to use CSS media queries for responsive layouts
  • learn to solve the problems caused by images in responsive layouts

Responsive Web Design (RWD)

In RWD, we adapt the layout to the characteristics of the device.

Responsive Web Design

  • A flexible (liquid) layout.;
  • Flexible images and media;
  • Media queries;
  • Least-capable-clients-first ('mobile-first'); and
  • Progressive enhancement.

Responsive Components

Some of the things we have seen so far are already helpful for RWD.

Liquid layouts, using relative units or percentages.
Flexbox, assuming, e.g. we allow items to wrap and to flex.
Grid layout, if we are not too prescriptive.
Multicolumn text.

When these do not suffice, then use media queries.

CSS Media Queries

Media Queries


@media screen and (min-width: 800px) {
    …
}
    

Simple media queries comprise:
the media type: e.g. screen
an expression containing a media feature (e.g. min-width) and a value (e.g. 800px) in parentheses; and
some CSS between curly braces.

A list of the media features that you can use.

Media Queries for RWD

RWD uses media queries to adapt to a range of devices.

E.g.
a single-column layout for narrower viewports;
but one or more multi-column layouts for wider viewports.

Least-capable-clients-first and Progressive Enhancement

  1. Write nice HTML:
    • proper use of HTML markup;
    • with content in a logical order; and
    • validate it.
    …for screen readers, search engine crawlers, etc.
  2. Write some core CSS:
    • something that looks OK on all devices,
    • e.g. single-column layout for the narrowest reasonable viewport widths.
  3. Write successive media queries that apply extra styles for relevant breakpoints:
    • e.g. multi-column when viewport min-width allows it;
    • e.g. even ultimately a fixed-width design for very wide viewports.

Simple RWD Example

  • In narrow-screen devices, we will use a one-column layout.
  • In wider devices, we will use a two-column layout.
One-column layout, with header, nav, main and footer stacked Two column layout, with nav and main side-by-side

Simple RWD Example: CSS


body {
    margin: 0;
}
        
@media screen and (min-width: 600px) {
        
    body {
        width: 80%;
        margin: 0 auto;
        display: grid;
        grid-template-columns: 1fr 3fr;
        grid-template-areas: "top top"
                             "middle-left middle-right"
                             "bottom bottom"
    }

    header {
        grid-area: top;
    }

    nav {
        grid-area: middle-left;
    }

    main {
        grid-area: middle-right;
    }

    footer {
        grid-area: bottom;
    }

}
    

Common Breakpoints

320px For small screen devices, like phones, held in portrait mode
480px For small screen devices, like phones, held in landscape mode
600px Smaller tablets held in portrait mode
768px Ten-inch tablets held in portrait mode
1024px Ten-inch tablets held in landscape mode, as well as some laptop, netbook and desktop displays
1200px For widescreen displays, primarily laptop and desktop browsers

Problem!
there is a much wider range of devices and viewports than this table assumes;
the table is not future-proof.

A Problem

Many small-screen devices draw a distinction between the layout viewport and the visual viewport.

The visual viewport of a phone may be much narrower than the layout viewport ued for rendering a web page.

Media queries use the layout viewport and so might not work as expected.

A Fix

To make RWD work on these devices, we need to make the two viewports of equal size.

Either add the following inside the head element of your HTML:


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

Or add the following to your CSS:


@viewport {
      width: device-width;
}
    

Or do both!

Foreground Images

We use HTML to include a foreground image, e.g.


<img src="wombat.jpg"
     alt="Wombats are furry and walk on all fours."
     title="A wombat walking along a road" />
    

There are also width and height attributes
—see below.

You can also nest the img element inside a figure,
with an optional figcaption.

Images in Fixed-width Layouts

In fixed-width layouts, you know how much space (in pixels) is available for your images.

So the image you put on your server should be neither smaller nor larger than needed.

Images in Fixed-width Layouts

Less satisfactory is to put on your server an image that is not the right size and let the browser scale the image by specifying the desired size…

…either in the HTML, e.g.


<img src="wombat.jpg" width="500" alt="..." />
    

…or in the CSS, e.g.


img {
    width: 500px;
}

Images for Liquid Layouts

In liquid layouts, you don't know how much space is available for your images.

If the image is wider than the available space, it will overflow its container.

In liquid layouts, we will have to allow the browser to scale the image.

Images for Liquid Layouts

Responsive Web Design recommends we set the max-width to 100% in the CSS:


img {
    max-width: 100%;
}
    

If the image is narrower than its container, it will display at its normal size.
If it is wider than its container, it will be scaled down to match the width of the container.

Or you could set the width to 100%:


img {
    width: 100%;
}
    

Then the image scales up or down to match the container.

But There Are Still Problems

resolution switching problem
you want to serve smaller image files to narrow screen devices, and also optionally you want to serve different resolution images to high density/low density screens
art direction problem
you want to serve different images for different layouts — for example a wide image showing a full scene for a desktop layout, and a narrower, cropped image showing a close-up for a mobile layout

Resolution Switching

HTML solves this using the srcset and sizes attributes, e.g.:


<img
     srcset="wombat-small.jpg 480w,
             wombat-large.jpg 800w"
     sizes="(max-width: 600px) 480px,
            800px"
     src="wombat-large.jpg"
     alt="..." />
    

<img 
     srcset="wombat.jpg,
             wombat-hi-res.jpg 1.5x"
     src="wombat-hi-res.jpg"
     alt="..." />
    

The Art Direction Problem

HTML solves this using the picture and source elements, e.g.


<picture>
    <source media="(max-width: 799px)" srcset="wombat-cropped.jpg" />
    <source media="(min-width: 800px)" srcset="wombat.jpg" />
    <img src="wombat.jpg" alt="..." />
</picture>
    

G'luck!