views:

619

answers:

4

I want a whole block to be centered in its parent, but I want the contents of the block to be left aligned.

Examples serve best

On this page :

http://yaml-online-parser.appspot.com/?yaml=%23+ASCII+Art%0d%0a---+%7c%0d%0a++%5c%2f%2f%7c%7c%5c%2f%7c%7c%0d%0a++%2f%2f+%7c%7c++%7c%7c%5F%5F%0d%0a&type=python

the ascii art should be centered (as it appears) but it should line up and look like "YAML".

Or this :

http://yaml-online-parser.appspot.com/?yaml=%3f+-+Detroit+Tigers%0d%0a++-+Chicago+cubs%0d%0a%3a%0d%0a++-+2001-07-23%0d%0a%0d%0a%3f+%5b+New+York+Yankees%2c%0d%0a++++Atlanta+Braves+%5d%0d%0a%3a+%5b+2001-07-02%2c+2001-08-12%2c%0d%0a++++2001-08-14+%5d%0d%0a

the error message should all line up as it does in a console.

Possible? Hard? Easy?

A: 

If I understand you well, you need to use to center a container (or block)

margin-left: auto;
margin-right: auto;

and to left align it's contents:

text-align: left;
eKek0
so, what do I put on my <pre> and <code>? I've tried variations on that and failed.
Paul Tarjan
did you try using a css class?
eKek0
also, you can use a container div for >pre>
eKek0
i'm trying a container div, and the only way to make it work is with fixed width (which I don't want).
Paul Tarjan
+1  A: 
<div>
    <div style="text-align: left; width: 400px; border: 1px solid black; margin: 0 auto;">
         <pre>
Hello
Testing
Beep
         </pre>
    </div>
</div>
Amber
can I put a <code> block in there?
Paul Tarjan
also, i don't want the width: 400px. possible without that?
Paul Tarjan
The problem is that a block-level element will expand to fill the greatest width possible unless you put a limit on it.
Amber
so, what I want to do is impossible?
Paul Tarjan
It could be that someone knows a secret that I'm not aware of, but to my knowledge, yes.
Amber
I agree with Dav. Perhaps, you might take a look at tinyMCE or another rich text-editor that allows more customization than the standard HTML textarea. Unfortunately, you might end up spending a lot of time with a hackish result. Good luck though!
mkelley33
A: 

Normally you should use margin: 0 auto on the div as mentioned in the other answers, but you'll have to specify a width for the div. If you don't want to specify a width you could either (this is depending on what you're trying to do) use margins, something like margin: 0 200px; , this should make your content seems as if it's centered, you could also see the answer of Leyu to my question

Waleed Eissa
perfect. see it in action at the links. thanks!
Paul Tarjan
Glad this helped :)
Waleed Eissa
sadly your solution creates an overflow with a forced horizontal scrollbar. Adding overflow: hidden to the parent element isn't good since my output might be long enough to warrant a scroll bar. Sorry :(
Paul Tarjan
Actually it's not my solution as I referred, but anyway, I don't get what you mean by overflow: hidden forcing scrollbars, it should hide contents not force scrollbars.
Waleed Eissa
The solution in your post causes a horizontal scrollbar since the content is actually shifted 50% to the right. This requires a overflow:hidden to remove which doesn't work for me.
Paul Tarjan
+2  A: 

Reposing working answer from other question: http://stackoverflow.com/questions/1232096/how-to-horizonatally-center-a-floating-element-of-a-variable-width/1232297#1232297

Assuming the element which is floated and will be centered is a div with an id="content" ...

<body>
<div id="wrap">
   <div id="content">
   This will be centered
   </div>
</div>
</body>

And apply the following CSS

#wrap {
    float: left;
    position: relative;
    left: 50%;
}

#content {
    float: left;
    position: relative;
    left: -50%;
}

Here is a good reference regarding that http://dev.opera.com/articles/view/35-floats-and-clearing/#centeringfloats

Paul Tarjan