To achieve this, you can use a combination of different text-align and display values.
.outer { text-align: center; display: block; } .inner { text-align: left; display: inline-block; }
<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 , this makes it easier to center text vertically.
.container { display: flex; justify-content: center; align-items: center; 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>
Another way is to use the CSS table for horizontal alignment, similarly to the other , but also use the CSS table cell to center vertically.
.outer { display: table; margin: auto; height: 100px; border: 1px solid; } .inner { display: table-cell; vertical-align: middle; }
<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>
source share