Viewports. Peter-Paul Koch CSS Day, 4 June 2014

Similar documents
Viewports. Peter-Paul Koch DevReach, 13 November 2017

A pixel is not a pixel. Peter-Paul Koch BlackBerry Jam, 6 February 2012

A pixel is not a pixel. Peter-Paul Koch SF HTML5, 6 April 2012

A pixel is not a pixel. Peter-Paul Koch MoBeers, 10 April 2012

Responsive Design. Responsive Design Approach. Technique for building webpages

Responsive Design. Responsive design is it important? But typical is not enough. Some typical screen sizes

The plural of Chromium is Chromia. Peter-Paul Koch Mobilism, 27 March 2015

CS7026 Media Queries. Different Screen Size Different Design

8/19/2018. Web Development & Design Foundations with HTML5. Learning Objectives (1 of 2) More on Relative Linking. Learning Objectives (2 of 2)

The Benefits of CSS. Less work: Change look of the whole site with one edit

Building Responsive Layouts

Responsive Web Design. From: Ethan Marcotte - Responsive Web Design 2011

Responsive web design (RWD) CSS3 Media queries. Mobile vs desktop web sites. Web Development 1 CS1115/CS5002

The plural of Chromium is Chromia. Peter-Paul Koch NLHTML5 Rotterdam, 19 February 2015

CSS3, Media Queries, & Responsive Design. May 23, 2012 STC Summit Zoe Mickley

Adaptations by PVII responsive and then creates your page instantly Al Sparber & Gerry Jacobsen PVII

Responsive Design for Web Applications R.I.T. S. Ludi/R. Kuehl p. 1 R I T. Software Engineering

Class 3 Page 1. Using DW tools to learn CSS. Intro to Web Design using Dreamweaver (VBUS 010) Instructor: Robert Lee

ORB Education Quality Teaching Resources

International Conference KNOWLEDGE-BASED ORGANIZATION Vol. XXIII No RESPONSIVE WEB DESIGN TECHNIQUES. Luminita GIURGIU, Ilie GLIGOREA

Designing for the Mobile Web

<layer> or the ingrained habits of web development. Peter-Paul Koch HTML Special, 16 June 2016

door to my garden Simple CSS resulting in impressive, sophisticated visual effects

The Marketer s Guide to. Responsive Design

CSS3: Using media queries to improve the web site experience. November 19, 2011 indieconf Zoe Mickley

Introduction to Computer Science Web Development

Web App Vs Web Site. Some tricks of the trade. Laurent Technical Director, BlackBerry Web Platform

The Importance of the CSS Box Model

Styles, Style Sheets, the Box Model and Liquid Layout

Reading 2.2 Cascading Style Sheets

Chapter 7 BMIS335 Web Design & Development

Perfect Student Midterm Exam March 20, 2007 Student ID: 9999 Exam: 7434 CS-081/Vickery Page 1 of 5

TODAY S AGENDA. An introduction for Libraries UW-SLIS CES Webinar April 6, 2015 What is Responsive Web Design? Definition & History

CS7026 CSS3. CSS3 Graphics Effects

CIT BY: HEIDI SPACKMAN

Contents FORMAT 3. Specifications STATIC ADVERTISING 4. Interstitial HTML5 ADVERTISING 5-12

Web Site Design and Development Lecture 13

Positioning in CSS: There are 5 different ways we can set our position:

Developing Web Applications for Smartphones with IBM WebSphere Portlet Factory 7.0

Magazine-style websites often have lots of small items on a page. First you re going to create a heading and background for your magazine.

Welcome Back! Without further delay, let s get started! First Things First. If you haven t done it already, download Turbo Lister from ebay.

Designing with CSS3 Effectively & Efficiently. October 2010 Adobe MAX Zoe Mickley

PAGE LAYOUT CHAPTER X. In this session you will:

1.7 Limit of a Function

Development Training. For Developer. 4 th May, AOT Intranet Web Portal

Web Design and Implementation

AGENDA. :: Add Meta Tag for Viewport (inside of head element): :: Add Script that renders HTML5 elements for older versions of Internet Explorer

Cascading Style Sheets for layout II CS7026

