Ben
Ben

Reputation: 2108

CSS - center everything within the body tag

I am trying to center everything within the tag on the page without adding another container within it. My situation doesn't allow me to change the markup that is being generated but I am able to edit the CSS. My overall objective is to create css that allows me to use the zoom tag in IE for the print css (to zoom out), but the way it is currently working, this creates a lot of white space on the right side and I'd like to make sure the content is always centered in the middle.

Upvotes: 13

Views: 53908

Answers (5)

Nathan
Nathan

Reputation: 1

Give your body tag a fixed width, then set margin to auto. Here is an example:

body {
  width: 70%;
  margin: auto;
}

Upvotes: 0

Shaurya Gupta
Shaurya Gupta

Reputation: 11

I don't know if this is what you want but I tried this code and use display: flex; and it worked:

 body {
    display: flex;
    background-color: #f4eae3;
    align-items: center;
    justify-content: center;
}

Upvotes: 0

Karim Oukara
Karim Oukara

Reputation: 2706

if you use primefaces ... , I recommend to you to add !important like this :

body { 
    width: 1280px;
    height: 1024px;
    margin: 0 auto !important;
}

Upvotes: 0

Dr. Frankenstein
Dr. Frankenstein

Reputation: 4704

If you want something like this - http://jsbin.com/emomi3/5/ ...

  body {
    position: relative;
    width: 100%;
    border: 1px solid red;
    display: block;
  }

  body > *  {
    margin: auto;
    width: 500px;
    border: 1px solid blue;
    display: block;
    overflow: auto;
  }

  body > script {
    display: none
  }

(The borders are just in for illustration purposes)

Upvotes: 2

jimr
jimr

Reputation: 11230

If you're ok with giving the body a fixed width, you can center it by giving it a width and a margin of auto for the left and right margins:

E.g:

body { width: 960px; margin: 0 auto; }

Upvotes: 24

Related Questions