Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text overflow behaviour in CSS with non-left values for text-align

Given the following HTML:

<p>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
</p>

And the following CSS:

p {
  border: 1px solid red;
  width: 200px;
  text-align: right;
  white-space: nowrap; 
}

What would the expected rendering be? I was expecting the text to butt up against the right hand side of the para and overflow off to the left. Observed results in Fx/Safari/Opera butt the text to the left and overflow to the right though. The same problem is observed with text-align:center; I’d expect the text to overflow equally to both sides.

CSS2.1 and CSS3 Text don’t seem to specify the rendering.

Test link: http://www.webdevout.net/test?0e&raw

like image 522
Robin Whittleton Avatar asked Sep 21 '10 10:09

Robin Whittleton


People also ask

How do you deal with overflow text in CSS?

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 .

How do I align text with one line in CSS?

The text-align-last property specifies how to align the last line of a text. Notice that the text-align-last property sets the alignment for all last lines within the selected element. So, if you have a <div> with three paragraphs in it, text-align-last will apply to the last line of EACH of the paragraphs.


2 Answers

I was able to get the result you were after using the direction property, e.g.

p { 
    direction: rtl; 
    border: 1px solid red; 
    width: 200px; 
    text-align: right; 
    white-space: nowrap;
}

That worked in current versions of Firefox, Safari and IE.

like image 153
Olly Hodgson Avatar answered Sep 19 '22 21:09

Olly Hodgson


The "Inline Formatting Context" section of the CSS 2.1 spec says:

When the total width of the inline boxes on a line is less than the width of the line box containing them, their horizontal distribution within the line box is determined by the 'text-align' property. If that property has the value 'justify', the user agent may stretch spaces and words in inline boxes (except for inline-table and inline-block boxes) as well.

When an inline box exceeds the width of a line box, it is split into several boxes and these boxes are distributed across several line boxes. If an inline box cannot be split (e.g., if the inline box contains a single character, or language specific word breaking rules disallow a break within the inline box, or if the inline box is affected by a white-space value of nowrap or pre), then the inline box overflows the line box.

So, the text-align property is only used in cases where the line box length is less than the block width. If the line box is wider than its containing element then the text-align property isn't considered.

like image 30
Gareth Avatar answered Sep 20 '22 21:09

Gareth