Center 2.0

Created Wednesday 17th of September 2008 by Andreas Lagerkvist
Copyright © 2008 Andreas Lagerkvist (andreaslagerkvist.com)

Please have a look at the "Other Resources" for bug reports or further help on jQuery. Do not post bug reports or feature requests as comments to unrelated articles. If bug reporting on bugtracker.a-framework.org fails, e-mail me.

What it Does

This little pluggy centers an element on the screen using either fixed or absolute positioning. Can be used to display messages, pop up images etc.

How to Use

jQuery('#my-element').center(true); would center the element with ID 'my-element' using absolute position (leave empty for fixed).

Example

I should be fixed centered

The paragraph above and the paragraph beneath this one are centered. They should be in the middle of the viewport.

I should be absolutely centered

Example Code

HTML

<div id="jquery-center-example">
<
p>I should be fixed centered</p>

<
p>The paragraph above and the paragraph beneath this one are centeredThey should be in the middle of the viewport.</p>

<
p>I should be absolutely centered</p>
</
div>

JS

jQuery('#jquery-center-example p:first-child').center();
jQuery('#jquery-center-example p:last-child').center(true);

Source Code

jQuery.fn.center = function (absolute) {
    return 
this.each(function () {
        var 
jQuery(this);

        
t.css({
            
position:    absolute 'absolute' 'fixed'
            
left:        '50%'
            
top:        '50%'
            
zIndex:        '99'
        
}).css({
            
marginLeft:    '-' + (t.outerWidth() / 2) + 'px'
            
marginTop:    '-' + (t.outerHeight() / 2) + 'px'
        
});

        if (
absolute) {
            
t.css({
                
marginTop:    parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(), 
                
marginLeft:    parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
            });
        }
    });
};

Download

Plug-in

Requires

Random jQuery Plug-ins

  • Static to Dynamic Google Map

    This plug-in turns a static google map into a dynamic one by inspecting the attributes passed to the static map API. It's still in beta and doesn't su...

    Details

  • Removable File Upload

    This plug-in adds a "remove"-link next to input[type=file]:s that allows the user to remove a selected file from the input.

    Details

  • Image Zoom

    This plug-in makes links pointing to images open in the "Image Zoom". Clicking a link will zoom out the clicked image to its target-image. Click anywh...

    Details

More Plug-ins

Recent Comments

  1. AL on "How to Create a Restaurant Menu in Wordpress 3 using Custom Post Types and Taxonomies"

    @kamera - I've also changed this part:

    <?p...

  2. AL on "How to Create a Restaurant Menu in Wordpress 3 using Custom Post Types and Taxonomies"

    @kamera - The last bit of code should be put in th...

  3. kamera on "How to Create a Restaurant Menu in Wordpress 3 using Custom Post Types and Taxonomies"

    Hi there,

    Very helpful tutorial.

    It all makes se...

Page cached. Loaded in: 0.069 second(s).
Last DB change: 2012-02-07 02:59:50
Last file change: 2011-10-03 07:42:35
Cache created: 2012-02-08 03:04:30