TAG STYLE SELECTORS. div Think of this as a box that contains things, such as text or images. It can also just be a

What do we mean by layouts?

Session 3.1 Objectives Review the history and concepts of CSS Explore inline styles, embedded styles, and external style sheets Understand style

B r o w s e r s u p p o r t

FRONT END DEVELOPER CAREER BLUEPRINT

epromo Guidelines DUE DATES NOT ALLOWED PLAIN TEXT VERSION

The mobile browser world. Peter-Paul Koch Front-end Day workshop, 26 April 2011

Responsive Web Design (RWD)

CS7026 Media Queries II. Different Screen Size Different Design

Internet Programming 1 ITG 212 / A

Here are some links to get you started in learning more about CSS3 media queries and responsive web design:

BORDER IMAGES THE BACKGROUND IMAGE PROPERTIES. Learning Web Design, 5e

Reader Release Notes. January 7, Release version: 3.1

Table Basics. The structure of an table

16A CSS LAYOUT WITH FLEXBOX

Touring the Mac S e s s i o n 4 : S A V E, P R I N T, C L O S E & Q U I T

Content Author's Reference and Cookbook

How to lay out a web page with CSS

Guidelines for doing the short exercises

Understanding Browsers

CSS worksheet. JMC 105 Drake University

Mobile & More: Preparing for the Latest Design Trends

Developing for touch. Peter-Paul Koch Mobilism, 16 and 17 May 2013

Cognos. Active Reports Development. Presented by: Craig Randell

How to deploy for multiple screens

AGENDA :: MULTIMEDIA TOOLS :: CLASS NOTES. INSTALLING FONTS :: Download, Install, & Embed Your Fonts. LECTURE :: Responsive Web Design [RWD]

WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5

Responsive Web Design and Bootstrap MIS Konstantin Bauman. Department of MIS Fox School of Business Temple University

Advanced Dreamweaver CS6

Visualforce & Lightning Experience

Header. Article. Footer

Web Site Design Small Screen Design Responsive Design R.I.T. S. Ludi/R. Kuehl p. 1 R I T. Software Engineering

Navigation. Websites need a formalized system of links to allow users to navigate the site

Standard Design Reference

The purpose of Veco Mobile is to give remote workers, such as negotiators and property management/maintenance staff real-time access to Veco data.

Comm 244 Week 3. Navigation. Navigation. Websites need a formalized system of links to allow users to navigate the site

AGENT123. Full Q&A and Tutorials Table of Contents. Website IDX Agent Gallery Step-by-Step Tutorials

RC Justified Gallery User guide for version 3.2.X. Last modified: 06/09/2016

Using Dreamweaver CC. Logo. 4 Creating a Template. Page Heading. Page content in this area. About Us Gallery Ordering Contact Us Links

AGENDA. EMBEDDING FONTS [ Font Files & CSS font-family ] :: Online Font Converter :: ADD font-family css code to style.css

WEBSITE STYLE GUIDE JULY 31, 2015 VERSION 1.0 PREPARED BY

Assignments (4) Assessment as per Schedule (2)

HTML5: Adding Style. Styling Differences. HTML5: Adding Style Nancy Gill

Dreamweaver CS4. Introduction. References :

CSS. Selectors & Measurments. Copyright DevelopIntelligence LLC

How mobile is changing and what publishers need to do about it

MindManager HTML5 Export Release Notes

HTML/XML. HTML Continued Introduction to CSS

HTML and CSS a further introduction

What is the Box Model?

Responsive Web Design Discover, Consider, Decide

CSS: Responsive Design, CSS3 and Fallbacks

Transcription:

Viewports Peter-Paul Koch http://quirksmode.org http://twitter.com/ppk CSS Day, 4 June 2014

or: Why responsive design works Peter-Paul Koch http://quirksmode.org http://twitter.com/ppk CSS Day, 4 June 2014

1 Pixels

A pixel is not a pixel CSS pixels Device pixels You already know what they are. You just don t realise it.

CSS pixels CSS pixels are the ones we use in declarations such as width: 190px or padding-left: 20px They are an abstract construct Their size increases or decreases when the user zooms

Device pixels Device pixels are the physical pixels on the device There s a fixed amount of them that depends on the device

