How to center your webpage horizontally and vertically using CSS
Ranked #4,848 in Internet, #256,280 overall
Centering a webpage layout
Centering your layout horizontally
margin: 0 auto;
to the CSS code for your container or wrapper div. So if, for example, your layout is enclosed in a div id called "container", the CSS code would be:
#container {
margin: 0 auto; /* to center the layout horizontally */
width: 950px; /* insert the width of your choice here */
background-color: #ffffff; /* white background */
}
You don't have to use a fixed-width for the container if you don't want to. A fluid measurement would work fine too, for example 75%, meaning the layout would stretch to 75% of the browser's viewport.
Centering your layout vertically
Firstly, you need to divide the container width by 2 and then add a minus to the front to find out what the measurement for the margin-left needs to be. For example, if your container div is going to be 750 pixels wide, the margin-left will be -375px. You need to do the same thing for the height, to find out what the margin-top should be. So if your container div is 500px high, the margin-top will be -250px. This is what the full CSS code for the container div will look like:
#container {
background-color: #ffffff;
font-family:arial;
width: 750px;
height: 500px;
margin-left: -375px;
margin-top: -250px;
position: absolute;
top: 50%;
left: 50%;
}
Centering your layout vertically using fluid measurements
#container {
background-color: #ffffff;
font-family:arial;
width: 50%;
height: 50%;
position: absolute;
top: 25%;
left: 25%;
}
Books about CSS from Amazon
CSS Poll
About the author of this page
My other web-development related pages
Would you like to join Squidoo?
Join Squidoo!
If you would like to create web pages like this one, why not join Squidoo? Click this link to get started!
Comments please!
Have you found the tips in this lens useful?
-
-
liam-goodacre
Apr 1, 2012 @ 2:45 pm | delete
- A more effective way to centre vertically and horizontally is to expand the left, top, right, bottom bounds to the rectangle you wish to centre within, then set your width/height and apply an auto margin ? No messing about with half size offsets, and the page will scroll properly if the viewing area is too small. Like so:
#container { position: absolute; left: 0; right: 0; top: 0; bottom: 0; width: 750px; height: 500px; margin: auto; }
-
-
-
goo2eyes
Jan 24, 2012 @ 3:23 pm | delete
- i bookmarked this lens and will use it as a reference. thanks for the useful info.
-
-
-
goo2eyes
Jan 24, 2012 @ 3:23 pm | delete
- i bookmarked this lens and will use it as a reference. thanks for the useful info.
-
-
-
djroll
Dec 24, 2011 @ 11:03 am | delete
- Thanks for sharing this information. I'll have to try it on my website.
-
-
-
Pastiche
Nov 15, 2011 @ 6:05 pm | delete
- I do like to tinker with CSS. Eons ago (no just a couple decades ...) I wrote in TeX to create formatting style macros for a publishing system based on SDML (an SGML, pre HTML/pre WWW markup standard). I still have that code love in my blood!
-
by WebaliciousGuides
Hi, my name is Victoria and I am a 28 year old woman from Norwich, England. Last year I started a new career as a web developer and I'm enjoying it a lot.... more »
- 16 featured lenses
- Winner of 18 trophies!
- Top lens » How to request a job application form via email
Explore related pages
- Create Stunning CSS Backgrounds with Images Create Stunning CSS Backgrounds with Images
- Learn CSS (Cascading Style Sheets) Learn CSS (Cascading Style Sheets)
- css code snippets to spice up your lenses css code snippets to spice up your lenses
- 10 advantages of CSS in web development 10 advantages of CSS in web development
- CSS software CSS software
- Cascading Style Sheets Cascading Style Sheets