/ Published in: JavaScript
Position of the element to center needs to be relative to the container.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function CenterInsideDiv(divContainer,elementToCenter) { var divContainerHeight = divContainer.height(); var elementToCenterHeight = elementToCenter.outerHeight(); var elementNewTop = divContainerHeight / 2 - elementToCenterHeight / 2; elementToCenter.css({ top: elementNewTop, left: 0 }); } // call function like this CenterInsideDiv($("#elementToCenterId").parent(), $("#elementToCenterId"));