English 中文(简体)
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 not another element. I d like to avoid use a plugin for this if possible.

Many thanks!

最佳回答

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

It s as simple as doing:

$(".preview").center();
问题回答

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 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.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

Drop down background url in Safari Issue

selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

CSS specific for Safari

How do you target specifically safari in css with styles?

Aligning textarea in a form

Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

CSS problem with page footer

I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...

热门标签