1

I'm having a problem getting long lines of text to correctly break and wrap in a chat feature that I'm working on. The HTML below is the relevant set of nested elements, but the crux of the biscuit is with .chat__messages__item and .chat__messages__body (the whole block below is inside of an element that is set to 24vw, so it is all intended to be window-width-responsive).

First off, here's the HTML/CSS...

<style>
    .chat__messages__inner{
        display: table;
        height: 100%;
        width: 100%;       
    }

    .chat__messages__list {
        display: table-cell;
        vertical-align: bottom;
        margin: 0;
        padding: 10px 20px 0;
        list-style-type: none;
    }

    .chat__messages__item {
        position: relative;
        margin-bottom: 10px;
        padding: 8px 10px;
        background-color: #D8F2FD;
        clear: both;
    }


    <!-- THIS STYLE HAS NO AFFECT UNLESS I SET A MAX-WIDTH ON .chat__messages__item -->
    .chat__messages__body {
        word-wrap: break-word;
    }
</style>

<div class='chat__messages__inner'>
    <ul class='chat__messages__list'>
        <li class='chat__messages__item'>
            <div class='chat__messages__body'>
                hereisaverylonglineoftextthatiwouldliketobreakandwrap
            </div>
        </li>
        <li class='chat__messages__item'>
            <div class='chat__messages__body'>
                here is a long sentence that will wrap and behave correctly
            </div>
        </li>
    </ul>
</div>

The desired behavior is that the <div> and <li> containing the text should be no wider/taller than the text itself, but those elements also should never be wider than their parents - so for a few words, they might be 150px wide, but if the container shrinks to be less than 150px, these elements will also start to shrink and the text inside will start to wrap.

Playing with this code, I was able to get close to the desired result by setting the style for .chat__messages__body to include word-wrap: break-word and then setting the parent .chat__messages__item to include max-width: 300px (omitted above). Although the long word would break and wrap, it only produced the correct result on my full-screen window - if the window is resized or starts off at less-than-full, the word still wraps but the div is 300px wide (I tried setting this as a percentage, but that does not work, the word actually unwraps).

The long sentence that I included above does exactly what I would like - its parent <div> and <li> are both the size of the text, and if the window shrinks so that the width of these elements would be greater than their parents (which all scale to the 24vw ancestor), they begin to shrink as well and the text wraps on spaces.

In plain English, I would like the long word's container to never be wider than the 100% width ancestors, and it needs to resize dynamically with the window, breaking and wrapping along the way.

I'm not really a CSS/design expert, this is code I inherited from someone else, and I've been fighting with this for way too long... any guidance would be much appreciated.

skwidbreth
  • 7,888
  • 11
  • 58
  • 105
  • 1
    Is there an example somewhere that you'd like to emulate? I can't quite tell what the desired end result is. Here's the fiddle with `display: inline-block` added to make the div match the size of the text, as a jumping-off point: https://jsfiddle.net/kgy69o9z/1/ – Sander Moolin Apr 13 '17 at 19:16

2 Answers2

0

Here is a question you could check out. One of the answers suggest what I would try, which is to use the <wbr> tag which creates a word break opportunity. You can read about it here.

Community
  • 1
  • 1
0

Ok, turns out the thing to do was to set .chat__messages__inner and .chat__messages__list to display: inline-block with width: 100%, and .chat__messages__item needed to have max-width: 100%.

skwidbreth
  • 7,888
  • 11
  • 58
  • 105