HTML Snippets: The Most Useful Ready-to-use Examples


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 user guide, 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>
<meta name="description" content="This is an example of a meta description.">
<link rel="stylesheet" type="text/css" href="theme.css">

<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>

</body>
</html>

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

Load JavaScript Asynchronously

One of the best features of HTML5 is the possibility to asynchronously load JavaScript files. This is done very easily, simply by adding the async attribute to your script tags:

<script src="https://catswhocode.com/script.js" async></script>

This simple code snippet shows an asynchronous loading of a JavaScript file. This will make the page load much faster as the browser will simultaneously load both the HTML and the JavaScript file.

Define Viewport for Responsive Websites

When creating a responsive website, setting the viewport is a must.The following HTML snippet should be inserted into the head section of your document.

<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="HandheldFriendly" content="true">

This HTML snippet set the view on all screens at a 1×1 aspect ratio and remove the default functionality from iPhones and other mobile devices which render websites at full-view and allow users to zoom into the layout by pinching.

Get Directions Form (Google Maps)

Here is a simple yet powerful code snippet 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: https://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 2019, 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: https://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}" />

Bootstrap Login Form

Bootstrap Vertical FormA very basic Bootstrap form template that can be easily enhanced and modified to suit almost any need. Use this HTML snippet as a starting point for almost any kind of simple forms, such as login forms, contact forms, etc.

<form role="form">
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd">
</div>
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>

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>

Insert HTML Video With Flash Fallback

Another great feature of the HTML5 specification is definitely the video tag which allows you to easily embed video files.Unfortunately, older browsers can’t deal with embedded HTML5 videos. So here is a complete HTML snippet with a flash fallback for old 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

With the release of the iPhone, Apple introduced a quick way to create call and sms links. Here is a sample code snippet 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>

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: https://davidwalsh.name/datalist

Country Dropdown 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: https://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>

Restrict Uploads To Specific Mime Types

The accept attribute was introduced in the HTML5 specification. Used on an input type="file" element, it restricts file upload to specified mime types:

<input type="file" name="media_empty" accept="image/gif,image/jpeg,image/jpg,image/png">

The accept attribute allows to specify a comma-separated list of mime types that will be accepted for upload. In the above code snippet, only images are accepted.Source: David Walsh

Crash IE6

In 2019, most people have finally upgraded from the horrible Internet Explorer 6 which gave nightmares to every front-end developer 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>

Frequently Asked Questions

HTML Snippet

What is a HTML Snippet?

An HTML snippet is a small portion of source code in HTML markup. Snippets like those featured in this user guide are easy to re-use in your own projects, hence their popularity among developers.

How Do I Insert HTML Code to a WordPress Post or Page?

When displaying an HTML snippet on a WordPress post or page, make sure to encode the HTML code and embed it with <pre> and </pre>. For more information, please refer to this display code in WordPress guide.

What Kind of Hosting is the Best for HTML?

Pure HTML pages can be hosted on any hosting plan. Bluehost provides a secure platform for HTML and PHP sites for $3.95/mo. More information can be found on our best web hosting guide.

Jean-Baptiste Jung is a self-taught web developer, web designer, and influential blogger from Belgium who has made significant contributions to the WordPress community since 2006. As the founder of CatsWhoCode (established in 2008) and WPRecipes, he has helped countless developers through his tutorials and technical guides. Jung is also a published author of the WordPress 2.7 Cookbook and a regular contributor to prestigious publications like Smashing Magazine. Known for prioritizing quality over quantity in his work, he advocates for passion in web development and believes in making technical knowledge accessible to others. His expertise spans multiple programming languages, with a focus on PHP, WordPress, HTML/CSS, and JavaScript development.

«
»

Leave a Reply

Your email address will not be published. Required fields are marked *