home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Float: Center (JS Fix)

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2612
Discussions: 200,996, Posts: 2,379,948, Members: 246,364
Old February 22nd, 2008, 12:46 PM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Dec 2007
Posts: 20
Float: Center (JS Fix)

So, all of us CSS junkies have tried to center floated elements within a parent container that can have a fixed or percentage width. And of course, who wants to set an absolute width on their floated elements? That’s great if you know how wide that div needs to be, but I wanted to create reusable classes that didn’t care about their width and still would stay centered, as well as float.

The JS does things like compare the width of all the floated elements to the width of the window (or parent container) to make sure the elements fit where they are suppose to. And it re-centers the elements when the user resizes their browser window.

EDIT: Tested in IE 7.0 AND FF 2.0

JAVASCRIPT CODE:
Code:
<script type="text/javascript" language="javascript">
	//I have this code-block in the actual page using the function
	//Repos the search div to the center of the page
	window.onresize = setWidth;
	
	//Initiate functions	
	function init(){
		//Pos the search div to the center of the page
		//Floated box name, container for all boxes, dynamic inner-container, center to window or parent container?
		makeCenter('searchBox', 'searchContainer', 'searchDynamicWidthBox', 'window');           
	}
	
	//Run on window resize
	function setWidth(){
		makeCenter('searchBox', 'searchContainer', 'searchDynamicWidthBox', 'window');           
	}
	
	//Causes the init funtion to load on page load 
	function addLoadEvent(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			}
		}
	}
	addLoadEvent(init);  

	
	//I have this funtion in a seperate JS functions page
	//Page center floated elements
	function makeCenter(boxName, boxContainer, boxDynamic, centerIn){        
		var aryTable = document.getElementsByTagName("div");
		var aryBoxes = [];
		var minWidth = 0;
		
		//Figure out the width of each of the floats that need to be centered
		for(var i=0; i<aryTable.length; i++){  
			if(aryTable.item(i).getAttribute( 'name' ) == boxName ){    
				minWidth = minWidth + Sys.UI.DomElement.getBounds(aryTable.item(i)).width;               
			}
		} 
		
		//Figure out the container width
		containerWidth = Sys.UI.DomElement.getBounds(document.getElementById(boxContainer)).width;
		
		//Figure out the window width
		//Should work with most browsers. IE has problems with window.innerWidth
		//Also works in different rendering modes (quirks vs strict)
		if(typeof(window.innerWidth) == 'number'){
			windowWidth = window.innerWidth;
		} else if( document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		} else if( document.body && document.body.clientWidth){
			windowWidth = document.body.clientWidth;
		}      
				
		//Make the child dynamic width box only as wide as its parent container
		if(minWidth > containerWidth){
			minWidth = containerWidth;    
		}
							  
		//Try to center in page
		//If we can't find the window width center within parent container
		if(typeof(windowWidth) == 'number' && windowWidth > 0 && centerIn == 'window'){
			marginPos = (windowWidth - minWidth) / 2;
		} else {
			marginPos = (containerWidth - minWidth) / 2;
		}             
		
		//Set the width of the dynamic centering box
		document.getElementById(boxDynamic).style.width = minWidth + "px";
		
		//Set the margin of the dyamic centering box (centers the floated elements)
		document.getElementById(boxDynamic).style.marginLeft = marginPos  + "px";
	}
</script>
And the HTML code
Code:
<div class="centerContainer" id="searchContainer">
	<div id="searchDynamicWidthBox" class="centerDynamic" style="">
		<div class="centerFloat" name="searchBox">
			Centered content floated block 1
		</div>
		<div class="centerFloat" name="searchBox">
			Centered content floated block 2
		</div>
		<div class="centerFloat" name="searchBox">
			Centered content floated block 3
		</div>
	</div>        
</div>
And the CSS code
Code:
.centerContainer
{
	width: 100%;
	float: left;
	text-align: center;
}
.centerFloat
{
	float: left;
	padding: 5px 5px;
}
.centerDynamic
{
	float: left; 
	margin-left: -700px; 
	width: 700px; 
	border: 1px solid #CCCCCC; 
	position: relative;
}
I hope this helps someone, as I looked for a while online and couldn't find any cross-browser solutions. It's not perfect, but works for what I needed it to do, and should be able to be expanded to work for whatever. You know of a better solution? Post it! (Please)


-ng217
new_guy217 is offline   Reply With Quote
Old February 25th, 2008, 09:21 AM     #2 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,083
Send a message via AIM to sr71000
easier way that I know of, in the css, just put
Code:
margin-left:auto;
margin-right:auto;
Haven't tested too thoroughly though...
sr71000 is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
Media Center Pc Shoegoo General Gaming Discussion 5 October 17th, 2005 10:14 AM
Abortion float freaks out Terminal23 IMO Community 91 August 26th, 2005 08:34 PM
Stereo X2 plus center possible? troublesome '03 Multimedia and Audio 1 July 3rd, 2004 11:08 PM
Bowling center Epidemic IMO Community 19 February 17th, 2004 09:28 AM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3092)
Charges against non-tippers dropped.. (22)
Foxconn Blackops x48 MoBo (5)
Health Care Rationing (16)
Nvidia GTX 260 problem (14)
Delete an OS (17)
Laptop with wireless problem. (13)
Wireless Televisions. (12)
windows vista security holes (19)
CPU fan stops spinning randomly (11)
Regular Build (11)
Point and Shoot Camera Suggestions. (9)
[F@H SPAM 11/16/09] ! 1/2 months to.. (41)
windows 7 problem (7)
Recent Discussions
add ram to existing (2)
Need help getting speakers to work (2)
Nvidia GTX 260 problem (14)
Laptop with wireless problem. (13)
Point and Shoot Camera Suggestions. (9)
Is the PSU I received dead? (16)
FreeAgent drive software not x64 comp.. (1)
Intel 5100 AGN issues fixed yet? (28)
Foxconn Blackops x48 MoBo (5)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (41)
Print spooler problem (17)
Q9650 vs. Q9550 (2)
Desktop Calendar Application (2)
Looking for new motherboard (1)
soundmon.exe (8)
Jedi Academy Problem (3)
Can a page file be "too big".. (1)
Size after cutting 700Mb file is 2.5 .. (0)
Delete an OS (17)
windows vista security holes (19)
updating BIOS via winflash, claims fi.. (1)
New Server Configuration Suggestions (0)
cheap gaming laptop? (12)
Unallocated Space (2)
help me pls laptop just stopped worki.. (1)


All times are GMT -4. The time now is 11:56 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28