1

I'm trying to style a drop-cap using the :first-letter pseudo selector, but Firefox is ignoring line-height:

p {
  font-family: sans-serif;
  line-height: 3rem;
  font-size: 2.4rem;
}

p:first-letter {
  float: left;
  background-color: black;
  color: white;
  line-height: 6rem;
  font-size: 4.8rem;
  padding: 0 1rem;
  margin-right: 0.5rem;
}
<p>Feugiat adipisci salutandi mei eu, eu vix purto vero oratio. Eam justo viris
iudicabit te, sea dico esse tritani ea. Sed ad epicurei splendide. Ut constituam
delicatissimi usu. Et pro quem ceteros, ad cum doming eripuit reformidans.
Suspendisse pulvinar congue elit, vitae tincidunt mauris tempus non. Morbi
mattis tellus orci, sit amet tincidunt nibh ullamcorper vitae. Phasellus
  suscipit augue nec diam elementum, at porta est egestas.</p>

It appears to be an ancient bug / feature. See here.

Is there a workaround?

I cannot modify the markup at all, so no wrapping the first letter in another element to allow targeting it without first-letter.

I can't use vertical padding as Firefox collapses to a different line-height than other browsers, even at a line-height of zero, meaning I would have to target FF with different vertical padding values.

Setting display explicitly to block, inline-block or flex has no effect.

Setting height or min-height has no effect.

I am aware of inital-letter but the support is a long way from enough.

Undistraction
  • 42,754
  • 56
  • 195
  • 331

1 Answers1

1

I would adjust margin-top of the first letter then consider a gradient as a background for the main element to simulate the background of the first letter:

p {
  font-family: sans-serif;
  line-height: 3rem;
  font-size: 2.4rem;
  background:linear-gradient(#000,#000) 0 0/4.8rem 6rem no-repeat;
}

p:first-letter {
  float: left;
  color: white;
  font-size: 4.8rem;
  padding: 0 1rem;
  margin-right: 0.5rem;
  margin-top: 1.5rem; /*adjust this as you need*/
}
<p>Feugiat adipisci salutandi mei eu, eu vix purto vero oratio. Eam justo viris
iudicabit te, sea dico esse tritani ea. Sed ad epicurei splendide. Ut constituam
delicatissimi usu. Et pro quem ceteros, ad cum doming eripuit reformidans.
Suspendisse pulvinar congue elit, vitae tincidunt mauris tempus non. Morbi
mattis tellus orci, sit amet tincidunt nibh ullamcorper vitae. Phasellus
  suscipit augue nec diam elementum, at porta est egestas.</p>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • Interesting solution. Thanks. The problem with this approach is that there is varying whitespace above the letter across browsers and none in FF, so as I mention in the question, for consistency this would require different margin-tops for different browsers. – Undistraction May 21 '18 at 20:22