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 :
the ascii art should be centered (as it appears) but it should line up and look like "YAML".
Or this :
the error message should all line up as it does in a console.
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;
For those of us still working with older browsers, here's some extended backwards compatibility:
<div style="text-align: center;">
<div style="display:-moz-inline-stack; display:inline-block; zoom:1; *display:inline; text-align: left;">
Line 1: Testing<br>
Line 2: More testing<br>
Line 3: Even more testing<br>
</div>
</div>
_x000D_
Partially inspired by this post: https://stackoverflow.com/a/12567422/14999964.
THIS works
<div style="display:inline-block;margin:10px auto;">
<ul style="list-style-type:none;">
<li style="text-align:left;"><span class="red">?</span> YouTube AutoComplete Keyword Scraper software <em>root keyword text box</em>.</li>
<li style="text-align:left;"><span class="red">?</span> YouTube.com website <em>video search text box</em>.</li>
<li style="text-align:left;"><span class="red">?</span> YouTube AutoComplete Keyword Scraper software <em>scraped keywords listbox</em>.</li>
<li style="text-align:left;"><span class="red">?</span> YouTube AutoComplete Keyword Scraper software <em>right click context menu</em>.</li>
</ul>
</div>
Is this what you are looking for? Flexbox...
.container{_x000D_
display: flex;_x000D_
flex-flow: row wrap;_x000D_
justify-content: center;_x000D_
align-content: center;_x000D_
align-items: center;_x000D_
}_x000D_
.inside{_x000D_
height:100px;_x000D_
width:100px;_x000D_
background:gray;_x000D_
border:1px solid;_x000D_
}
_x000D_
<section class="container">_x000D_
<section class="inside">_x000D_
A_x000D_
</section>_x000D_
<section class="inside">_x000D_
B_x000D_
</section>_x000D_
<section class="inside">_x000D_
C_x000D_
</section>_x000D_
</section>
_x000D_
<div>
<div style="text-align: left; width: 400px; border: 1px solid black; margin: 0 auto;">
<pre>
Hello
Testing
Beep
</pre>
</div>
</div>
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
First, create a parent div
that centers its child content with text-align: center
. Next, create a child div
that uses display: inline-block
to adapt to the width of its children and text-align: left
to make the content it holds align to the left as desired.
<div style="text-align: center;">_x000D_
<div style="display: inline-block; text-align: left;">_x000D_
Centered<br />_x000D_
Content<br />_x000D_
That<br />_x000D_
Is<br />_x000D_
Left<br />_x000D_
Aligned_x000D_
</div>_x000D_
</div>
_x000D_
I've found the easiest way to centre and left-align text inside a container is the following:
HTML:
<div>
<p>Some interesting text.</p>
</div>
CSS:
P {
width: 50%; //or whatever looks best
margin: auto; //top and bottom margin can be added for aesthetic effect
}
Hope this is what you were looking for as it took me quite a bit of searching just to figure out this pretty basic solution.
Source: Stackoverflow.com