When I specify text-align:center
for an element with a width that is greater than the width of the text, the text is centered within the content box of the element (as expected).
When I specify text-align:center
for an element with a width that is less than the width of the text, the text is aligned to the left edge of the content box and overflows the right edge of the content box.
You can see the two cases in action here.
Can any CSS magic make the text equally overflow both the left edge and the right edge of the content box, so that it stays centered?
Just add display:flex and justify-content: center to the . content .
Select the text that you want to center. in the Page Setup group, and then click the Layout tab. In the Vertical alignment box, click Center. In the Apply to box, click Selected text, and then click OK.
To center the text using float we can use margin-left or margin-right and make it 50% , like below. You can learn more about the uses of Float here.
To have text-overflow property used, the text element must first overflow. This can be done by preventing the element from wrapping the overflowed content to a new line, which can be done by setting white-space: nowrap . Additionally overflow must be set to hidden .
I know this question is old, but I just had the same Problem and found a much easier solution with just a span. http://jsfiddle.net/7hy3w2jj/
<div>some text</div> <div> <span class="text-overflow-center">some text that will overflow</span> </div>
Then you just need this definition
.text-overflow-center { margin-left: -100%; margin-right: -100%; text-align: center; }
If you can work with pseudo elements, it can be done with no html at all. Just add these definition to your text container. http://jsfiddle.net/7287L9a8/
div:before { content: ""; margin-left: -100%; } div:after { content: ""; margin-right: -100%; }
The only downside to the pseudo variant is that it only works with one line of text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With