MicroTut: Centering a Div Both Horizontally And Vertically

In: tutorial

10 Mar 2010

While building web page layouts, you’ve probably been faced with a situation where you need to center a div both horizontally and vertically with pure CSS. There are more than a few ways to achieve this, and in this MicroTut I am going to show you my favorite involving CSS and jQuery.

But first, the basics:

Horizontal centering with CSS

It is as easy as applying a margin to a div:

.className{
	margin:0 auto;
	width:200px;
	height:200px;
}

To center a div only horizontally, you need to specify a width, and an auto value for the left and right margins (you do remember the shorthand declarations in CSS don’t you?). This method works on block level elements (divs, paragraphs, h1, etc). To apply it to inline elements (like hyperlinks and images), you need to apply one additional rule – display:block.

Horizontal and vertical centering with CSS

Center a div both horizontally and vertically with CSS is a bit more tricky. You need to know the dimensions of the div beforehand.

.className{
	width:300px;
	height:200px;
	position:absolute;
	left:50%;
	top:50%;
	margin:-100px 0 0 -150px;
}

By positioning the element absolutely, we can detach it from its surroundings and specify its position in relation to the browser window. Offsetting the div by 50% from the left and the top part of the window, you have its upper-left corner precisely at the center of the page. The only thing we are left to do is to move the div to the left and to the top with half its width and height with a negative margin, to have it perfectly centered.

Horizontal and vertical centering with jQuery

As mentioned earlier – the CSS method only works with divs with fixed dimensions. This is where jQuery comes into play:

$(window).resize(function(){

	$('.className').css({
		position:'absolute',
		left: ($(window).width() - $('.className').outerWidth())/2,
		top: ($(window).height() - $('.className').outerHeight())/2
	});

});

// To initially run the function:
$(window).resize();

The functionality is inserted into a $(window).resize() statement, which is executed every time the window is resized by the user. We use outerWidth() and outerHeight(), because unlike from the regular width() and height(), they add the padding and the border width to the returned size. Lastly, we simulate a resize event to kick center the div on page load.

The benefit of using this method, is that you do not need to know how big the div is. The main disadvantage is that it will only work with JavaScript turned on. This however makes it perfect for rich user interfaces (such as facebook’s).

Be sure to share your favorite methods in the comment section.

Go to Source

3 Responses to MicroTut: Centering a Div Both Horizontally And Vertically

Avatar

sailor astra

March 11th, 2010 at 2:00 am

You pegged element V correctly. It is an inert gas. It's atomic radius being the smallest, gave it away as Neon.

Elemeny W is not Fluorine, because Fluorine is a gas, and the clue stated that element W is solid. So, element W must be Iodine, the first solid among the halogens. Also, clue 7 implies that element W has more electron levels than X or T, and Iodine is in row 5 of the Periodic Table. So, HW is actually HI (hydrogen Iodide).

Element Y is most likely Aluminum. It forms Al2O3 and is the first metal in its family, Boron being a metalloid. It should be noted that Arsenic, Antimony, and Bismuth also form Y2O3 compounds, but these are also metalloids.

Element Z must be Nitrogen. It forms NH3 (Ammonia), and is a component of HNO3 (Nitric Acid), which is an important industrial acid. Nitrogen also has a lower ionization energy than element T (Sulfur).

Element T must be Sulfur. It forms H2S (Hydrogen Sulfide, fart gas, rotten egg odour), and is a component of H2SO4 (Sulfuric Acid), another important industrial acid. Sulfur has a greater ionization energy than element Z (Nitrogen), and has less electron levels than W (Iodine), and more electron levels than X (Carbon).

Lastly, element X must be Carbon. It forms CO2 (Carbon Dioxide) and CH4 (Methane, Marsh Gas), and has fewer electron levels than either T (Sulfur) or W (Iodine).

Avatar

David D

March 20th, 2010 at 9:11 am

http://www.w3.org/TR/html4/struct/global.html#h-7.5.3 explains.

has the list (follow the links to expand the %groups).

Avatar

oklatom

May 21st, 2010 at 9:32 am

"totaled out" sounds like the proper term. Also, in some states you can "buy" a "totaled out" vehicle and have a "re-furbished" title issued…contact your local dmv or secretary of state for more information….

Comment Form

About this blog

This blog delivers stylish and dynamic news for designers and web-developers on all subjects of design, ranging from: CSS, Ajax, Javascript, web design, graphics, typography, advertising & much more. Our goal is to help you communicate effectively on the web with an engaging website or functional interface.