29

I am confused about how can I force my div element to be center (vertically and horizontally) at my page (mean which way or ways for cross browser compatibility)?

Dharman
  • 30,962
  • 25
  • 85
  • 135
SilverLight
  • 19,668
  • 65
  • 192
  • 300

3 Answers3

70

There are many methods :

  1. Center horizontal and vertical align of an element with fixed measure

CSS

 <div style="width:200px;height:100px;position:absolute;left:50%;top:50%;
margin-left:-100px;margin-top:-50px;">
<!–content–>
</div> 

2 . Center horizontally and vertically a single line of text

CSS

<div style="width:400px;height:200px;text-align:center;line-height:200px;">
<!–content–>
</div>  

3 . Center horizontal and vertical align of an element with no specific measure

CSS

<div style="display:table;height:300px;text-align:center;">
<div style="display:table-cell;vertical-align:middle;">
<!–content–>
</div>
</div>  

Vijay Chavda
  • 826
  • 2
  • 15
  • 34
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
4

This blog post describes two methods of centering a div both horizontally and vertically. One uses only CSS and will work with divs that have a fixed size; the other uses jQuery and will work divs for which you do not know the size in advance.

I've duplicated the CSS and jQuery examples from the blog post's demo here:

CSS

Assuming you have a div with a class of .classname, the css below should work.

The left:50%; top:50%; sets the top left corner of the div to the center of the screen; the margin:-75px 0 0 -135px; moves it to the left and up by half of the width and height of the fixed-size div respectively.

.className{
    width:270px;
    height:150px;
    position:absolute;
    left:50%;
    top:50%;
    margin:-75px 0 0 -135px;
}

jQuery

$(document).ready(function(){
    $(window).resize(function(){
        $('.className').css({
            position:'absolute',
            left: ($(window).width() - $('.className').outerWidth())/2,
            top: ($(window).height() - $('.className').outerHeight())/2
        });
    });
    // To initially run the function:
    $(window).resize();
});

Here's a demo of the techniques in practice.

Greg
  • 33,450
  • 15
  • 93
  • 100
  • 2
    What if the OP doesn't need to use jquery ? – Saurabh Gokhale Mar 24 '11 at 15:25
  • There is an example with CSS, right above the jQuery example. The jQuery example could also be modified to use straight javascript if the OP does not want to use jQuery, and needs to center divs for which the size is not known in advance. – Greg Mar 24 '11 at 15:26
1

This isn't as easy to do as one might expect -- you can really only do vertical alignment if you know the height of your container. IF this is the case, you can do it with absolute positioning.

The concept is to set the top / left positions at 50%, and then use negative margins (set to half the height / width) to pull the container back to being centered.

Example: http://jsbin.com/ipawe/edit

Basic CSS:

#mydiv { 
    position: absolute;
    top: 50%;
    left: 50%;
    height: 400px;
    width: 700px;
    margin-top: -200px; /* -(1/2 height) */
    margin-left: -350px; /* -(1/2 width) */
  }
RussellUresti
  • 6,211
  • 4
  • 28
  • 26
  • Of course, if you don't know the height of your container, you can always use JavaScript to get the computed height, and then set your margins using JS as well. – RussellUresti Mar 24 '11 at 15:20