0

What should I do to get a <div> containing variable text behaving as follows:

  • width is always at least 400px;
  • words are not broken across lines, but lines can be broken at word boundaries;
  • text never overflows the border of the div, i.e. the width stretches to accommodate content such as very long words;
  • width is exactly 400px whenever possible. In particular, if there is a long paragraph with short words, the width should be exactly 400px.

The closest I got was using display: inline-block; min-width: 400px; but long paragraphs with short words still stretch the width.

laurt
  • 1,811
  • 3
  • 15
  • 18

3 Answers3

2

You're saying you want your div to behave like a table / table-cell.

This should meet all your stated requirements:

.box {
  width: 400px;
  display: inline-table; /* table|inline-table|table-cell */
  word-wrap: normal;
}

Demo: https://jsfiddle.net/cqo9yupw/

  • This seems to work as I wanted. Didn't need the `word-wrap` property, since `normal` is default anyway. – laurt Oct 13 '16 at 15:05
1

Try inline-table instead:

.mydiv{
    display:inline-table;
    width:400px;
    border:1px solid red;
    word-wrap:normal;
    word-break:keep-all;
}
thooyork
  • 475
  • 1
  • 4
  • 8
0

I suggest you try the following CSS:

#container {
    width:400px;
}


@media screen and (min-width:401px) {

    #container {
        width:auto;
    }
}


@media screen and (max-width:399px) {

    #container {
        width:auto;
    }
}

A div with the id 'container' should be used to encapsulate all parts of your page. This width has been set to 400px. Now when the user zooms in or out, the width should automatically adjust to their user's window. Place the id for the paragraph(s) you have in the @media sections, and they should also auto adjust.

E.g.: @media screen and (min-width:401px) {

#container {
    width:auto;
}


}


@media screen and (max-width:399px) {

#container {
    width:auto;
}

#paragraph {
    width:auto; 
}
}
User44
  • 140
  • 8