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>
CodePudding user response:
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>
CodePudding user response:
Please find below the new styling that I added -> word-wrap: break-word; This makes the word break and fit the box size.
<p>no spaces:</p>
<div style="height:50px;width: 200px;border: 4px solid red; word-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>
CodePudding user response:
By default, the UserAgent (default stylesheet of the browser) will not randomly break a word into multiple lines. The default setting is word-break: normal
.
That means that a line-break will only appear on white-spaces, dashes, and line break opportunities (<wbr>
tag).
Alternativly, you can allow a browser to break words at any point to fit the container by using: word-break: break-all
:
div {
word-break: break-all;
}
<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>