I have this HTML page using div boxes with text being added into it, and long strings without no spaces like aaaaa spam causes the text to run off the DIV, and doesn't even contain it.
I don't want overflow: scroll
where the div just turns into a box where you scroll, I want it so it goes to the next line.
I tried multiple solutions involving properties with the word wrap
or overflow
and break-word
but that didn't work.
It's got to be a DIV element I'm using, I have to put in different types of colored text and <textarea>
won't let me do that. It just regards the elements I add in as a string and nothing like HTML.
That oninput
code just adjusts the div box when you enter a lot of words.
So;
- I want to make
innerHTML
elements with text to wrap in a div box - It has to be a
div
element - Must wrap, not scroll.
My current code:
- When you have a spaceless, long word: just goes off the screen.
- Works with sentences like "The fox jumped over the lazy brown dog"
I have a feeling this is a CSS issue, but I'll just say the language I use is Javascript
, just in case.
.myClass {
height: 30vh;
width: 80%;
padding: 10px;
font-size: 14px;
text-align: left;
resize: none;
margin: auto;
background-color: gray;
}
div,
span {
white-space: pre;
overflow-wrap: break-word;
word-wrap: break-word;
display: inline-flex;
}
<div class="myClass" readonly="true" oninput='this.style.height = "";this.style.height = this.scrollHeight + "px"'>
<span>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
</div>