You can use a combination of different text-align
and display
values to achieve that.
.outer {
text-align: center; /*center the inner inline block mainly*/
display: block; /*not needed here but if using nested <span>s*/
}
.inner {
text-align: left; /*reset the text to be left aligned*/
display: inline-block; /*the width of the box based on content length*/
}
<div class="outer">
<div class="inner">The quick brown fox jumps over the lazy dog.</div>
</div>
<hr>
<div class="outer">
<div class="inner">"The quick brown fox jumps over the lazy dog" is an English-language pangram—a phrase that contains all of the letters of the alphabet. It is commonly used for touch-typing practice.</div>
</div>
You can also use flexbox, it makes it easy for vertically centering the text too.
.container {
display: flex;
justify-content: center; /*center horizontally*/
align-items: center; /*center vertically*/
height: 100px;
border: 1px solid;
}
<div class="container">
The quick brown fox jumps over the lazy dog.
</div>
<hr>
<div class="container">
"The quick brown fox jumps over the lazy dog" is an English-language pangram—a phrase that contains all of the letters of the alphabet. It is commonly used for touch-typing practice.
</div>
One more way would be using CSS table for centering horizontally, similarly to the other suggestion, but also using CSS table cell for centering vertically.
.outer {
display: table;
margin: auto; /*center horizontally*/
height: 100px;
border: 1px solid;
}
.inner {
display: table-cell;
vertical-align: middle; /*center vertically*/
}
<div class="outer">
<div class="inner">The quick brown fox jumps over the lazy dog.</div>
</div>
<hr>
<div class="outer">
<div class="inner">"The quick brown fox jumps over the lazy dog" is an English-language pangram—a phrase that contains all of the letters of the alphabet. It is commonly used for touch-typing practice.</div>
</div>