See https://stackoverflow.com/a/11868159/14894786 for the W3C algorithm with a JSFiddle demo. 
See https://stackoverflow.com/a/11868159/14894786 for the W3C algorithm with a JSFiddle demo.
