Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max-width not breaking single words, exceeding max-width

Tags:

html

css

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>
like image 367
HarshWombat Avatar asked Aug 29 '16 16:08

HarshWombat


People also ask

Does width override max-width?

max-width overrides width , but min-width overrides max-width .

Can we use max-width and width together?

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.

How do you override max-width in CSS?

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.

Can we set min-width and max-width for an element at the same time?

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 .


1 Answers

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):

  • will wrap overflowed words onto the the next line.
  • will only break a word if its length exceeds the length of the container.

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:

  • will break when the content hits the specified width, even if the text is a single word.

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.

like image 123
Hunter Turner Avatar answered Nov 14 '22 22:11

Hunter Turner