Search code examples
htmlcssoverflow

text comes out of the container when I write long text (without space)


If I write a long sentence the text doesn't fit the container. Why does the text overflow the container?

But when I write a long sentence but use spaces (write more than 1 sentence), the text finally fits the container.

<p>no spaces:</p>
<div style="height:50px;width: 200px;border: 4px solid red;">helloooooooooooooooooooooooooooooooo</div>
<br>
<p>with spaces:</p>
<div style="height:50px;width: 200px;border: 4px solid red; ">hello ooooooo ooooo ooooooo ooooooooo oooooooo</div>


Solution

  • To break the line of your text you can use overflow-wrap: break-word;:

    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    <body>
      <p>no spaces:</p>
      <div style="height:50px;width: 200px;border: 4px solid red;overflow-wrap: break-word;">helloooooooooooooooooooooooooooooooo</div>
      <br>
      <p>with spaces:</p>
      <div style="height:50px;width: 200px;border: 4px solid red;
      ">hello ooooooo ooooo ooooooo ooooooooo oooooooo</div>
    </body>
    </html>