How to align a DIV to the center of a page?

Let’s say you want your main wrapper div to be centered. It’s practiced widely so if you don’t know how I am going to show you the easiest way. for this example we are using embedded style rules not external Cascading Style Sheet (CSS). Your embedded style rules are placed within the head tag between ‘<style>’ & ‘</style>’ tags.

Example:

<html>

<head>

<title>Aligning DIV to the Center</title>

<style type="text/css">

#wrapper {

     width: 980px; margin: 0 auto; /* it is same as saying top-0 bottom-0 left-auto right-auto | This alone aligns your div to the center */

}

</style>

</head>

<body>

<div id="wrapper">

    <h2>This is the main wrapper div which is now aligned to the center</h2>

</div>

</body>

</html>

This example uses ID Selector to define the css rules. Hope this helps you. Smiles…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s