192

How can I prevent text in a div block from overflowing in CSS?

div {
  width: 150px;
  /* what to put here? */
}
<div>This div contains a VeryLongWordWhichDoesNotFitToTheBorder.</div>
j08691
  • 204,283
  • 31
  • 260
  • 272

15 Answers15

213

If you want the overflowing text in the div to automatically newline instead of being hidden or making a scrollbar, use the

word-wrap: break-word

property.

Alkanshel
  • 4,198
  • 1
  • 35
  • 54
88

You can try:

<div id="myDiv">
    stuff 
</div>

#myDiv {
    overflow:hidden;
}

Check out the docs for the overflow property for more information.

Peter Grace
  • 569
  • 5
  • 20
brettkelly
  • 27,655
  • 8
  • 56
  • 72
84

Short Answer

Simply use:

word-wrap: break-word;
white-space: pre-wrap;
word-break: break-word;

Long Answer

1. word-wrap

Allows long words to be able to break and wrap onto the next line.

Possible values:

  • normal: Break words only at allowed break points
  • break-word: Allows unbreakable words to be broken

div {
    width: 150px; 
    border: 2px solid #ff0000;
}

div.normal {
    word-wrap: normal;
}

div.break {
    word-wrap: break-word;
}
<h2>word-wrap: normal</h2>
<div class="normal"> This div contains a VeryLongWordWhichDoesNotFitToTheBorder.</div>

<h2>word-wrap: break-word</h2>
<div class="break"> This div contains a VeryLongWordWhichDoesNotFitToTheBorder.</div>

2. white-space

Specifies how white-space inside an element is handled.

Possible values:

  • nowrap: Text will never wrap to the next line.
  • pre-wrap: Whitespace is preserved by the browser. Text will wrap when necessary, and on line breaks

div {
    width: 150px;
    border: 2px solid #ff0000;
}

div.nowrap {
    white-space: nowrap;
}

div.pre-wrap {
    white-space: pre-wrap;
}
<h2>white-space: nowrap</h2>
<div class="nowrap">This div contains a very long but normal paragraph with so many words and nothing else.</div>

<h2>white-space: pre-wrap</h2>
<div class="pre-wrap">This div contains a very long but normal paragraph with so many words and nothing else.</div>

3. word-break

Specifies how words should break when reaching the end of a line.

Possible values:

  • normal: default line break rules.
  • break-word: To prevent overflow, word may be broken at arbitrary points.

div {
  width: 150px; 
  border: 2px solid #ff0000;
}

div.normal {
  word-break: normal;
}

div.break-word {
  word-break: break-word;
}
<h2>word-break: normal (default)</h2>
<div class="normal"> This div contains a VeryLongWordWhichDoesNotFitToTheBorder.</div>

<h2>word-break: break-word</h2>
<div class="break-word"> This div contains a VeryLongWordWhichDoesNotFitToTheBorder.</div>

For browser-specific versions of white-space, use:

white-space: pre-wrap;      /* CSS3 */
white-space: -moz-pre-wrap; /* Firefox */
white-space: -pre-wrap;     /* Opera <7 */
white-space: -o-pre-wrap;   /* Opera 7 */
evandrix
  • 6,041
  • 4
  • 27
  • 38
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
62

You can use the CSS property text-overflow to truncate long texts.

  #greetings
  {
    width: 100px;
    border: 1px red solid;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis; // This is where the magic happens
  }
    <div id="greetings">
      Hello universe! 
    </div>

    <div id="greetings">
      Hello universe! 12345678901234567890
    </div>

reference: http://makandracards.com/makandra/5883-use-css-text-overflow-to-truncate-long-texts

Sachin
  • 2,152
  • 1
  • 21
  • 43
Mara Jimenez
  • 846
  • 8
  • 13
20

You can control it with CSS, there is a few options :

  • hidden -> All text overflowing will be hidden.
  • visible -> Let the text overflowing visible.
  • scroll -> put scroll bars if the text overflows

Hope it helps.

Timothée Martin
  • 775
  • 4
  • 13
15

there is another css property :

white-space : normal;

