Skip to content

Instantly share code, notes, and snippets.

@alvr
Created July 5, 2017 23:20
Show Gist options
  • Save alvr/f1326b8944acdd69b563652d86559cf4 to your computer and use it in GitHub Desktop.
Save alvr/f1326b8944acdd69b563652d86559cf4 to your computer and use it in GitHub Desktop.
(function() {
function center_el(tagName) {
var tags = document.getElementsByTagName(tagName), i, tag;
for (i = 0; i < tags.length; i++) {
tag = tags[i];
var parent = tag.parentElement;
// center an image if it is the only element of its parent
if (parent.childNodes.length === 1) {
// if there is a link on image, check grandparent
if (parent.nodeName === 'A') {
parent = parent.parentElement;
if (parent.childNodes.length != 1) continue;
}
if (parent.nodeName === 'P') parent.style.textAlign = 'center';
}
}
}
var tagNames = ['img', 'embed', 'object'];
for (var i = 0; i < tagNames.length; i++) {
center_el(tagNames[i]);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment