When using max-width
why won't it "break" words that are longer than allowed and how would I go about making it work?
JSFiddle
function input() {
var inputText = document.getElementById("inputField").value;
document.getElementById("changingParagraph").innerHTML = inputText;
}
#changingParagraph {
max-width: 100px;
}
<input type="text" id="inputField" oninput="input()">
<p id="changingParagraph">
</p>
max-width overrides width , but min-width overrides max-width .
This is a simple way to put it: if the element would render wider than the max-width says it should be, then the max-width property wins over the width property. But if it would render less than the max-width says, then the width property wins. In mathematical terms: if width > max-width; let the browser use max-width.
The max-width property in CSS is used to set the maximum width of a specified element. The max-width property overrides the width property, but min-width will always override max-width whether followed before or after width in your declaration.
And min-width specify lower bound for width. So the width of the element will vary from min-width to ... (it will depend on other style). So if you specify min-width and max-width , you will set up a lower and upper bound and if both are equal it will be the same as simply specifing a width .
The <p>
is not exceeding the max-width
length that you have set. The issue is that the text is overflowing so it goes past the length you set for your element. There are a few different methods for breaking text to the next line.
Using overflow-wrap: break-word;
(previously known as word-wrap: break-word
):
function input() {
var inputText = document.getElementById("inputField").value;
document.getElementById("changingParagraph").innerHTML = inputText;
}
#changingParagraph {
max-width: 100px;
overflow-wrap: break-word;
}
<input type="text" id="inputField" oninput="input()">
<p id="changingParagraph">
</p>
Using word-break: break-all
:
function input() {
var inputText = document.getElementById("inputField").value;
document.getElementById("changingParagraph").innerHTML = inputText;
}
#changingParagraph {
max-width: 100px;
word-break: break-all;
}
<input type="text" id="inputField" oninput="input()">
<p id="changingParagraph">
</p>
For the most readable and clean looking breaks, overflow-wrap: break-word;
is the best option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With