How to center your webpage horizontally and vertically using CSS

Ranked #4,848 in Internet, #256,280 overall

Centering a webpage layout

Let's say you've almost finished designing a new website, but now you want to make it look even better. Centering your website's layout is one way of achieving this - it will make your website look more aesthetically pleasing. Centering a layout horizontally is simple, but many people struggle with vertically centering a layout. This lens discusses a simple but effective CSS (cascading stylesheet) method that can be used to vertically center any website which has static content.

Centering your layout horizontally

If you only want to center your layout horizontally, that is simple: it's just a matter of adding


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

Centering your layout vertically is a bit trickier, but it is possible. This particular method only works for fixed layouts - in other words, you have to know in advance what the height and width of the container div will be. As such, this method isn't suitable for websites where the content is dynamically generated, such as blogs, but it could come in handy for static sites where you know what content will appear on each page.

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

It is also possible to do this with fluid measurements instead, if you prefer using percentages instead of pixel measurements. However, you still need to be aware that if your text is too long for the container div, it will spill out over the end of the div; no scrollbar will appear. Therefore, like the fixed layout example above, it should only be used for pages containing static content. Here is the code to do this:


#container {
background-color: #ffffff;
font-family:arial;
width: 50%;
height: 50%;
position: absolute;
top: 25%;
left: 25%;
}

Books about CSS from Amazon

If you'd like to learn more about Cascading Stylesheets (CSS), I'd recommend any of the books below.
Loading

CSS Poll

Loading poll. Please Wait...

About the author of this page

Loading

My other web-development related pages

If you enjoyed this page, check out the other Squidoo lenses that I have created.
Loading

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 »

Feeling creative? Create a Lens!