0

Does anyone know why the text in this div adds all that extra white-space on the ends of each line, instead of having the border wrap "tightly" around the text as expected?

https://jsfiddle.net/wvuszL5f/

.myClass { padding:0px; max-width:368px; white-space:normal; border:1px solid black; }

I thought white-space:normal would get this working, but I also tried some of its other values. No luck.

2 Answers 2

2

You can add text-align css in your class like below:

.myClass { padding:0px; max-width:368px; white-space:normal; border:1px solid black; text-align: justify;}
Sign up to request clarification or add additional context in comments.

5 Comments

That adds a lot of expanded spaces to my text. Is that the only way for "wrap" to "truly wrap" correctly?
Did you add text-align: justify as I mentioned? For more beautification you can change the padding like padding:0px 10px 0px 0px;
@DonnaDonna You can add word-spacing to resolve the spacing issues
text-align:justify looks awful with massive spaces between words. Adding MORE padding is exactly the opposite of what I'm trying to do.
How would I use word-spacing to reduce the large space at the end of each of my lines of text?
0

As soon as there are (automatic) linebreaks in the text, the element will expand to full width (in this case the defined max-width, by default 100% of the parent element). There is no way around this.

In a left-aligned text this will cause the described effect, in right-aligned text the white spaces will be at the right, centered will have space on both sides, only justify will be different, i.e. distribute the spaces evenly between the words, but still become full-width.

4 Comments

Not sure why "add linebreaks for me" also tells css "ignore my max-width, and just set that to the width instead". Not sure why css can't do exactly what I tell it to do.
Your max-width isn't ignored, the width of the element is 370px, which is your max-width plus twice the border. What I described is simply the way it works – CSS won't know the exact text content and resulting width of the longest line, it simply knows that the overall text length is longer than your max-width and therefore sets the width to the defined max-width.
So it's not really "ignoring my max-width setting"... it's just "not using it, and changing it to another value". Isn't that the same as "ignoring it"? You explained (twice) why it happens... but it's still "not using my max-width as the max-width I want".
It is using your max-width – check in in the browser tools to see it. And if the way it works isn't "the way you want it" you simply have to accept that an live with it, sorry... (that's not my fault)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.