Table of Contents for
The Modern Web

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition The Modern Web by Peter Gasston Published by No Starch Press, 2013
  1. The Modern Web
  2. Cover
  3. The Modern Web
  4. Advance Praise for
  5. Praise for Peter Gasston’s
  6. Dedication
  7. About the Author
  8. About the Technical Reviewer
  9. Acknowledgments
  10. Introduction
  11. The Device Landscape
  12. The Multi-screen World
  13. Context: What We Don’t Know
  14. What You’ll Learn
  15. A. Further Reading
  16. 1. The Web Platform
  17. A Quick Note About Terminology
  18. Who You Are and What You Need to Know
  19. Getting Our Terms Straight
  20. The Real HTML5
  21. CSS3 and Beyond
  22. Browser Support
  23. Test and Test and Test Some More
  24. Summary
  25. B. Further Reading
  26. 2. Structure and Semantics
  27. New Elements in HTML5
  28. WAI-ARIA
  29. The Importance of Semantic Markup
  30. Microformats
  31. RDFa
  32. Microdata
  33. Data Attributes
  34. Web Components: The Future of Markup?
  35. Summary
  36. C. Further Reading
  37. 3. Device-Responsive CSS
  38. Media Queries
  39. Media Queries in JavaScript
  40. Adaptive vs. Responsive Web Design
  41. Viewport-Relative Length Units
  42. Responsive Design and Replaced Objects
  43. Summary
  44. D. Further Reading
  45. 4. New Approaches to CSS Layouts
  46. Multi-columns
  47. Flexbox
  48. Grid Layout
  49. The Further Future
  50. Summary
  51. E. Further Reading
  52. 5. Modern JavaScript
  53. New in JavaScript
  54. JavaScript Libraries
  55. Polyfills and Shims
  56. Testing and Debugging
  57. Summary
  58. F. Further Reading
  59. 6. Device Apis
  60. Geolocation
  61. Orientation
  62. Fullscreen
  63. Vibration
  64. Battery Status
  65. Network Information
  66. Camera and Microphone
  67. Web Storage
  68. Drag and Drop
  69. Interacting with Files
  70. Mozilla’s Firefox OS and WebAPIs
  71. PhoneGap and Native Wrappers
  72. Summary
  73. G. Further Reading
  74. 7. Images and Graphics
  75. Comparing Vectors and Bitmaps
  76. Scalable Vector Graphics
  77. The canvas Element
  78. When to Choose SVG or Canvas
  79. Summary
  80. H. Further Reading
  81. 8. New Forms
  82. New Input Types
  83. New Attributes
  84. Datalists
  85. On-Screen Controls and Widgets
  86. Displaying Information to the User
  87. Client-side Form Validation
  88. The Constraint Validation API
  89. Forms and CSS
  90. Summary
  91. I. Further Reading
  92. 9. Multimedia
  93. The Media Elements
  94. Media Fragments
  95. The Media API
  96. Media Events
  97. Advanced Media Interaction
  98. Summary
  99. J. Further Reading
  100. 10. Web Apps
  101. Web Apps
  102. Hybrid Apps
  103. TV Apps
  104. Webinos
  105. Application Cache
  106. Summary
  107. K. Further Reading
  108. 11. The Future
  109. Web Components
  110. The Future of CSS
  111. Summary
  112. L. Further Reading
  113. M. Browser Support as of March 2013
  114. The Browsers in Question
  115. Enabling Experimental Features
  116. Chapter 1: The Web Platform
  117. Chapter 2: Structure and Semantics
  118. Chapter 3: Device-Responsive CSS
  119. Chapter 4: New Approaches to CSS Layouts
  120. Chapter 5: Modern JavaScript
  121. Chapter 6: Device APIs
  122. Chapter 7: Images and Graphics
  123. Chapter 8: New Forms
  124. Chapter 9: Multimedia
  125. Chapter 10: Web Apps
  126. Chapter 11: The Future
  127. N. Further Reading
  128. Introduction
  129. Chapter 1: The Web Platform
  130. Chapter 2: Structure and Semantics
  131. Chapter 3: Device-Responsive CSS
  132. Chapter 4: New Approaches to CSS Layouts
  133. Chapter 5: Modern JavaScript
  134. Chapter 6: Device APIs
  135. Chapter 7: Images and Graphics
  136. Chapter 8: New Forms
  137. Chapter 9: Multimedia
  138. Chapter 10: Web Apps
  139. Chapter 11: The Future
  140. Index
  141. About the Author
  142. Copyright

Viewport-Relative Length Units

As mentioned previously, one problem with using percentages for widths is the fact that they’re relative to their parent elements; that may not seem like a problem at first, but it can soon become quite complex. A better approach is to use a unit that’s relative to a fixed value, rather than a value inherited from a parent; this is what the vh and vw units are for.

The v stands for viewport, and you may then be able to extrapolate that h is for height and w for width. In other words, this unit is relative to the dimensions of the viewport. Each number you use with this value is equal to 1 percent of the respective length of the viewport. So to make an element that’s half the height of the viewport use this:

E { height: 50vh; }

And as they’re always relative to the viewport rather than the parent, that means no more long strings of numbers after the decimal point. If you return to the earlier example of an element that’s 85 percent the width of the viewport with a child element that’s 55 percent of the viewport, the values are much more straightforward and easier to manage:

E { width: 85vw; }
F { width: 55vw; }