Device pixels

Device pixels

What kind of pixels? In general, almost all pixels you use in your code will be CSS pixels. The only exception is screen.width but screen.width is a serious problem that we ll study later

2 Viewports

Viewports The 34% is calculated relative to its container: the <body>. Every block-level element, including <html> and <body>, has an implicit width: 100%. So we get 34% of the <body> width of 100%. 100% of what? Of the <html> width, which is again 100%.

Viewports The <html> element s width is calculated relative to the viewport. Also called the initial containing block. On desktop it s equal to the browser window width. On mobile it s more complicated.

Viewports When you zoom in, you enlarge the CSS pixels and as a result less of them fit on the browser screen Thus the viewport becomes smaller

Viewports On mobile it s quite a bit more complicated Mobile browsers must render all sites correctly, even if they haven t been mobileoptimized If the (narrow) browser window were to be the viewport, many sites would be squeezed to death

Viewports That s why the mobile browser vendors changed the rules: By default, the viewport is 768-1024px wide (depending on the browser), with 980px the most common size We call this the layout viewport Responsive design is the art of overriding the default width of the layout viewport

Viewports But this layout viewport is now much wider than the mobile screen Therefore we need a separate viewport for the actual window width We call this the visual viewport

JavaScript - layout viewport document.documentelement.clientwidth document.documentelement.clientheight Works (almost) everywhere.

JavaScript - visual viewport window.innerwidth window.innerheight Doesn t work in Android 2, Opera Mini, and UC 8.

Viewports So the desktop viewport has been split into two: layout viewport visual viewport

Viewports So the desktop viewport has been split into two: layout viewport! visual viewport! But! there s a third mobile viewport that has no equivalent on the desktop:! ideal viewport

Ideal viewport What mobile browser vendors want is to give the site the perfect width for the device so that zooming and panning are not necessary and the user can read the text Enter the ideal viewport, which has the ideal size for the device Essentially a width and a height

Ideal viewport There are no wrong dimensions for the ideal viewport. They re what they need to be for the device they run on. (Admittedly, there are weird values. But they re not wrong.)

Ideal viewport: 320px

Ideal viewport: 320px

JavaScript - ideal viewport screen.width screen.height UNRELIABLE! Some browsers define screen.width and screen.height as the dimensions of the ideal viewport while others define them as the number of device pixels

3 Meta viewport

Meta viewport In order to create a responsive design we must set the layout viewport dimensions to the ideal viewport dimensions. How?

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

Meta viewport <meta name= viewport content= width = device-width > By default, the layout viewport is between 768 and 1024 pixels wide. The meta viewport tag sets the width of the layout viewport to a value of your choice. You can use a pixel value (width=400) or you can use the device-width keyword to set it to the ideal viewport

Meta viewport <meta name= viewport content= width = device-width > I m assuming this does not come as a surprise But did you know that the following does exactly the same?

Meta viewport <meta name= viewport content= initial-scale = 1 > In theory, initial-scale gives the initial zoom level (where 1 = 100%) 100% of WHAT? Of the ideal viewport In practice, it also sets the layout viewport dimensions to the ideal viewport

Meta viewport <meta name= viewport content= initial-scale = 2 > In theory, initial-scale = 2 tells the browser to zoom in to 200%. It does so, but many browsers set the layout viewport to half the ideal viewport. Why half? Because zooming to 200% means that only half as many CSS pixels fit the visual viewport

Meta viewport <meta name= viewport content= initial-scale = 1 > And yes, this is weird. I wonder what Apple was smoking when it set these rules. I want some.

Let s mess things up

Meta viewport <meta name= viewport content= width = 400,initial-scale = 1 > Now the browser gets conflicting orders. Set the layout viewport width to 400px. No, wait, set it to the ideal viewport width (and also set the zoom to 100%). Browsers react by taking the highest value

Min-width viewport <meta name= viewport content= width = 400,initial-scale = 1 > Set the layout viewport width to either 400px, or the ideal viewport width, whichever is larger If the device orientation changes, this is recalculated. As a result, the layout viewport now has a minimum width of 400. Is this useful? Dunno.

