JavaScript polyfill that enables HTML5 semantic elements to work in legacy Internet Explorer browsers
npx @tessl/cli install tessl/npm-html5shiv@3.7.0HTML5 Shiv is a JavaScript polyfill that enables HTML5 semantic elements to work properly in legacy Internet Explorer browsers (IE6-9) and other older browsers. The library works by using JavaScript to create these HTML5 elements in the DOM before they are used, allowing CSS styling to be applied correctly.
npm install html5shivAlternative installations:
bower install html5shivBrowser script tag (most common usage):
<!--[if lt IE 9]>
<script src="node_modules/html5shiv/dist/html5shiv.js"></script>
<![endif]-->For print support in IE6-8:
<!--[if lt IE 9]>
<script src="node_modules/html5shiv/dist/html5shiv-printshiv.js"></script>
<![endif]-->CommonJS/Node.js:
const html5 = require('html5shiv');AMD/RequireJS:
require(['html5shiv'], function(html5) {
// Use html5 object
});HTML5 Shiv is built around several key components that work together to provide HTML5 element support in legacy browsers:
document.createElementdocument.createDocumentFragmentThe library follows a progressive enhancement approach - modern browsers with native HTML5 support receive minimal overhead, while legacy browsers get the full polyfill functionality.
Automatic Setup (Recommended): The library automatically initializes when loaded, detecting the browser environment and applying necessary HTML5 element support:
<!DOCTYPE html>
<html>
<head>
<!--[if lt IE 9]>
<script src="dist/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<header>
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
</ul>
</nav>
</header>
<main>
<article>
<section>
<h1>Article Title</h1>
<p>Content goes here...</p>
</section>
</article>
</main>
<footer>
<p>© 2023 Company Name</p>
</footer>
</body>
</html>Configuration Before Loading:
<script>
// Configure before loading the library
window.html5 = {
'elements': 'mark section customelement', // Custom element list
'shivCSS': false, // Disable CSS injection
'shivMethods': false // Disable method overriding
};
</script>
<!--[if lt IE 9]>
<script src="dist/html5shiv.js"></script>
<![endif]-->Programmatic Usage:
// After library is loaded, access the html5 object
console.log('HTML5 Shiv version:', html5.version);
// Manually shiv a document (usually not needed)
html5.shivDocument(document);
// Create HTML5 elements programmatically
var article = html5.createElement('article');
var section = html5.createElement('section');
// Add custom elements to the shiv list
html5.addElements('customelement anotherelement', document);Applies HTML5 element support and CSS styling to a document, enabling HTML5 semantic elements to work properly in legacy browsers.
/**
* Shivs the given document by applying HTML5 element support and CSS styling
* @param ownerDocument - The document to shiv (optional, defaults to global document)
* @returns The shived document
*/
function shivDocument(ownerDocument?: Document): Document;Creates HTML5 elements that work correctly in legacy browsers with proper styling and behavior.
/**
* Creates a shived HTML5 element that works in legacy browsers
* @param nodeName - Name of the element to create (e.g., 'article', 'section')
* @param ownerDocument - Context document (optional, defaults to global document)
* @returns The created shived element
*/
function createElement(nodeName: string, ownerDocument?: Document): Element;Creates document fragments with HTML5 element support for efficient DOM manipulation.
/**
* Creates a shived document fragment with HTML5 element support
* @param ownerDocument - Context document (optional, defaults to global document)
* @returns The created shived document fragment
*/
function createDocumentFragment(ownerDocument?: Document): DocumentFragment;Extends the built-in list of HTML5 elements that the library will shiv, useful for custom elements or newer HTML5 elements not in the default list.
/**
* Extends the built-in list of HTML5 elements and re-shivs the document
* @param newElements - Whitespace-separated string or array of new element names to shiv
* @param ownerDocument - Context document to re-shiv (optional)
* @returns void
*/
function addElements(newElements: string | string[], ownerDocument?: Document): void;Applies additional shiving for print scenarios in IE6-8 by wrapping HTML5 elements with printable equivalents.
/**
* Shivs the given document for print scenarios in IE6-8
* Available only in html5shiv-printshiv.js version
* @param ownerDocument - Document to shiv for printing (optional, defaults to global document)
* @returns The shived document
*/
function shivPrint(ownerDocument?: Document): Document;Note: This method is only available in the
html5shiv-printshiv.jstypeControls which HTML5 elements the library will shiv.
/**
* List of HTML5 element names to shiv
* Can be set as a whitespace-separated string or array
* Default: 'abbr article aside audio bdi canvas data datalist details dialog figcaption figure footer header hgroup main mark meter nav output picture progress section summary template time video'
*/
elements: string | string[];Controls whether the library injects basic HTML5 CSS styling.
/**
* Flag to enable/disable HTML5 CSS styling injection
* When true, adds display:block styles for HTML5 elements and basic mark styling
* Default: true
*/
shivCSS: boolean;Controls whether the library overrides createElement and createDocumentFragment methods.
/**
* Flag to enable/disable overriding of createElement and createDocumentFragment methods
* When true, document methods are replaced with shived versions
* Default: true
*/
shivMethods: boolean;/**
* Current version of html5shiv library
* Read-only property
*/
version: string; // '3.7.3'/**
* Indicates if browser supports creating unknown/HTML5 elements natively
* Automatically detected at runtime, read-only
*/
supportsUnknownElements: boolean;/**
* Describes the type of html5 object
* 'default' for basic version, 'default print' for print version
* Read-only property
*/
type: string;/**
* Main html5shiv object exposed globally and via module exports
*/
interface HTML5Shiv {
elements: string | string[];
version: string;
shivCSS: boolean;
shivMethods: boolean;
supportsUnknownElements: boolean;
type: string;
shivDocument(ownerDocument?: Document): Document;
createElement(nodeName: string, ownerDocument?: Document): Element;
createDocumentFragment(ownerDocument?: Document): DocumentFragment;
addElements(newElements: string | string[], ownerDocument?: Document): void;
shivPrint?(ownerDocument?: Document): Document; // Only available in print version
}
/**
* Configuration options that can be set before library initialization
*/
interface HTML5ShivOptions {
elements?: string | string[];
shivCSS?: boolean;
shivMethods?: boolean;
}The
html5shiv-printshiv.jshtml5shiv:applyElementremoveNodeThe library includes built-in error handling for various edge cases:
createElementcreateElementhtml5.shivMethods = falseBasic HTML5 Document Structure:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My HTML5 Site</title>
<!--[if lt IE 9]>
<script src="html5shiv.js"></script>
<![endif]-->
<style>
header, nav, main, article, section, aside, footer {
display: block; /* Already handled by html5shiv CSS */
}
</style>
</head>
<body>
<header>
<h1>Site Title</h1>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>
</header>
<main>
<article>
<header>
<h2>Article Title</h2>
<time datetime="2023-01-15">January 15, 2023</time>
</header>
<section>
<p>Article content goes here...</p>
</section>
</article>
<aside>
<section>
<h3>Related Links</h3>
<nav>
<ul>
<li><a href="/related1">Related Article 1</a></li>
<li><a href="/related2">Related Article 2</a></li>
</ul>
</nav>
</section>
</aside>
</main>
<footer>
<p>© 2023 My Company</p>
</footer>
</body>
</html>Custom Element Support:
<script>
// Add support for custom elements before loading html5shiv
window.html5 = {
elements: 'abbr article aside audio canvas details figcaption figure footer header hgroup mark nav section summary time video my-custom-element another-element'
};
</script>
<!--[if lt IE 9]>
<script src="html5shiv.js"></script>
<![endif]-->
<!-- Now you can use custom elements -->
<my-custom-element>
<another-element>Custom content</another-element>
</my-custom-element>Dynamic Element Addition:
// Add new elements dynamically after library is loaded
html5.addElements(['dialog', 'details', 'summary'], document);
// Now these elements will work properly in IE
var dialog = document.createElement('dialog');
var details = document.createElement('details');
var summary = document.createElement('summary');