1

I know a lot of techniques out there for centering a div vertically, but I made this according to my thinking. I believe the concept is correct, but I'm not getting 100% results.

Here is the javascript

jQuery(document).ready(function(){
  var l=jQuery('.div1').height();
  var level=l/2;
  var bt=jQuery('.div2').height()/2;
  var val=level-bt;
  jQuery('.div2').css("margin-top",val);
});

CSS

.div1
{
  height:50px;
  background-color: red;
  position:relative;    
}
.div2
{
    border: 1px solid;
    background-color: orchid;
    text-decoration: none;
    letter-spacing: 1px;
    text-transform:uppercase;
    padding: 5px;
    position: absolute;
    right:5%;
    top:0%

}

HTML

<div class="div1"><div class="div2">vertical</div>
  1. I calculated the outer div height and calculating the center by dividing it by 2.
  2. Calculating the div center which should be centered, Here it is div2
  3. Now I reduce the half of the div2 height with half of div1 height by the result, if I give that as a margin-top, I should be able to get it to center vertically. Am I right? if not could some one explain to me?

Check the fiddle

xdumaine
  • 10,096
  • 6
  • 62
  • 103
sun
  • 1,598
  • 11
  • 26
  • 45

4 Answers4

1

Try this, You are missing to subtract padding

jQuery(document).ready(function(){
    var l=jQuery('.div1').height();
    var level=l/2;
    var bt=jQuery('.div2').height()/2;

    var val=level-bt-parseInt(jQuery('.div2').css('padding-top'));
    jQuery('.div2').css("margin-top",val);
});

Demo

Amit
  • 15,217
  • 8
  • 46
  • 68
0

Use outerHeight to ignore the padding.

jQuery(document).ready(function () {
    var l = jQuery('.div1').outerHeight();
    var level = l / 2;
    var bt = jQuery('.div2').outerHeight() / 2;
    var val = level - bt;
    jQuery('.div2').css("margin-top", val);
});

http://jsfiddle.net/DZxW2/1/

James Coyle
  • 9,922
  • 1
  • 40
  • 48
  • is outerHeight always a good practice even if we didn't have padding? – sun Sep 10 '13 at 11:46
  • @sun It depends on what the situation requires. `outerHeight()` gets the computed height of the element - height + padding + border - and can be given a parameter of true to also add the margin. `width()` will only get the width as defined by the CSS box model. – James Coyle Sep 10 '13 at 11:56
0

You're not taking into account the padding on div2, if you get the outerHeight you should have more joy: http://jsfiddle.net/CPrtZ/1/

jQuery(document).ready(function(){
    var l=jQuery('.div1').height()/2;
    var bt=jQuery('.div2').outerHeight()/2;
    var val=l-bt;
    jQuery('.div2').css("margin-top",val);
});
David MacCrimmon
  • 966
  • 1
  • 6
  • 19
0

I realise that this question is about how to do center a div vertically using jQuery, but it seems from the comments on this question that there is a mistaken belief that this cannot be done using only CSS. Here's a jsFiddle showing that it can be done. It works in all current browsers and IE8+.

HTML

<div class="div1">
    <div class="div2">vertically aligned while
        <br />height is not fixed but
        <br />based on content height</div>
</div>

CSS

body {
    display: table;
    width: 100%;
}
.div1 {
    height:200px;
    background-color: red;
    position:relative;
    display: table-cell;
    vertical-align: middle;
}
.div2 {
    border: 1px solid;
    background-color: orchid;
    text-decoration: none;
    letter-spacing: 1px;
    text-transform:uppercase;
    padding: 5px;
}
Mathijs Flietstra
  • 12,900
  • 3
  • 38
  • 67