Hyper useful, ready to use HTML5 snippets

by Jean. 16 Comments -

HTML is super easy to write, but when creating webpages you often need to do the same repetitive tasks, such as creating forms. In this article, I have compiled 10+ ready-to-use HTML snippets to fasten your front-end coding.

HTML5 Starter Template

When starting a new project, you need a starter template. Here is a concise and clean template to serve as a basis for your HTML5 projects.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Untitled</title>
		<!--[if lt IE 9]>
		<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
		<![endif]-->
	</head>
	<body>
		
	</body>
</html>

Source: http://snipplr.com/view/68539/plain-html5-starter-template/

Get Directions Form (Google Maps)

Here is a simple yet powerful code to create a form where the user can enter his location to get directions to a specific place. Very useful for contact pages.

<form action="http://maps.google.com/maps" method="get" target="_blank">
   <label for="saddr">Enter your location</label>
   <input type="text" name="saddr" />
   <input type="hidden" name="daddr" value="350 5th Ave New York, NY 10018 (Empire State Building)" />
   <input type="submit" value="Get directions" />
</form>

Source: http://css-tricks.com/snippets/html/get-directions-form-google-maps/

Base64 Encode of a 1*1px “spacer” gif

I don’t recommend using transparent “spacer” gifs, but I know that even in 2013, many people are still using them from time to time. If you’re one of them, you’ll probably enjoy this Base64 encode of a 1*1px “spacer” gif. Way better than using an image.

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7">

Source: http://css-tricks.com/snippets/html/base64-encode-of-1x1px-transparent-gif/

Regexp pattern for email validation

HTML5 allows, among other things, to validate emails using a regular expression pattern. Here is a ready to use input field with the regexp pattern to validate an email address.

<input type="text" title="email" required pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" />

Source: http://blog.staffannoteberg.com/2012/03/01/html5-form-validation-with-regex/

Valid flash embed

Are you often embedding Flash files into your html pages? If yes, you’ll better save the valid flash embed code below for future use.

<object type="application/x-shockwave-flash" 
  data="your-flash-file.swf" 
  width="0" height="0">
  <param name="movie" value="your-flash-file.swf" />
  <param name="quality" value="high"/>
</object>

Source: http://yoast.com/articles/valid-flash-embedding/

HTML5 video with Flash fallback

Another great feature of the new HTML5 specification is definitely the video tag which allows you to easily embed video files. But unfortunately, some browsers can’t deal with embedded HTML5 video. So here is a complete code with a flash fallback for older browsers.

<video width="640" height="360" controls>
	<source src="__VIDEO__.MP4"  type="video/mp4" />
	<source src="__VIDEO__.OGV"  type="video/ogg" />
	<object width="640" height="360" type="application/x-shockwave-flash" data="__FLASH__.SWF">
		<param name="movie" value="__FLASH__.SWF" />
		<param name="flashvars" value="controlbar=over&amp;image=__POSTER__.JPG&amp;file=__VIDEO__.MP4" />
		<img src="__VIDEO__.JPG" width="640" height="360" alt="__TITLE__"
		     title="No video playback capabilities, please download the video below" />
	</object>
</video>

Source: http://camendesign.com/code/video_for_everybody

iPhone call & sms links

With the release of the iPhone, Apple introduced a quick way to create call and sms links. Here is a sample code to keep in your snippet library.

<a href="tel:1-408-555-5555">1-408-555-5555</a>
<a href="sms:1-408-555-1212">New SMS Message</a>

Source: https://developer.apple.com/library/ios/featuredarticles…

Autocompletion with HTML5 datalists

Using the datalist element, HTML5 allows you to create a list of data to autocomplete a input field. Super useful! Here is a sample code to re-use in your own projects.

<input name="frameworks" list="frameworks" />

<datalist id="frameworks">
	<option value="MooTools">
	<option value="Moobile">
	<option value="Dojo Toolkit">
	<option value="jQuery">
	<option value="YUI">
</datalist>

Source: http://davidwalsh.name/datalist

Country drop down list for web forms

Here’s another time saver: A ready-to-use dropdown list with all countries.
Due to the size of the code, please see source directly.
Source: http://snipplr.com/view/4792/country-drop-down-list-for-web-forms/

Downloadable files

HTML5 allows you to force download of files using the download attribute. Here is a standard link to a downloadable file.

<!-- will download as "expenses.pdf" -->
<a href="/files/adlafjlxjewfasd89asd8f.pdf" download="expenses.pdf">Download Your Expense Report</a>

Source: http://snipplr.com/view/4792/country-drop-down-list-for-web-forms/

Crash IE6

In 2013, most people have finally upgraded from the horrible Internet Explorer 6 which gave nightmares to every front-end developper for years. But some persons are still using it. If you want to get rid of this prehistoric browser for good, here is a very funny code to include in your html pages.

This code will crash IE6. Bam!

<style>*{position:relative}</style><table><input></table>

Source: http://www.catswhocode.com/blog/html5-code-snippets-to-take-your-website-to-the-next-level

  • tobozo

    Best source so far for country list, not html5 formatted but provides 2 and 3 letters country codes, and country names are available in all languages :

    http://cpansearch.perl.org/src/GRAF/Locale-Country-Multilingual-0.23/lib/Locale/Country/Multilingual/

  • Peter

    Nice, thanks for posting !

    BTW : wrong link added to ‘Downloadable files’ item …

  • dj

    You think that doing damage to someone’s browser is a joke do you? I’m disgusted to think you would publish something like this, although I can’t honestly say that I’m too surprised.

    • maurycy

      It’s not a damage to browser it’s forcing retarded people to update

    • concerned

      your right, we shouldnt inform people, just let the browser crash and have no idea why.

      • qgustavor

        Ok, let’s inform people:

        > Hey retarded, update your browser.

        > [Yes] [No, my browser is a good browser]

        Clicking yes: it will go to browsehappy.com.
        Clicking no: it will show “Really?” and append that crash code into the DOM.

        • Andy Davies

          You’re assuming people who are still on IE6 are able to update and it’s pretty clear from the evidence that they aren’t.

  • Angelo

    The email validation doesn’t allow for subdomains after the @ sign. Might want something like the following:

  • J.B Lievremont

    Each time I see a proposal regex for email address validation, I feel forced to share this StackOverflow answer: http://stackoverflow.com/a/201378

    Using regex for email address validation is akin to using regex for XML/HTML parsing: when you do it, you have one more problem to solve.

    This point aside, nice article, especially the simple trick of using datalist for autocomplete.

  • Paweł

    Why would you use HTML5 pattern to validate an email, when you have HTML5 type=”email”?

  • http://www.codegeekz.com ceba

    Nice collection, thanks for posting.

  • http://knah-tsaeb.org Knah Tsaeb

    Thanks for this another great post.

    But the link for “Downloadable files” is not good (same of previous link, probably bad CRTL+C / CRTL+V)

  • http://www.bestskincarezone.com Jamie

    Ready to use indeed! These snippets can save a lot of time.

  • podi eka

    Can you add similar code to ie7 and 8 as well :)

  • http://www.computertechtips.net Xps

    The HTML5 Starter Boilerplate doesn’t include the language attribute to the html tag.

    I’m happy to see you have the character-set above the title tag. So many websites have it backwards and so many writers fail to mention it. The character-set goes before any text is rendered, including the title.

    I *think* that IE versions below the posted v9 require the script tag to include the type attribute as well.

  • http://ru.kpitv.net/ Онлайн ТВ

    Instead

    use

    Thanks for Autocompletion, download attribute

    But my site http://en.kpitv.net/ is still mainly html4 :)