{"id":341,"date":"2006-07-12T05:10:05","date_gmt":"2006-07-12T12:10:05","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=341"},"modified":"2006-07-12T05:10:05","modified_gmt":"2006-07-12T12:10:05","slug":"floatcenter","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/floatcenter\/","title":{"rendered":"float:center"},"content":{"rendered":"<p>You wish&#8230;<\/p>\n<p>So many work arounds to center divs, that I was almost getting to the point where I was about to start selling black t-shirts with the words<\/p>\n<p><strong>float:center<\/strong><\/p>\n<p>If you&#8217;re a web developer, web designer, and you have to deal with CSS, and CSS not working properly on IE (cause of it&#8217;s stupid box model conception and endless ilogic behaviour bugs) then I&#8217;m sure you have always wanted to be able to do something like &#8220;float:center&#8221;<\/p>\n<p>I don&#8217;t really get why the people that made the CSS standards, didn&#8217;t include stupid float:center&#8230; there&#8217;s float:left, float:right&#8230; it&#8217;s only obvious. And worst of all, you know centering is one very important thing&#8230;<\/p>\n<p>so we have to go around doing stuff like<\/p>\n<p>margin:0 auto;<\/p>\n<p>but that won&#8217;t really work on all occasions, you have to think of the size of the parent elements too, so that the auto margins will work correctly.<\/p>\n<p>The other day a friend that&#8217;s a graphic designer even came up with his own trick of using percentages to add correct margins to center divs.<\/p>\n<p>But look no more (at least until they decide to give us float:center), if you use Prototype, maybe you also use Scriptaculous&#8230;<\/p>\n<p>Just add this to your effects.js file:<\/p>\n<p><code><br \/>\nEffect.Center = function(element)<br \/>\n{<br \/>\n    try<br \/>\n    {<br \/>\n        element = $(element);<br \/>\n    }<br \/>\n    catch(e)<br \/>\n    {<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    var my_width  = 0;<br \/>\n    var my_height = 0;<\/p>\n<p>    if ( typeof( window.innerWidth ) == 'number' )<br \/>\n    {<\/p>\n<p>        my_width  = window.innerWidth;<br \/>\n        my_height = window.innerHeight;<br \/>\n    }<br \/>\n    else if ( document.documentElement &amp;&amp; ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )<br \/>\n    {<\/p>\n<p>        my_width  = document.documentElement.clientWidth;<br \/>\n        my_height = document.documentElement.clientHeight;<br \/>\n    }<br \/>\n    else if ( document.body &amp;&amp; ( document.body.clientWidth || document.body.clientHeight ) )<br \/>\n    {<\/p>\n<p>        my_width  = document.body.clientWidth;<br \/>\n        my_height = document.body.clientHeight;<br \/>\n    }<\/p>\n<p>    element.style.position = 'absolute';<br \/>\n    element.style.display  = 'block';<br \/>\n    element.style.zIndex   = 99;<\/p>\n<p>    var scrollY = 0;<\/p>\n<p>    if ( document.documentElement &amp;&amp; document.documentElement.scrollTop )<br \/>\n    {<br \/>\n        scrollY = document.documentElement.scrollTop;<br \/>\n    }<br \/>\n    else if ( document.body &amp;&amp; document.body.scrollTop )<br \/>\n    {<br \/>\n        scrollY = document.body.scrollTop;<br \/>\n    }<br \/>\n    else if ( window.pageYOffset )<br \/>\n    {<br \/>\n        scrollY = window.pageYOffset;<br \/>\n    }<br \/>\n    else if ( window.scrollY )<br \/>\n    {<br \/>\n        scrollY = window.scrollY;<br \/>\n    }<\/p>\n<p>    var elementDimensions = Element.getDimensions(element);<\/p>\n<p>    var setX = ( my_width  - elementDimensions.width  ) \/ 2;<br \/>\n    var setY = ( my_height - elementDimensions.height ) \/ 2 + scrollY;<\/p>\n<p>    setX = ( setX <\/p>\n<p>This will center your div if you do something like this on Javascript to the div in question:<\/p>\n<p><code><br \/>\nEffect.Center('idOfMyDiv')<br \/>\n<\/code><\/p>\n<p>Try it, it works.<br \/>\nYou might want to hack the function in case you don't want this effect to center vertically.<\/p>\n<p>Also, if you want to keep centering the div even on window resize or scroll, you'll have to add<br \/>\ncallbacks to  window.onresize and window.onscroll<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/static.flickr.com\/63\/186374336_4f817d0770.jpg?w=640\" \/><\/p>\n<p>And remember, Linux Rules.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You wish&#8230; So many work arounds to center divs, that I was almost getting to the point where I was about to start selling black t-shirts with the words float:center If you&#8217;re a web developer, web designer, and you have to deal with CSS, and CSS not working properly on IE (cause of it&#8217;s stupid [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15,30,33,68],"tags":[],"class_list":["post-341","post","type-post","status-publish","format-standard","hentry","category-code","category-geeklife","category-gubatron","category-random-stuff"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-5v","jetpack-related-posts":[{"id":987,"url":"https:\/\/www.gubatron.com\/blog\/geek-t-shirt-collection-3-css-protest-wedoit4youcom\/","url_meta":{"origin":341,"position":0},"title":"Geek T-Shirt Collection #3 &#8211; CSS Protest \/ wedoit4you.com","author":"gubatron","date":"November 22, 2008","format":false,"excerpt":"wedoit4you.com was one of my personal efforts from school, with it I learned a lot about web development, technology, news, and the tech industry since it was a blog before the term was even invented. During those times, I was learning CSS, and I had to learn about centering elements,\u2026","rel":"","context":"In &quot;Geeklife&quot;","block_context":{"text":"Geeklife","link":"https:\/\/www.gubatron.com\/blog\/category\/geeklife\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3616,"url":"https:\/\/www.gubatron.com\/blog\/codejava-argb_8888-pixel-abstraction\/","url_meta":{"origin":341,"position":1},"title":"[CODE\/JAVA] ARGB_8888 Pixel Abstraction","author":"gubatron","date":"December 4, 2016","format":false,"excerpt":"This is one of the ways you can decode Pixel colors out of the integers you get from Android Pixels. ARGB_8888, stands for Alpha, Reg, Green, Blue. The 8's stand for the number of bits per channel. In Android, signed int's are used to represent pixel's alpha\/color information. Since Android's\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":913,"url":"https:\/\/www.gubatron.com\/blog\/how-to-filter-logs-in-lighttpd\/","url_meta":{"origin":341,"position":2},"title":"How to filter logs in lighttpd","author":"gubatron","date":"October 27, 2008","format":false,"excerpt":"I usually don't keep lighttpd access logs turned on to avoid writing for every read, but there are times when you need to monitor what's going on, and you'd like to have a high signal-to-noise ratio so it might be convenient to ignore all requests to .gif, .png, .jpg, .css,\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.lighttpd.net\/light_logo.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":310,"url":"https:\/\/www.gubatron.com\/blog\/david-blaine-under-water-at-the-lincoln-center-in-new-york\/","url_meta":{"origin":341,"position":3},"title":"David Blaine Under Water at the Lincoln Center in New York","author":"gubatron","date":"May 4, 2006","format":false,"excerpt":"There's also a Picture Gallery Click on Read more to see credits Shot on a Cannon PowerShot SD110 (so bear with me until I get a dv camera) Edited with iMovie (so bear with me until I get the hang of Final Cut Pro, the subtitles could've been better, but\u2026","rel":"","context":"In &quot;Diary&quot;","block_context":{"text":"Diary","link":"https:\/\/www.gubatron.com\/blog\/category\/diary\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1051,"url":"https:\/\/www.gubatron.com\/blog\/jquery-tag-wrapping\/","url_meta":{"origin":341,"position":4},"title":"jQuery tag wrapping","author":"gubatron","date":"December 19, 2008","format":false,"excerpt":"Many times you'll be working on something in HTML that could be long and repetitive, and then for some reason you need to edit the entire thing to wrap each of the tags on some other tags, a common example would be to link many elements. Take this example, there's\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":308,"url":"https:\/\/www.gubatron.com\/blog\/useful-firefox-extensions\/","url_meta":{"origin":341,"position":5},"title":"Useful Firefox extensions","author":"gubatron","date":"May 2, 2006","format":false,"excerpt":"Here's a page I've been meaning to do a long time ago, if you're a web developer you might want to bookmark it so that next time you set up Firefox +1.5 you'll have direct links to all the web developer tools you'll ever need. Web Developer Extension (Best extension\u2026","rel":"","context":"In &quot;Gubatron&quot;","block_context":{"text":"Gubatron","link":"https:\/\/www.gubatron.com\/blog\/category\/gubatron\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=341"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/341\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}