Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Tutorials
jQuery Dead Centre
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Markshall" data-source="post: 249057" data-attributes="member: 1872"><p>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.</p><p></p><p>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.</p><p></p><p>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.</p><p></p><p>Script below:</p><p>[HTML]<!DOCTYPE html></p><p><html></p><p> <head></p><p> <title>jQuery Dead Centre</title></p><p> <style type="text/css"></p><p> #div1 {</p><p> height: 500px;</p><p> width: 700px;</p><p> background: #dedede;</p><p> }</p><p> </p><p> #div2 {</p><p> height: 300px;</p><p> width: 450px;</p><p> background: #de17fd;</p><p> }</p><p> </style></p><p> </head></p><p> </p><p> <body></p><p> <div id="div1"></p><p> <div id="div2" class="deadcentre"></div></p><p> </div></p><p> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script></p><p> <script type="text/javascript"></p><p> $(function(){</p><p> $('.deadcentre').each(function(i, a) {</p><p> $parent = $(a).parent();</p><p> $(a).css({</p><p> position: "relative",</p><p> top : ($parent.height() - $(a).outerHeight())/2,</p><p> left : ($parent.width() - $(a).outerWidth())/2</p><p> });</p><p> });</p><p> });</p><p> </script></p><p> </body></p><p></html>[/HTML]</p><p></p><p>Before deadcentre:</p><p><img src="http://i.imgur.com/HLn2iLE.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>After deadcentre:</p><p><img src="http://i.imgur.com/b9RmUY8.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>Hope it helps, fuckers.</p><p></p><p>- Mark</p></blockquote><p></p>
[QUOTE="Markshall, post: 249057, member: 1872"] 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>[/HTML] Before deadcentre: [img]http://i.imgur.com/HLn2iLE.png[/img] After deadcentre: [img]http://i.imgur.com/b9RmUY8.png[/img] Hope it helps, fuckers. - Mark [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Tutorials
jQuery Dead Centre
Top