Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Centering A Div


7 replies to this topic

#1 oxida

    Member [Level 1]

  • Kontributors
  • PipPipPipPip
  • 51 posts
  • Gender:Male
  • Location:Netherlands
  • Interests:cosmology, hi-tech, photoshop, php, webdesign, graphic design, downhill biking, swimming, coastguard.

Posted 30 June 2008 - 08:57 AM

Many people would like theire website in the center of the screen.
This is how to do that.

1. make a new css document.

2. Then place the following code in it.

#main-div {
	position: absolute;
	top: 50%;
	left: 50%;
	width: 70em;
	height: 50em;
	margin-top: -25em; /*always take 1/2 from the size of the height, they have to be negative*/;
	margin-left: -35em; /*always take 1/2 from the size of the width, they have to be negative*/
}

Then place in your html: the following code.

<div id="main-div">
content
</div>

This wil place the div box in the center of the screen, works mostly for any resolution depends on the size of your div box.

Hope this helped some people.

#2 Saint_Michael

    $p4m 0n j00 $h4m3 m3 0nc3 $p4m 0n m3 $h4m3 m3 7\/\/1c3

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 7,459 posts
  • Gender:Male
  • Location:9r33|\| 399$ 4|\|D 5P4/\/\
  • Interests:$p4m 0n j00 $h4m3 m3 0nc3 $p4m 0n m3 $h4m3 m3 7\/\/1c3
  • myCENT:71.24

Posted 16 October 2008 - 02:29 AM

I would think that their be another way to do because using a negative number in order to center a website is a bit sloppy and HTML 4. Especially on the fact you would have to make the center fluid with all resolutions and so it could get a bit tricky and so I think a cleaner solution like this example http://www.shadow-fox.net/tutorial/How-to-...a-Layout-in-CSS would prove more beneficial. However, depending on how wide you make your layout you might have to change the dimensions a bit on how to position the layout.

#3 jlhaslip

    Insert Custom Title Here

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,040 posts
  • Gender:Not Telling
  • Location:Linux, DOS and Windows…the good, the bad and the ugly
  • myCENT:81.07
  • Spam Patrol

Posted 16 October 2008 - 02:55 AM

The Original posting shows a technique for centering a div in the viewport both horizontally and vertically.
You need to know the exact dimensions of the div being centered, position it absolutely at top: 50% and left:50% with top and left negative margins exactly as the Topic Starter states.
The link you show will center the page horizontally only, and is a good method to do that, but if the intent is to center the div from top to bottom/left to right, use the Opening Poster's method.

#4 shadowx

    Live your life so that in death you may stand side by side with your gods. Not at their feet.

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1,674 posts
  • Gender:Male
  • Location:Essex, UK
  • Interests:Photography is a big interest, i have some photos up at my site, apex photographs (http://apex-photographs.com). Using my Lumix g1 to take the photos of course! <br /><br />Um computer games... photo editing and thats about it!
  • myCENT:68.57
  • Spam Patrol

Posted 16 October 2008 - 11:51 AM

The latest CSS trick ive found from a WYSIWYG editor (i only use it to make layouts! I then edit the code as its rubbish!) is to use:

margin-left: auto;
margin-right: auto;

Works perfectly no matter what size window or div!

#5 Forbez

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 662 posts

Posted 16 October 2008 - 04:02 PM

Great tutorial, nice and simple and good use of examples. Perhaps you could host a demonstration of the code in use?

View Postshadowx, on Oct 16 2008, 12:51 PM, said:

The latest CSS trick ive found from a WYSIWYG editor (i only use it to make layouts! I then edit the code as its rubbish!) is to use:

margin-left: auto;
margin-right: auto;

Works perfectly no matter what size window or div!
^^^ This guy makes a very valid point, when web coding myself I use that way. Not to divert the attention away from your tutorial though, but that way is simpler and easier.

#6 triplebtalk

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 210 posts
  • Gender:Male
  • Location:Australia
  • Interests:Forums, Blogs, Web Design, Graphic Design, Radio
  • myCENT:0.76

Posted 11 January 2009 - 03:41 PM

Or you could go:

margin: auto

I think that this method is ridiculous and it seems never to make sense to go into adjusting percentages etc.

#7 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 07 July 2009 - 08:05 PM

 I tried this:

margin: auto

or margin-left: auto

margin-right:auto

works great with Firefox but doesn't work with my IE 8 with default settings. 

this code:

#main-div { position: absolute; top: 50%; left: 50%; width: 70em; height: 50em; margin-top: -25em; /*always take 1/2 from the size of the height, they have to be negative*/; margin-left: -35em; /*always take 1/2 from the size of the width, they have to be negative*/}

work well with firefox and my IE 8.



#8 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 28 November 2009 - 08:07 PM

This is awesomeCentering A Div

Thanks, this was exactly what I was looking for.

 One thing, though, your code has an extra semi-colon at the end of the first comment.

-reply by David D Ochoa




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users