No, it is not a reliable behavior.
The previous response was incorrect, and I wanted to provide some context as to why.
This observation was first made by @GSerg in a comment below the original answer. So credit goes to him for identifying this issue.
CR = Carriage Return (\r
, 0x0D
in hexadecimal, or 13
in decimal) does not result in a line break with either white-space: pre-wrap
or white-space: pre-line
.
LF = Line Feed (\n
, 0x0A
in hexadecimal, or 10
in decimal) does cause a line-break with both white-space: pre-wrap
and white-space: pre-line
, including combinations such as
where it is combined with a carriage return.
Evidence for this can be found at jsfiddle.net/n135a8wq.
This explanation makes sense when you consider that CR
and LF
originate from typewriters, where LF moved the paper up while CR brought back the "carriage" so the next character typed would start at the leftmost position on the same line. CR+LF
performed both actions simultaneously.
Previous (Incorrect) Response
Yes, it is valid. Using white-space: pre-wrap;
functions similarly to using <pre>
, but with automatic line wrapping according to its parent's boundaries.
Nevertheless, ensuring your CSS compatibility across different browsers is crucial.
#log {
white-space: pre-wrap; /* CSS3 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
If you prefer to maintain everything on a single line within the output, you can use white-space: pre;