Return to Snippet

Revision: 53525
at November 25, 2011 21:44 by Neven


Initial Code
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"));

Initial URL

                                

Initial Description
Position of the element to center needs to be relative to the container.

Initial Title
Center element inside of a div container

Initial Tags
center

Initial Language
JavaScript