The white-space property controls how text is handled on the element it is applied to.

div {

  /* This is the default, you don't need to
     explicitly declare it unless overriding
     another declaration */
  white-space: normal; 

}
palaѕн
  • 72,112
  • 17
  • 116
  • 136
Sachin
  • 2,152
  • 1
  • 21
  • 43
6

Try adding this class in order to fix the issue:

.ellipsis {
  text-overflow: ellipsis;

  /* Required for text-overflow to do anything */
  white-space: nowrap;
  overflow: hidden;
}

Explained further in this link http://css-tricks.com/almanac/properties/t/text-overflow/

5

overflow: scroll ? Or auto. in the style attribute.

bean
  • 1,091
  • 1
  • 12
  • 23
5

I guess you should start with the basics from w3

https://www.w3schools.com/cssref/css3_pr_text-overflow.asp

div {
    white-space: nowrap; 
    overflow: hidden;
    text-overflow: ellipsis;
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Ronit Oommen
  • 3,040
  • 4
  • 17
  • 25
5

It's now the css property:

word-break: break-all

Cloud
  • 241
  • 4
  • 10
2
.NonOverflow {
    width: 200px; /* Need the width for this to work */
    overflow: hidden;
}
<div class="NonOverflow">
    Long Text
</div>
TylerH
  • 20,799
  • 66
  • 75
  • 101
Kirtan
  • 21,295
  • 6
  • 46
  • 61
  • in multilingual text other than, it causes some characters of long words be hidden from display. – Bhupi Sep 25 '10 at 13:16
1

If your div has a set height in css that will cause it to overflow outside of the div.

You could give the div a min-height if you need to have it be a minimum height on the div at all times.

Min-height will not work in IE6 though, if you have an IE6 specific stylesheet you can give it a regular height for IE6. Height changes in IE6 according to the content within.

1

You can just set the min-width in the css, for example:

.someClass{min-width: 980px;}

It will not break, nevertheless you will still have the scroll-bar to deal with.

3xCh1_23
  • 1,491
  • 1
  • 20
  • 39
1

Recommendations for how to catch which part of your CSS is causing the issue:

1) set style="height:auto;" on all the <div> elements and retry again.

2) Set style="border: 3px solid red;" on all the <div> elements to see how wide of an area your <div>'s box is taking up.

3) Take away all the css height:#px; properties from your CSS and start over.

So for example:

<div id="I" style="height:auto;border: 3px solid red;">I
    <div id="A" style="height:auto;border: 3px solid purple;">A
        <div id="1A" style="height:auto;border: 3px solid green;">1A
        </div>
        <div id="2A" style="height:auto;border: 3px solid green;">2A
        </div>
    </div>
    <div id="B" style="height:auto;border: 3px solid purple;">B
        <div id="1B" style="height:auto;border: 3px solid green;">1B
        </div>
        <div id="2B" style="height:auto;border: 3px solid green;">2B
        </div>
    </div>
</div>
Gene
  • 10,819
  • 1
  • 66
  • 58
0

!! Hard coded tradoff ahead !! Depending on the surrounding code and the screen resolution this could lead to different / unwanted behaviour

If hidden overflow is out of the question and correct hyphenation is needed you could use the soft hyphen HTML entity where you want the word / text to break. This way you are able to predetermine a breaking point manually.

&shy;

The hyphen will only appear when the word needs to break to not overflow its surrounding container.

Example:

<div class="container">
  foo&shy;bar
</div>

Result if the container is wide enough and the text would not overflow the container:

foobar

Result if the container is to small and the text would actually overflow the container:

foo-
bar

.container{
  background-color: green;
  max-width: 30px;
}
Example 1 - container to small => text overflows container:

<div class="container">
  foobar
</div>

Example 2 - using soft hyphen => text breaks at predetermined break point:

<div class="container">
  foo&shy;bar
</div>

Example 3 - using soft hyphen => text still overflowing because the text before the soft hyphen is to long:

<div class="container">
  foobar&shy;foo
</div>

Further information: https://en.wikipedia.org/wiki/Soft_hyphen

janDo
  • 23
  • 3