6

When I use

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

...there is one additionl line before text. To hide it, I used:

.class:first-line { line-height: 0; }

...and it is working in Chrome browser, but not in the Firefox.

Here is a working fiddle and please open it in Firefox : https://jsfiddle.net/t3qj51co/2/

enter image description here

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
Dejan Munjiza
  • 790
  • 1
  • 12
  • 23

3 Answers3

24

Why not just fix your HTML (demo)

The problem is that in your html you have an empty line. Since you use pre-line it takes that into account as well.

Just removing the empty line fixes it.


The issue is that firefox does not allow to decrease the line-height more than the currently active one.

At first this looks like a Firefox bug (it works as described in the OP in Chrome, IE, Safari).
But reading the specification i see that it is open to interpretation as it says that

This paragraph might be "rewritten" by user agents to include the fictional tag sequence for ::first-line. This fictional tag sequence helps to show how properties are inherited.

If a UA follows this approach then it is valid to not decrease the visual height of the line, as it only applies to the "fictional tag". (a nested tag cannot influence the line-height of its container downwards)

So not sure if it is an actual bug (on either spectrum of browsers) or a valid difference in implementations.

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
0

Just write your HTML like this:

<div class="test">Don't press a carriage return after an opening div and continue typing....

This is the next paragraph and at the end, just close the div like this.</div>

If you use template code, just do like this:

<div class="test">{{{MustacheDataHere}}}</div>

No need to put this .test:first-line { line-height: 0; } in your .test class. Refer to this answer for more info.

Antonio Ooi
  • 1,601
  • 1
  • 18
  • 32
0

I was able to botch my way to obtain the same results in FF and Chrome by adding a negative margin; see the content in .new:

.class {
  white-space: pre-line;
}

.class::first-line {
  line-height: 0;
}

.new:before {
  content: '';
  margin: -1.5em 0;
  display: block;
}
<hr>
<div class='class'>
    line #1
    line #2
    line #3
</div>
<hr>
<div class='class new'>
    line #1
    line #2
    line #3
</div>
Chrome Firefox
Chrome FireFox
Mehvix
  • 296
  • 3
  • 12