Safari workaround <meta name= viewport content= width = device-width > Safari always takes the portrait width (320 on iphone, 768 on ipad). Sometimes this is what you want; at other times it isn t. How to solve this?

Safari workaround <meta name= viewport content= initial-scale = 1 > Now Safari does it right. In portrait mode it s the ideal portrait width; in landscape mode it s the ideal landscape width. All other browsers do the same. Except for IE10, which has exactly the opposite bug.

Safari workaround <meta name= viewport content= width=device-width,initial-scale=1 > Use both device-width and initial-scale. initial-scale works in Safari device-width works in IE10 and both work in all other browsers

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

@viewport @viewport { width: device-width; } Opera s idea initially Only IE for now, and prefixed at that.

@viewport @-ms-viewport { width: device-width; } Gives you the true ideal viewport. The tag gives you 320px (because iphone) @viewport overrides tag

Even perfecter viewport @-ms-viewport { width: device-width; } <meta name= viewport content= width = device-width, initial-scale = 1 >

4 Media queries

Media queries @media all and (max-width: 600px) {.sidebar { float: none; } }

Media queries There are two important media queries: width (min-width and max-width) device-width (min-device-width and maxdevice-width) width is the one you want

Media queries - device-width device-width media query is always equal to screen.width but the problem is screen.width may have two meanings, depending on the browser: 1) ideal viewport 2) number of device pixels

Media queries - width width gives the width of the layout viewport This is what you want to know Works always and everywhere

Responsive design Set the layout viewport width to the ideal viewport width (or, rarely, another value) Use the width media query to figure out how wide the layout viewport is Adjust your CSS to the width you found That s how responsive design works. You already knew that, but now you understand why it works.

Media queries Always use min- or max-width. Thus you define a breakpoint: these styles are valid for all widths equal to or less/ greater than X Exact widths, such as 320, are going to misfire in a lot of browsers. (Remember the 342px of the Z10.) There s more than just the iphone.

Responsive design < m e t a n a m e = v i e w p o r t c o n t e n t = w i d t h = d e v i c e - w i d t h, i n i t i a l - s c a l e = 1 >! @media all and (max-width: 600px) {! }

Responsive design But we d like to make our design respond to the physical width of the device, too. For instance, by setting a min-width: 25mm on our navigation items Tough luck You can t

5 CSS units

CSS units width: 25mm does not mean the element is 25 real-life millimeters wide Instead, it means 94.488 pixels cm, mm, and in are in a sense fake units, because they do not correspond to the real world

CSS units 1 inch is defined as 96 CSS pixels If you zoom, the CSS pixels become larger, and your inches become larger, too. It has nothing to do with the real world.

CSS units 1 inch is defined as 96 CSS pixels 1 inch is defined as 2.54 cm 1 cm is defined as 10 mm 1 inch is defined as 72 points 1 pica is defined as 12 points

CSS units I used to think this is a bad idea But I changed my mind If an element would have a width of 25 real-world millimeters the browser would have to recalculate its width every single time the user zooms Eats too much battery life and processor time

CSS units But surely resolution tells us something useful. doesn t it?

6 Resolution

Resolution if (window.devicepixelratio >= 2)! @media all and ( ( - w e b k i t - m i n d e v i c e - p i x e l - r a t i o : 2 ), ( m i n - r e s o l u t i o n : 1 9 2 d p i ) )

Resolution What is device pixel ratio? It s the ratio of screen size in device pixels and ideal viewport size

iphone 3G device pixels: 320 ideal viewport: 320 Therefore the devicepixelratio is 1

iphone 4S device pixels: 640 ideal viewport: 320 Therefore the devicepixelratio is 2

Samsung Galaxy Pocket device pixels: 240 ideal viewport: 320 Therefore the devicepixelratio is 0.75

BlackBerry Z10 device pixels: 768 ideal viewport: 342 Therefore the devicepixelratio is 2.24561403508772 (Weird, but not wrong)

7 More information

The Mobile Web Handbook by me Published by Smashing Magazine For sale online Ships late June or July

Thank you I ll put these slides online Questions? Peter-Paul Koch http://quirksmode.org http://twitter.com/ppk CSS Day, 4 June 2014