Two companion units, vmax and vmin, are also available; the first means use the greater length of the viewport, whether height or width, and the second means use the lesser value. So given a viewport with a resolution of 800×600, vmax would be equivalent to vw, and vmin equivalent to vh.

Root-Relative Units

Units that are relative to the dimensions are helpful, but if you want to use a unit over which you have a little more control, you can use one that’s relative to a value that you set. The root em (rem) is a typographical unit, which like em is based on the width of a capital M character; the definition is not really important though, all you really need to know is that you can use rem for relative sizing.

Where rem differs from em is that the latter is inherited, whereas the former is fixed. For example, say you set a font size of 10px on the root of a document:

html { font-size: 10px; }

If you want to make a p element that’s 12px in size, you could use either one of them in this way:

p { font-size: 1.2em; }
p { font-size: 1.2rem; }

So far, so identical. But now presume that you have a b element inside that p, like this:

<p>This is <b>bold</b>.</p>

And you want the b to be 13px in size. Because em is inherited, you would have to divide the larger figure by the smaller to get the new font size:

p b { font-size: 1.08333em; }

Using em has the same two drawbacks you saw earlier in the chapter: long, unwieldy numbers that become harder and harder to work with and differences in rounding values between browsers. But the rem value, which is always relative to the root, doesn’t have these problems, making it much easier to work with:

p b { font-size: 1.3rem; }

Of course, although it’s a typographic unit, you can still use rem for length values in the same way that many people use em.

Note

I’m using 10px as the root font size only for the sake of illustration, but you shouldn’t do that in real life as it ignores the user’s custom font settings. Better to use 62.5 percent, which equates to 10px for most users (16px is the default font size, 62.5 percent of which is 10px) but still makes allowances for users with vision impairment.

Mobile First and Content Breakpoints

Two systems that I find really useful when building responsive websites are mobile first and breakpoints. These aren’t hard and fast rules—you don’t have to implement them if you think they’re not appropriate for what you’re making—but certainly for browser-based websites I can’t imagine building without them.

The mobile first methodology targets the smaller (and most probably lower-powered) devices first and then adds layers of complexity to accommodate larger devices. What this means in essence is you start with a set of styles that’s served to all devices—such as color, typography, iconography, and so on—as well as the minimal layout rules required for a small-screen device.

The next step is to add a media query that adds a new set of rules for larger devices/agents; this might be a tablet, for example. For anyone using an even larger device, add extra rules and so on, until you’ve catered to a core set of devices. If you imagine your core devices are mobile, tablet, and desktop, you’d end up with a set of style rules that looked something like this:

<link rel="stylesheet" href="base.css" media="screen">
<link rel="stylesheet" href="tablet.css" media="screen and (min-width: 481px)">
<link rel="stylesheet" href="desktop.css" media="screen and (min-width: 801px)">

The actual breakpoints you use are completely up to you; they depend on the devices you want to optimize for and should be based on analysis of existing visitors, if that information is relevant and available to you. The examples I’ve used here are simplistic and employed only to illustrate the method.

Luke Wroblewski is one of the leading proponents of this method. In the introduction to his book Mobile First (A Book Apart, 2011), he says:

Designing for mobile first now can not only open up new opportunities for growth, it can lead to a better overall user experience for a website or application.

The other approach to consider is setting breakpoints for content rather than devices. With the huge range of web-enabled devices on the market now (and plenty more to come in the future), the boundaries between phone, tablet, laptop, and desktop are incredibly blurred. The phone I own has a 4.65” screen. Samsung’s Galaxy Note has a 5.3” screen. Google’s Nexus 7 has a 7” screen. At what point does the device stop being a phone and become a tablet?

In my opinion, labels like phone and tablet (and especially portmanteau terms like phablet) are fast becoming obsolete. As the functionality of all our Internet-enabled devices converges, we’ll have to find a new vocabulary to describe these things (although I’m not going to presume to know what that new vocabulary will be).

My point in mentioning this is that the idea of building breakpoints based on device dimensions may well be a snipe hunt. Think about making breakpoints based on content instead; rather than thinking “how wide should my content be on a tablet,” think “what’s the maximum width this content can be before it becomes unreadable.”

What this means in practice is using media queries to change content, not when an arbitrary device size has been reached, but when that content becomes awkward. To illustrate what I mean, imagine I have a stylesheet applied to wide viewports using a media query:

<link rel="stylesheet" href="foo.css" media="screen and (min-width: 1000px)">

And within that stylesheet I set the width of an article element to be some 60 percent of the viewport’s width and a font to be 120 percent of the root:

article { width: 60vw; }
article p { font-size: 1.2rem; }

Many studies have shown that for optimal readability the maximum number of characters in a line of text is between 45 and 75, with 66 considered ideal, but if the user has a device with a very large viewport width, the width of 60vw may well lead to lines of text that could double that. Using the theory of content breakpoints, you may want to add an extra rule within the larger device stylesheet to make the text larger on much wider devices to restore some of the readability:

@media screen and (min-width: 1200px) {
  p { font-size: 1.4rem; }
}

Working in this way isn’t going to be easy—quite the opposite, in fact. You will have to perform a lot of testing and analysis and make decisions based on best practice and instinct. Some people, such as Thierry Koblentz in his article “Device-Agnostic Approach to Responsive Web Design,” have advocated using content breakpoints solely and not considering devices at all:

If we consider that content is king, then it makes sense to look at it as the corner stone of the solution. In other words, we should set break-points according to content instead of devices.

My opinion is that a combination of the two serves you best. As always, experiment and find your own system.