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

Microdata

HTML5 has addressed the semantic issue with the creation of a simple syntax called microdata. This is essentially a series of name-value pairs that provide meaningful machine-readable data. As always, before trying to explain it, showing you how it works is easier:

<p itemscope>I live in <span itemprop="city">London</span></p>

This markup creates a single item. The attribute itemscope is used on the containing element to mark the limits, or scope, of this particular item. Inside we have the name-value pair, known as a property: The value of the itemprop attribute is the name—which, in this example, is city—and the element’s content is the value—in this case, London. The result is an item with a single property:

city: 'London'

But you’re not limited to a single property per item; you can have as many as you like:

<p itemscope>Hello, my name is <span itemprop="given-name">Peter</span> and I’m <span itemprop="role">a developer</span> from <span itemprop="city">London</span>.</p>

In this case, the item’s property list looks like this:

given-name: 'Peter'
role: 'a developer'
city: 'London'

As you can see, this markup is somewhat similar to RDFa, and just like that format, you can give different values to machines and humans. Look at this example where I use the datetime attribute:

<p itemscope>My birthday this year is on <span itemprop="birthday" datetime="2013-12-14">December 14</span>.</p>

And, as with RDFa, you can describe content with predefined schema by linking to it with the itemtype attribute:

<p itemscope itemtype="http://example.org/birthday">My birthday this year is on <span itemprop="birthday" datetime="2013-12-14">December 14</span>.</p>

You can use schema such as the previously mentioned Dublin Core, or even one of your own invention, as I just showed in the previous code block.

The Microdata API

Microdata has a companion DOM API, which is useful for extracting the data from the page and is already fairly broadly implemented in modern browsers. The key to the API is the getItems() method, which returns a NodeList containing all of the items on the page:

var items = document.getItems();

From there, you can choose a single item and, for example, see how many properties it contains using the properties object:

var firstItemLen = items[0].properties.length;

Or you can discover the value of one of those properties:

var itemVal = items[0].properties['name'][0].itemValue;

You can see these demonstrated in the example file microdata-api.html. I’ve logged the results in the console, so open up your favorite browser and take a look. I encourage you to play around with it yourself. For anyone who doesn’t have a browser handy, Figure 2-2 shows how the results are logged in Firebug.

The results of some simple explorations of the microdata API, shown in the console
Figure 2-2. The results of some simple explorations of the microdata API, shown in the console

Microdata, Microformats, and RDFa

If you’ve decided that adding machine-readable semantic data to your pages is the right way to go, which format should you use? The answer, of course, is it depends. Evaluate your content, read about the strengths and weaknesses of each of the data types, and decide which one’s best for you.

My personal feeling is that in the future we’ll mostly see a mixture of microdata with simple microformats. One of the interesting things about microdata is that it’s capable of accommodating both of its contemporaries within its own flexible syntax. For example, here’s how to mark up hCard using microdata:

<div itemscope itemtype="http://microformats.org/profile/hcard">
<p><a href="http://about.me/petergasston" itemprop="url fn">Peter Gasston</a> writes for <a href="http://broken-links.com" itemprop="url org">Broken Links</a>.</p>
</div>

Likewise, you can easily use RDFa data schema:

<p itemscope itemtype="http://purl.org/dc/elements/1.1/date" datetime="2013-04-01">April 1</p>

In my opinion, microdata’s flexibility will lead to it being used more and more. That said, it’s not perfect for everything; some microformats, such as Rel-Tag, are so concise and easy to use that’s there’s little point in trying to replace them.

Schema.org

One good reason for using microdata, and another reason I think it’s set to conquer microformats and RDFa, is that you might receive a nice advantage and get your content noticed and promoted by search engines and portals. In 2011 four big Web giants—Google, Microsoft, Yahoo!, and Yandex—launched a new website, Schema.org, which introduced a set of shared vocabularies for marking up common patterns using microdata.

Those patterns include reviews, events, places, items, and objects, things that get discussed frequently across the Web. To illustrate, say you’re writing a book review on your website (I’ve chosen a book at random and given it an unbiased review):

<div class="review">
  <h1>The Book of CSS3, by Peter Gasston</h1>
  <p>What an amazing book! 5 stars!</p>
</div>

This review actually contains two items: the details of the book and a review of it. Schema.org has two vocabularies that you can use to mark this up semantically: they are Book and Review. A visit to the relevant sections shows me which microdata patterns I should use. With that done, I can update my markup:

<div class="review" itemscope itemtype="http://schema.org/Review">
  <h1><span itemprop="itemReviewed">The Book of CSS3</span>, by <span itemprop="creator">Peter Gasston</span></h1>
  <p><span itemprop="reviewBody">What an amazing book!</span> <span itemprop="reviewRating">5</span> stars!</p>
</div>

Although my markup has gotten more complex, it means more now. Each of the vocabularies I’ve used is defined with a link to the relevant schema in the itemtype attribute, and the items are marked up with preset itemprop values.

What’s interesting about Schema.org is the way that specific schema inherit properties from broader ones; Book, for example, has properties from its own schema, the broader CreativeWork vocabulary, and the top-level Thing (great name!), which has the most generic properties.

By marking up my content using Schema.org patterns, all of the crawlers that reach my page will know the author and title of this book, the fact that I’m reviewing it, and that I gave it a five-star rating. If someone searches for that book, my review could appear in the search results or be aggregated with others to provide a decent overview to the reader.

Rich Snippets

The method of giving extra information in search results, which is used by many search engines, is known by Google as rich snippets. Rich snippets give a user’s search query more context, allowing the user to better evaluate the relevance of the result without having to click through to the page. You can see an example of a rich snippet in Figure 2-3.

Example of a rich snippet giving extra information on Google search results
Figure 2-3. Example of a rich snippet giving extra information on Google search results

Rich snippets work with microformats and RDFa, but its preferred syntax is microdata. Plenty of information and documentation is available for developers on Google’s Webmaster pages, including a useful tool to test if your microdata is formatted correctly. In Figure 2-4, you can see the data this tool has extracted from the book review created in the previous section.

Data extracted from the marked-up book review by the rich snippet testing tool
Figure 2-4. Data extracted from the marked-up book review by the rich snippet testing tool