1:<!DOCTYPE html PUBLIC 2:"-//W3C//DTD XHTML 1.0 Transitional//EN" 3:"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">If so, why? Switch to the new HTML5 doctype. You’ll live longer — as Douglas Quaid might say.
1:<!DOCTYPE html>In fact, did you know that it truthfully isn’t even really necessary for HTML5? However, it’s used for current, and older browsers that require a specified doctype. Browsers that do not understand this doctype will simply render the contained markup in standards mode. So, without worry, feel free to throw caution to the wind, and embrace the new HTML5 doctype.
1:<img src="path/to/image" alt="About image" /> 2:<p>Image of Mars. </p>There unfortunately isn’t any easy or semantic way to associate the caption, wrapped in a paragraph tag, with the image element itself. HTML5 rectifies this, with the introduction of the <figure> element. When combined with the <figcaption> element, we can now semantically associate captions with their image counterparts.
1:<figure> 2: <img src="path/to/image" alt="About image" /> 3: <figcaption> 4: <p>This is an image of something interesting.</p> 5: </figcaption> 6:</figure>
Not long ago, I utilized the <small> element to create subheadings that are closely related to the logo. It’s a useful presentational element; however, now, that would be an incorrect usage. The small element has been redefined, more appropriately, to refer to small print. Imagine, a copyright statement in the footer of your site; according to the new HTML5 definition of this element; the <small> would be the correct wrapper for this information.
1:<link rel="stylesheet" href="path/to/stylesheet.css" 2:type="text/css"/> 3:<script type="text/javascript" src="path/to/script.js"> 4:</script>This is no longer necessary. It’s implied that both of these tags refer to stylesheets and scripts, respectively. As such, we can remove the type attribute all together.
1:<link rel="stylesheet" href="path/to/stylesheet.css" /> 2:<script src="path/to/script.js"></script>
1:<p class=myClass id=someId> Start the reactor.
Make up your own mind on this one. If you prefer a more structured document, by all means, stick with the quotes.
1:<!DOCTYPE html> 2: 3:<html lang="en"> 4:<head> 5: <meta charset="utf-8"> 6: <title>untitled</title> 7:</head> 8:<body> 9: <h2> To-Do List </h2> 10: <ul contenteditable="true"> 11: <li> Break mechanical cab driver. </li> 12: <li> Drive to abandoned factory 13: <li> Watch video of self </li> 14: </ul> 15:</body> 16:</html>Or, as we learned in the previous tip, we could write it as:
1:<ul contenteditable=true>
1:<!DOCTYPE html> 2: 3:<html lang="en"> 4:<head> 5: <meta charset="utf-8"> 6: <title>untitled</title> 7:</head> 8:<body> 9: <form action="" method="get"> 10: <label for="email">Email:</label> 11: <input id="email" name="email" type="email" /> 12: 13: <button type="submit"> Submit Form </button> 14: </form> 15:</body> 16:</html>
It should also be noted that all the current browsers are a bit wonky when it comes to what elements and attributes they do and don’t support. For example, Opera seems to support email validation, just as long as the name attribute is specified. However, it does not support the placeholder attribute, which we’ll learn about in the next tip. Bottom line, don’t depend on this form of validation just yet…but you can still use it!
1:<input name="email" type="email" 2:placeholder="doug@givethesepeopleair.com" />
Again, support is shady at best across browsers, however, this will continue to improve with every new release. Besides, if the browser, like Firefox and Opera, don’t currently support the placeholder attribute, no harm done.
Thanks to local storage (not officially HTML5, but grouped in for convenience's sake), we can make advanced browsers "remember" what we type, even after the browser is closed or is refreshed.
While obviously not supported across all browsers, we can expect this method to work, most notably, in Internet Explorer 8, Safari 4, and Firefox 3.5. Note that, to compensate for older browsers that won’t recognize local storage, you should first test to determine whether window.localStorage exists.
1:<div id="header"> 2: ... 3:</div> 4: 5:<div id="footer"> 6: ... 7:</div>Divs, by nature, have no semantic structure — even after an id is applied. Now, with HTML5, we have access to the <header> and <footer> elements. The mark-up above can now be replaced with:
1:<header> 2: ... 3:</header> 4: 5:<footer> 6: ... 7:</footer>
Try not to confuse these elements with the “header” and “footer” of your website. They simply refer to their container. As such, it makes sense to place, for example, meta information at the bottom of a blog post within the footer element. The same holds true for the header.
Learn about more helpful HTML5 form features in this quick video tip.
Unfortunately, that dang Internet Explorer requires a bit of wrangling in order to understand the new HTML5 elements.
All elements, by default, have a display of inline.
In order to ensure that the new HTML5 elements render correctly as block level elements, it’s necessary at this time to style them as such.1:header, footer, article, section, nav, menu, hgroup { 2: display: block; 3:}Unfortunately, Internet Explorer will still ignore these stylings, because it has no clue what, as an example, the header element even is. Luckily, there is an easy fix:
1:document.createElement("article"); 2:document.createElement("footer"); 3:document.createElement("header"); 4:document.createElement("hgroup"); 5:document.createElement("nav"); 6:document.createElement("menu");Strangely enough, this code seems to trigger Internet Explorer. To simply this process for each new application, Remy Sharp created a script, commonly referred to as the HTML5 shiv. This script also fixes some printing issues as well.
1:<!--[if IE]> 2:<script 3:src="http://html5shim.googlecode.com/svn/trunk/html5.js"> 4:</script> 5:<![endif]-->
1:<header> 2: <hgroup> 3: <h1> Recall Fan Page </h1> 4: <h2> Only for people who want the memory of a 5: lifetime.</h2> 6: </hgroup> 7:</header>
1:<input type="text" name="someInput" required>Or, with a more structured approach.
1:<input type="text" name="someInput" required="required">Either method will do. With this code, and within browsers that support this attribute, a form cannot be submitted if that “someInput” input is blank. Here’s a quick example; we’ll also add the placeholder attribute as well, as there’s no reason not to.
1:<form method="post" action=""> 2: <label for="someInput"> Your Name: </label> 3: <input type="text" id="someInput" name="someInput" 4: laceholder="Douglas Quaid" required> 5: <button type="submit">Go</button> 6:</form>
If the input is left blank, and the form is submitted, the textbox will be highlighted.
1:<input type="text" name="someInput" placeholder="Douglas 2:Quaid" required autofocus>
Interestingly enough, while I personally tend to prefer a more XHTML approach (using quotation marks, etc.), writing "autofocus=autofocus" feels odd. As such, we’ll stick with the single keyword approach.
1:<audio autoplay="autoplay" controls="controls"> 2: <source src="file.ogg" /> 3: <source src="file.mp3" /> 4: <a href="file.mp3">Download this file.</a> 5:</audio>
Mozilla and Webkit don’t fully get along just yet, when it comes to the audio format. Firefox will want to see an .ogg file, while Webkit browsers will work just fine with the common .mp3 extension. This means that, at least for now, you should create two versions of the audio.
When Safari loads the page, it won’t recognize that .ogg format, and will skip it and move on to the mp3 version, accordingly. Please note that IE, per usual, doesn’t support this, and Opera 10 and lower can only work with .wav files.
1:<video controls preload> 2: <source src="cohagenPhoneCall.ogv" 3: type="video/ogg; codecs='vorbis, theora'" /> 4: <source src="video.mp4" 5: type="video/mp4; 'codecs='avc1.42E01E, mp4a.40.2'" /> 6: <p>Your browser is old. <a href="video.mp4"> 7: Download this video instead.</a></p> 8:</video>
There are a few things worth noting here.
1. We aren’t technically required to set the type attribute; however, if we don’t, the browser has to figure out the type itself. Save some bandwidth, and declare it yourself.
2. Not all browsers understand HTML5 video. Below the source elements, we can either offer a download link, or embed a Flash version of the video instead. It’s up to you.
3. The controls and preload attributes will be discussed in the next two tips.
1:<video preload>
1:<video preload controls>
Please note that each browser renders its player differently from one another.
1:<form action="" method="post"> 2: <label for="username">Create a Username: </label> 3: <input type="text" 4: name="username" 5: id="username" 6: placeholder="4 <> 10" 7: pattern="[A-Za-z]{4,10}" 8: autofocus 9: required> 10: <button type="submit">Go </button> 11:</form>
If you’re moderately familiar with regular expressions, you’ll be aware that this pattern: [A-Za-z]{4,10} accepts only upper and lowercase letters. This string must also have a minimum of four characters, and a maximum of ten.
Notice that we’re beginning to combine all of these new awesome attributes!
If regular expressions are foreign to you, refer here.
1:alert( 'pattern' in document.createElement('input') ); In fact, this is a popular method of determining browser compatibility. The jQuery library utilizes this trick. Above, we’re creating a new input element, and determining whether the pattern attribute is recognized within. If it is, the browser supports this functionality. Otherwise, it of course does not.1:<script> 2:if (!'pattern' in document.createElement('input') ) { 3: // do client/server side validation 4:} 5:</script>
Keep in mind that this relies on JavaScript!
1:<h3> Search Results </h3> 2:<p> They were interrupted, just after Quato said, 3:<mark>"Open your Mind"</mark>. </p>
Some of us initially questioned when we should plain-ole divs. Now that we have access to headers, articles, sections, and footers, is there ever a time to use a...div? Absolutely.
"Divs should be utilized when there’s no better element for the job."
For example, if you find that you need to wrap a block of code within a wrapper element specifically for the purpose of positioning the content, a <div> makes perfect sense. However, if you’re instead wrapping a new blog post, or, perhaps, a list of links in your footer, consider using the <article> and <nav> elements, respectively. They’re more semantic.
With all this talk about HTML5 not being complete until 2022, many people disregard it entirely – which is a big mistake. In fact, there are a handful of HTML5 features that we can use in all our projects right now! Simpler, cleaner code is always a good thing. In today’s video quick tip, I’ll show you a handful of options.
People can be forgiven for assuming that awesome JavaScript-less transitions are grouped into the all-encompassing HTML5. Hey — even Apple has inadvertently promoted this idea. For non-developers, who cares; it’s an easy way to refer to modern web standards. However, for us, though it may just be semantics, it’s important to understand exactly what is not HTML5.
1. SVG: Not HTML5. It’s at least five years old.
2. CSS3: Not HTML5. It’s...CSS.
3. Client Storage: Not HTML5. It was at one point, but was removed from the spec, due to the fact that many worried that it, as a whole, was becoming too complicated. It now has its own specification.
4. Web Sockets: Not HTML5. Again, was exported to its own specification.
Regardless of how much distinction you require, all of these technologies can be grouped into the modern web stack. In fact, many of these branched specifications are still managed by the same people.