开发者

Centering element inside an element (jQuery)

<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开发者_StackOverflow中文版' not another element. I'd like to avoid use a 'plugin' for this if possible.

Many thanks!


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


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

It's as simple as doing:

$(".preview").center();


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);


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.


You could add your own function to jquery:

// Centers on div
jQuery.fn.center = function (div) {
this.css("position", "absolute");

var position = div.position();
this.css("top", Math.max(0, ((div.height() - this.outerHeight()) / 2) + position.top) + "px");

this.css("left", Math.max(0, ((div.width() - this.outerWidth()) / 2) + position.left) + "px");
return this;
};

Use:

$('#ajxload').center($('#mapWrapper')).show();

Took the concept from the code at Using jQuery to center a DIV on the screen


You could do it using CSS.
This post provides a good solution - http://mindthesemicolon.com/how-to-center-an-element-vertically-and-horizontally-using-css/
Basically:
1. Set the child to position absolute.
2. Set the parent to position relative.
3. Set child's left and top to 50%.
4. translate(-50%,-50%) to shift left and top by half the width of the child element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