Click here to Skip to main content
15,905,008 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In an attempt to get content on my page to center horizontally (it was hugging the left corner), I added this CSS:

body {
    width:80%;
    margin-left:auto;
    margin-right:auto;
}


This improves matters by adding some margin ("dead space") on the left of the page, but the content is still left-centric. How can I get the content to really center, rather than just begrudgingly move a little to the right?
Posted
Updated 11-Sep-15 7:43am
v2
Comments
Sergey Alexandrovich Kryukov 11-Sep-15 17:09pm    
It makes no sense for body. What do you want to achieve?
—SA

Here is the hint for you: this trick centers the element, not children of element. It makes sense for div, and not for body.
Let's say you want to place a poem. It requires that the strophes was left-aligned, as always, but you need to create a bounding column around the strophes, and the column should have equal margins on left and right. As you don't know how much space is available and required, you use auto. It makes sense for the element which width is defined by its content.

You need to do one important step: stop mixing up elements and content of elements.

I have no idea what would you expect doing it on page, but perhaps all you need is some fixed padding on left and right, better be in relative em units, not absolute pixels.

[EDIT]

Another approach would be:
HTML
<html>
   <head>
      <style>
         div.body {
            width: 80%;
            margin-left:auto;
            margin-right:auto;
         }
      </style>
   </head>
<body>

<div class="body">
Put here what used to be your page's content
</div>

</body>
</html>

Are you getting the idea? It certainly works, but I don't see whole lot of practical sense in it. Trust me, just use some 3em for margin-left and margin-right.

—SA
 
Share this answer
 
v2
Comments
B. Clay Shannon 11-Sep-15 17:44pm    
No, I don't want everything to center by itself. I want everything to be aligned left, but the whole "box" in which it all fits should be cented. IOW, not like this:

Bla
Bla Bla

...but like this:

Bla
Bla Bla

Well, it doesn't seem to let me show what I mean, but I simply want the left edge to be straight vertically.
Sergey Alexandrovich Kryukov 11-Sep-15 18:00pm    
Perfect. This is what can be done on a div. A page cannot have 80% width. How so? The page content flows and takes all room left for its width. I'm telling you, just add left and right padding to the page, say, 3 em.
Please see my update to the answer, after [EDIT].
—SA
The page body is basically a rectangle which contains your page.
Your css simply says that this rectangle is 80% of browser window width and that it is centred horizontally.
You aim the wrong target.

You need to apply the centring to each element of the page.
If you use paragraphes, you need something like:
CSS
p {
    text-align:center;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900