session five
agenda
- what is responsive design?
- the mobile viewport
- flexible grids
- media queries
- responsive images & text
- examples & exercises
- readings & resources
what is responsive design?
- at a basic level, responsive design is aimed at making websites look good on all devices
- not a technology - an approach that uses a set of best practices to create a website layout that suits the user's device
- sites should "respond" to the user's behaviour based on screen size, platform and orientation - content adapts to "fit" any device
- goal is to detect user's screen size and orientation and adjust the layout accordingly
- originally included three main components:
- Flexible, grid-based layouts
- Flexible images and media
- Media queries
- flexible grids and images allow the website to resize to the appropriate width and orientation of viewports
- media queries allow for different styles for specific browsers and devices
mobile viewport
- mobile devices have three viewport models to consider:
- layout viewport is how mobile devices render full-sized, non-responsive desktop styled layouts. It might 'fake' the actual width, e.g., a mobile browser that is actually 640px wide might act as 950px wide to show more of the page. It is advisable to override this behavior using the
meta
tag described below. - visual viewport is what the user can currently see e.g., if they zoom in or out, the visual viewport is changed
- ideal viewport is the optimal layout that should require no zooming to be usable. The particular width and height of the ideal viewport is different across mobile devices. Portrait vs. landscape orientation will also change the ideal viewport.
- layout viewport is how mobile devices render full-sized, non-responsive desktop styled layouts. It might 'fake' the actual width, e.g., a mobile browser that is actually 640px wide might act as 950px wide to show more of the page. It is advisable to override this behavior using the
- meta viewport tag
- add meta viewport tag to
.html
files to ensure your responsive designs use the ideal viewport, not the layout viewport. - place the following in the
head
section, before you apply your stylesheets:<!-- ensure ideal device width detection... -->
<meta name="viewport" content="width=device-width, initial-scale=1.0"><!-- ...before you add your stylesheets -->
<link rel="stylesheet" href="responsive_styles.css" media="all">
New and improved webpage template
- add meta viewport tag to
flexible grids
- early responsive design achieved flexible layouts using
float
- floated layouts gave each element a percentage width so that the total layout came to 100% - since it's impossible to target every device size, using a flexible grid allows you to add a breakpoint and change the design at the point where the content layout starts to breakdown, e.g.:
- if the line lengths become too long (unreadable) as the screen size increases
- a box is squashed and can only allow two or three words on each line as it gets narrower
- Ethan Marcotte described a formula to convert a fixed-width layout designed using pixels into a responsive layout using percentages:
target / context = result
- example: say you have a fixed-width layout where a column (target) is 80 pixels, and the container (context) holding it is 1024 pixels - we divide 80 by 1024 and move the decimal point two places to the right to get a value we can use in our CSS:
.col {
width: 7.81%; /* 80 / 1024 = 0.078125 */
} - this approach is still in use on many sites, and it is worth understanding it as you may come across it - but it has been surpassed by more modern approaches:
- all are responsive by default and give you easier ways to achieve flexible layouts
- walkthrough exercise: add flexible grid structure to the
01_fluid_layout_begin
files in thesession05
media queries
- CSS Media Queries are used to detect different client characteristics such as device width or orientation
- use @media declarations to detect the status of the client, then provide a suitably styled layout
- a basic media query can be used to apply styles based on the type of client. Use it to choose a stylesheet for
screen
orprint
@media TYPE { CSS rules go here }
Example:
/* styles applied to screen devices */
@media screen { body { color:#fff; } } - a compound media query is used to be more selective about which styles a client will receive based on its characteristics (e.g., desktop vs tablet vs smartphone)
- use the
and
conjunctive to chain together a series of conditional statements
@media TYPE and (FEATURE: VALUE) { CSS rules go here }
Examples:
/* client is 1025px wide or greater */
@media screen and (min-width: 1025px){ body {color:blue;} }/* client is between 801px and 1024px wide */
@media screen and (min-width: 801px) and (max-width: 1024px){ body {color:red;} }/* client is 800px wide or less */
@media screen and (max-width: 800px){ body {color:green;} } - use of logical conjunctive
and
ensures more than one condition must be true before the styles will apply - most media queries will involve one or more
and
, but there are other possibilities as well - learn more about the media query logic options
- your queries that reference widths or heights should use
and
to define both the upper and lower boundaries to ensure a more predictable cascade - options:
TYPE
: Target media type, usuallyscreen
andprint
. If you want older browsers to avoid using your responsive styles, you use only screenFEATURE
: There are a number of features that can be targeted, including:- width: width of the viewport
- height: height of the viewport
- min-width, max-width: minimum/maximum width of the viewport
- min-height, max-height: minimum/maximum height of the viewport
- device-width: width of the device
- device-height: height of the device
- orientation:
landscape
orportrait
VALUE
: the corresponding value such as size in pixels or other specifications likelandscape
- implementation:
- when a page is initially rendered, the user agent (browser) assesses the size of the viewport and selects CSS based on any media query declarations, and continues to adjust if/as the viewport is resized
- embedded queries: media queries can be specified in the
link
tag itself:<link rel="stylesheet" href="smaller.css" media="screen and (max-width: 1025px)">
<link rel="stylesheet" href="larger.css" media="screen and (min-width: 1024px)">
<link rel="stylesheet" href="printer.css" media="print"> - external queries: defined in an external CSS file, and linked to like any other stylesheet:
/* client is 1024px or less */
@media screen and (max-width: 1024px){
body { font-size: .8em; }
}/* client is wider than 1024px */
@media screen and (min-width: 1024px){
body { font-size: 2em; }
}/* styles for printers */
@media print {
header { display:none; }
} - if your
.css
contains media queries for bothscreen
andprint
, ensure the<link>
tag usesmedia="all"
- media queries demo
- walkthrough exercise: update the
02_media_query_demo_begin
css file with additional media queries to achieve the effects in the file linked above - walkthrough exercise: add media queries to the previous
01_fluid_layout_begin
css file
responsive images & text
- essential idea is that images will scale to suit the viewport
- original simple technique was to set
max-width: 100%;
onimg
elements in your CSS - allows images to scale down smaller if containing column is narrower than the image's native size, but keep it from growing larger
- downsides:
- bandwidth: image can be displayed much smaller than original size, wasting bandwidth e.g., a mobile user would need to download an image of much greater filesize than warranted for what is displayed in the window
- aspect ratio: no control over image aspect ratio - you may want different orientation (landscape, portrait, square) depending on viewport
- responsive images using
<picture>
and the<img> srcset
andsizes
attributes allow you provide multiple images for a client to choose from - each
picture
tag requires one or moresource
tags and its last child must be animg
- advantages:
- send lower bandwidth images for smaller clients
- reframe images suitably for different sized devices
- attributes for the
source
tag:srcset
- the path to the imagemedia
- a media query that determines if the image is usedtype
- define the MIME type of the image; if a client does not support the type, the image will not be used
- responsive images demo
- font sizing techniques
lab exercise: creating a responsive flexbox layout
- From the
session05
folder, open the06_responsive_flex_begin
folder in your HTML editor. - Open the
responsive-flex-layout.html
file and add the<meta>
element to ensure ideal device width detection (see notes above). Make sure you add this before the style sheet. - Review the rest of the file to familiarize yourself with the structure, e.g., IDs, classes, nested structure, etc.
- Open the
responsive-flexbox.css
file and review the instructions at the top of the page. - Add CSS to create flex containers as described.
- Update the CSS rules for flex items to distribute content as outlined.
- Review the instructions at the bottom of the page and add a media query that stacks the content of the page into one column, with the sidebars appearing after the main content, when the client is less than 600px wide.
- Upload the finished page and style sheet to your webspace and send a link to your instructor via chat/email.
readings & resources
- Responsive data tables
- Responsive images
- CSS Layout: Responsive design
- CSS Tricks: Viewport sized typography
- The Beginner’s Guide to Responsive Web Design
- Beginner's guide to Media Queries
- Using Media Queries
- CSS Selector Reference
- CSS Layout Templates
- W3.CSS templates
- CSS Layout Templates From CodePen