Feb 11

Simple and elegant vertical HTML centering.

All we need is jQuery (jquery.com). With simple snipet, we can center whatever we want inside any element. The idea is simple: move child element to vertical center with margin-top.

Everything you have to do is put this simple code into head of your html file.

<script type="text/javascript">
$(document).ready(function(){
    $('.vc').each(function(){
        var thish = $(this).height();
        var parenth = $(this).parent().height();
        var topmargin = (parenth - thish) / 2;
        $(this).css('margin-top', topmargin);
    });
});
</script>

This should work on all elementswith class .vc.

If you want it like function you should check this link.

One Response to “Vertical centering”

  1. art Says:

    All my life i’v been searching for manual of vertical centering like this one. THANKS, i owe you big one!

Leave a Reply

Spam Protection by WP-SpamFree