jQuery Dead Centre

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
A lot of people have this problem and I've just recently saw a thread about this, but dead-centering elements in HTML seems to be a struggle when it shouldn't be so I decided to write a little work-around for it.

There are probably many of these sorts of scripts and I've also saw work-arounds where people will wrap the element in 2 divs that will position it from the top and the 2nd div will position it from the left.

This script simply takes every element with the class of 'deadcentre' then it will take it's parent element, get the height and width of it, subtract the outer-height and outer-width from the parent elements and then divide by 2 to get it slap bang in the centre.

Script below:
HTML:
<!DOCTYPE html>
<html>
    <head>
        <title>jQuery Dead Centre</title>
        <style type="text/css">
        #div1 {
            height: 500px;
            width: 700px;
            background: #dedede;
        }
       
        #div2 {
            height: 300px;
            width: 450px;
            background: #de17fd;
        }
        </style>
    </head>
   
    <body>
        <div id="div1">
            <div id="div2" class="deadcentre"></div>
        </div>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript">
        $(function(){
            $('.deadcentre').each(function(i, a) {
                $parent = $(a).parent();
                $(a).css({
                    position: "relative",
                    top    : ($parent.height() - $(a).outerHeight())/2,
                    left    : ($parent.width() - $(a).outerWidth())/2
                });
            });
        });
        </script>
    </body>
</html>

Before deadcentre:
HLn2iLE.png


After deadcentre:
b9RmUY8.png


Hope it helps, fuckers.

- Mark
 

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
Could achieve with CSS by using %'s but I guess this is a neat alternative, nice release.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Could achieve with CSS by using %'s but I guess this is a neat alternative, nice release.
That means creating new CSS rules each time, technically this is doing just that, but it applies the styling to the elements itself.

The only downsides are that you need to ensure the user has JavaScript enabled, but who doesn't nowadays? All the big websites rely on JavaScript.
 

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
That means creating new CSS rules each time, technically this is doing just that, but it applies the styling to the elements itself.

The only downsides are that you need to ensure the user has JavaScript enabled, but who doesn't nowadays? All the big websites rely on JavaScript.
True but it's not often you'll need to dead center an element often. I guess it's down to a personal preference, I prefer doing it in CSS but it makes no real difference tbh.
 

Nehalem

Aug 31, 2013
293
47
I'm kinda surprised that I didn't think of this, since I know jQuery pretty well... but know I do! It's a simple script too, easy to understand (if you've worked with jQuery before...) and were grateful to you for this.

Thanks m8 :3
 

Users who are viewing this thread

Top