The element where you put the text have position: absolute. There is a couple of things you should know when using position: absolute in an element:
It'll by default only respect it's content width and height with a position different than absolute/fixed.
It'll not consume space in the parent element, so the structural size of the parent element will never be affected by it.
You can anchor an element with position: absolute in the nearest parent with other position than the default (static), with the css properties (top, right, bottom, left).
Therefore you should either remove the position absolute from that element, or since you are already using a left property, define a right property too (ie. right: 0;)
SNIPPET
#parent {
width: 500px;
height: 500px;
border: solid 1px;
position: relative;
}
#child {
position: absolute;
top: 20px;
right: 0;
left: 300px;
border: solid 1px;
}
.wordwrap {
white-space: pre-wrap;
/* CSS3 */
white-space: -moz-pre-wrap;
/* Firefox */
white-space: -pre-wrap;
/* Opera <7 */
white-space: -o-pre-wrap;
/* Opera 7 */
word-wrap: break-word
/* IE */
}
<div id="parent">
<div id="child" class="wordwrap">
Pneumonoultramicroscopicsilicovolcanoconiosis...
</div>
</div>
Update
If you don't want to compromise the absolute positioned #child div, by defining right: 0, which would also anchor it to the right side of the #parent div, based on Weedoze's answer, you can in fact use the property word-break: break-all;
However it has a downside, every word you have inside #child will automatically break no matter what size they have, even if they are inside a descendent element!
SNIPPET
#parent {
width: 500px;
height: 500px;
border: solid 1px;
position: relative;
}
#child {
position: absolute;
top: 20px;
left: 300px;
border: solid 1px;
text-align: justify;
}
.wordwrap {
white-space: pre-wrap;
/* CSS3 */
white-space: -moz-pre-wrap;
/* Firefox */
white-space: -pre-wrap;
/* Opera <7 */
white-space: -o-pre-wrap;
/* Opera 7 */
word-break: break-all;
}
<div id="parent">
<div id="child" class="wordwrap">
Pneumonoultramicroscopicsilicovolcanoconiosis...
With word-break: break-all every word will be broken, even little ones! With word-break: break-all every word will be broken, even little ones!
</div>
</div>
I tested this one on Firefox and it worked! I couldn't test on Edge or IE because I'm running on Ubuntu and I don't have a virtual machine installed right now :)
But if you really don't want the little/normal words to be broken, nor a compromised right anchored #child, and of course if possible, then perhaps would be better to approach this layout with a different solution, Pugazh idea is not a bad start!
You could use float: left; max-width: calc(100% - 300px); and box-sizing: border-box; to get the same results, then using a super negative margin-bottom to pull the rest of #parent's content to the top. It's a bit hacky I know, probably there are better ways to get the same results, but this way you wouldn't need to use position: absolute nor word-break: break-all in #child)
SNIPPET
#parent {
border: solid 1px;
width: 500px;
height: 500px;
}
#child {
position: relative; /* not really needed, just to be on top for contrast */
background-color: rgba(255, 255, 255, 0.9);
text-align: justify;
box-sizing: border-box;
border: solid 1px;
float: left;
max-width: calc(100% - 300px);
margin-top: 20px;
margin-left: 300px;
margin-bottom: -1000000%; /* Hack to pull #parent content to the top */
}
.wordwrap {
white-space: pre-wrap;
/* CSS3 */
white-space: -moz-pre-wrap;
/* Firefox */
white-space: -pre-wrap;
/* Opera <7 */
white-space: -o-pre-wrap;
/* Opera 7 */
word-wrap: break-word;
/* IE */
}
<div id="parent">
<div id="child" class="wordwrap">
Pneumonoultramicroscopicsilicovolcanoconiosis...
And this little words will not get broken And this little words will not get broken And this little words will not get broken.
Butgiantwordswilldefinitelybreakforsure!
</div>
This is some content This is some content This is some content This is some content
This is some content This is some content This is some content This is some content
This is some content This is some content This is some content This is some content
This is some content This is some content This is some content This is some content
This is some content This is some content This is some content This is some content
This is some content This is some content This is some content This is some content
This is some content This is some content This is some content This is some content
This is some content This is some content This is some content This is some content
This is some content This is some content This is some content This is some content
This is some content This is some content This is some content This is some content
This is some content This is some content This is some content This is some content
This is some content This is some content This is some content This is some content
This is some content This is some content This is some content This is some content
This is some content This is some content This is some content This is some content
This is some content This is some content This is some content This is some content
This is some content This is some content This is some content This is some content
:)
</div>