Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can overflow text be centered?

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?

like image 455
Nathan Ryan Avatar asked Jul 08 '11 00:07

Nathan Ryan


People also ask

How do I center an overflow in HTML?

Just add display:flex and justify-content: center to the . content .

How do I make my text centered?

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.

How do I center text in a float?

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.

How do you handle overflow text?

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 .


1 Answers

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.

like image 116
Nemo64 Avatar answered Sep 29 '22 04:09

Nemo64