Centering vertically a web page

Last update on November 1, 2009 09:24 AM by jak58
Published by jak58

Centering vertically a web page







Centering vertically a web page; consider it as a waste time trying to achieve it with CSS. The most practical solution is to use JavaScript.

e .js file


Copy/paste the following script in a file named for example: center.js


function align()
{

	var lmt = document.getElementById('center);
	var container = document.documentElement;

	if(lmt && container)
	{
	    var containerHeight;
	    if (container.innerWidth)
	    {
            containerHeight = container.innerHeight;
		}
		else
		{
            containerHeight = container.clientHeight;
		}
	    var lmtHeight;
	    if (lmt.innerWidth)
	    {
            lmtHeight = lmt.innerHeight;
		}
		else
		{
            lmtHeight = lmt.offsetHeight;
		}
		var y = Math.ceil((containerHeight - lmtHeight) / 2);
		if(y < 0)
		{
			y = 0;
		}
		lmt.style.position = "relative";
		lmt.style.top = y + "px";
	}
	if (document.getElementById)
	{
		document.body.style.visibility = 'visible';
	}

}

function addevent(obj,evt,fn,capt){
	if(obj.addEventListener)
	{
		obj.addEventListener(evt, fn, capt);
		return true;
	}
	else if(obj.attachEvent)
	{
		obj.attachEvent('on'+evt, fn);
		return true;
	}
	else return false;
}

if (document.getElementById && document.getElementsByTagName)
{
	addevent(window, 'load', align, false);
	addevent(window, 'resize', align, false);
}

e .html file


For your page to be centered, you must call the .js file.
The page will be displayed as such:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
	<head>
		<!-- [...] -->
		<script type="text/javascript" src="center.js"></script>
	</head>
	<body>
		<div id="centering">

		<!Put the code of your webpage here. It will be centered -->
		
		</div>
	</body>
</html>


Implementation

The align() function defined in the .js file will retrieve the content that should be centered. In this example, the item having the ID [id = "center"] will be centered.

var lmt = document.getElementById('center ');

The same goes for the .html page, the contents found between the <div id="center"></div>, will be centered.

Note : This tip will only be effective if the user has javascript enabled in their browser.
Best answers for « Centering vertically a web page » in :
White background's problem in web pages Show Your web pages will be displayed more accurately : No background color, everything is white !!! (Like this image) To resolve this problem follow these steps : 1. Go to the "Control Panel" : Click on "Option accessibility" 2. In the...
Personalize web pages with GreaseMonkey Show Personalize web pages with GreaseMonkey What is GreaseMonkey? Example 1 Example 2 XPath Utilities removeElement() removeAttributeOfElement() setAttributeOfElement() injectCSS() Links What is GreaseMonkey? GreaseMonkey is an...
How can I save web pages on my computer? Show How can I save web pages on my computer? IE Google Chrome Firefox IE For Internet Explorer, click on the File menu. Click on "Save As" In the "Save as type" field, drop down the list and choose "Web Archive (Single...
Fonts used in web pages ShowFonts used in web pages How to solve this issue? Default fonts If you have chosen the perfect font that reflects the atmosphere of your website as well as the global idea that you want the user to get from it, you should be careful...
Displaying images in web browser ShowDisplaying images in web browser The Flash / Shockwave not displayed When surfing on the internet, you have surely come across small rectangles with red crosses replacing images on the web pages. This may be due to several images on...
Stopping animated images on Web pages ShowStopping animated images on Web pages Internet Explorer Mozilla Firefox Opera Ad blocking Animated images are annoying you and slow down the display of web pages you visit? Do not panic, you can stop. Internet Explorer In...
Download ABB Web Icon Extractor ShowABB Web Icon Extractor is a tool for converting images from web page icons. It supports image formats: jpg, png, bmp. The images will be converted to formats: ICO, ICL, JPG, PNG, BMP. Advantage Saving of the extracted icons as ICO, ICL, JPG, PNG,...
Managing images in HTML ShowHow do you display images on a web page? Having a few images on a website can make it more attractive and user-friendly. However, it is important not to go overboard, since images can take a long time to load, and in some cases can make a document...
Hyperlinks ShowIntroduction to anchors Hypertext links or hyperlinks (anchors) are HTML elements that, when clicked on, enable readers to visit a new address. Hyperlinked text is underlined by default. Hyperlinks are what connect web pages to one another. They...
Backgrounds ShowAdding a Background Image A background image for a web page can be set using the tag: Attribute Visual Effect BACKGROUND="image" Displays image as background bgcolor="name_of_Colour or #XXXXXX" Displays the given colour...