CSS - center everything within the body tag
Asked Answered
F

5

13

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.

Frangos answered 23/6, 2010 at 16:19 Comment(1)
#6465092Carib
E
24

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; }
Ecdysis answered 23/6, 2010 at 16:23 Comment(0)
B
2

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)

Barns answered 23/6, 2010 at 16:26 Comment(1)
Why "position: relative;"?Foretoken
M
0

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

body { 
    width: 1280px;
    height: 1024px;
    margin: 0 auto !important;
}
Mere answered 20/2, 2013 at 13:21 Comment(0)
B
0

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;
}
Blockhouse answered 14/3 at 21:47 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Almire
D
0

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

body {
  width: 70%;
  margin: auto;
}
Dragoman answered 14/3 at 22:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.