views:

475

answers:

5
<div class="preview">
  <span class="center">This will be centered</div>
</div>

Preview has fixed width (120x120), but span may contain anything (image, text). How do I center it vertically and horizontally using jQuery? I looked up some snippets but they all center elements inside the 'body' not another element. I'd like to avoid use a 'plugin' for this if possible.

Many thanks!

A: 

Change your span to a div and try

<div class="preview">
  <div class="center">This will be centered</div>
</div>

.center {
    text-align: center;
}

Also note your code shows

<span class="center">This will be centered</div>

Which is a tag with a start of a span and and end of a div!

Rippo
That would only center the div horizontally on the page
James
A: 

Since you mentioned you're using jquery, i assume you would like to do this via javascript. You can add styles to elements in the DOM using Jquery. You can use

http://docs.jquery.com/CSS/css#properties

  $(.center).css({'display' : 'block', 'text-align' : 'center'});

Depending on the element, you may be able to center it without having to use text-align:center if you set the margin to

margin: 0 auto 0 auto

This will set the margin on the top and bottom to zero, and auto on the left and right, this can be used to center the block element inside of another block element.

To center an element vertically in jquery you can use this

http://cool-javascripts.com/jquery/vertical-alignment-of-contents-inside-an-element-using-jquery.html

    function ($) {
    $.fn.vAlign = function(container) {
        return this.each(function(i){
    if(container == null) {
       container = 'div';
    }
    var paddingPx = 10; //change this value as you need (It is the extra height for the parent element)
    $(this).html("<" + container + ">" + $(this).html() + "</" + container + ">");
    var el = $(this).children(container + ":first");
    var elh = $(el).height(); //new element height
    var ph = $(this).height(); //parent height
    if(elh > ph) { //if new element height is larger apply this to parent
        $(this).height(elh + paddingPx);
        ph = elh + paddingPx;
    }
    var nh = (ph - elh) / 2; //new margin to apply
    $(el).css('margin-top', nh);
        });
     };
})(jQuery);
ThinkBohemian
+2  A: 

Since you don't mind using jQuery, you can use the Center Element Plugin

It's as simple as doing:

$(".preview").center();
James
A: 

You can use a CSS only solution (ignoring IE)

<div class="preview" style="display:table-cell;vertical-align:middle;text-align:center;margin:0 auto"> <span class="center">This will be centered</div> </div> using display:block will also work but only for horizontal alignment, for vertical alignment either you will need to emulate a table as per the above code or alternatively use JavaScript.

yannis
A: 

The latest jQuery UI has a position component:

$("#myDialog").position({
   my: "center",
   at: "center",
   of: window
});

Doc: http://jqueryui.com/demos/position/
Get: http://jqueryui.com/download

Hugo