5

It seems that pre tag behaves differently if placed inside block layout and flex layout item:

  <div>
    <pre style="background-color: green;">
      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    </pre>
  </div>
  <div style="display: flex;">
    <pre style="background-color: blue;">
      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    </pre>
  </div>

First pre (marked with green background) is correctly sized by parent div's width (page width). But second pre that is placed inside flex container seems to have maximum width and spawns beyond page width. This happens only if pre have long words. Any ways I can correctly limit second pre's width without hacks like explicitly specifying it or cutting out exceeding text via overflow: hidden? I have tried white-space: pre-wrap; but it doesn't make any difference :(

enter image description here

grigoryvp
  • 40,413
  • 64
  • 174
  • 277

2 Answers2

11

Seems a famouse min-width: 0 problem. Flexbox introduced new auto value for min-width that do lots of weird things. Setting it to 0 for pre element makes it's width be as expected.

grigoryvp
  • 40,413
  • 64
  • 174
  • 277
0

Try white-space: nowrap; text-overflow: ellipsis; overflow: hidden;

David Abaev
  • 690
  • 5
  • 22
  • `overflow: hidden` works, but is it possible to limit width while still being able to display all text? I want to display code samples, and hiding overflow will be kinda overkill – grigoryvp Aug 16 '15 at 13:20