Drop caps, first-letter and Firefox

I have been exper­i­ment­ing with the :first-letter pseudo-element to try and write up a tutorial on using first-letter, adja­cent select­ors and @font-face to pro­duce drop caps and I have run into a prob­lem in the form of Fire­fox. Because you need to apply the prop­erty float: left to stop first-letter being an inline ele­ment and Fire­fox applies dif­fer­ent rules to floated :first-letter pseudo-element than the other browsers, mak­ing it dif­fi­cult to ver­tic­ally align a floated :first-letter pseudo-element and the the remain­ing text.

The specs for :first-letter pseudo-element state:

These are the prop­er­ties that apply to :first-letter pseudo-elements: font prop­er­ties, … ‘line-height’, … mar­gin prop­er­ties, pad­ding prop­er­ties … UAs may apply other prop­er­ties as well.

Width and height are not prop­er­ties that have to be applied to first-letter. Only the Web­Kit fam­ily (Safari, Chrome etc.) apply these prop­er­ties. The main issue is line-height which could be used to con­trol the ver­tical size of the first-letter pseudo-element, as accord­ing to the specs.

To allow UAs to render a typo­graph­ic­ally cor­rect drop cap or ini­tial cap, the UA may choose a line-height, width and height based on the shape of the letter

Inter­net Explorer, Web­kit and Opera have chosen not to apply line-height and unless one is spe­cified, a :first-letter pseudo-element will inherit line-height from the par­ent ele­ment. Fire­fox has chosen to apply a line-height to all first-letter ele­ments equal to the height of the ele­ment and you have no method of chan­ging it. This auto­mat­ic­ally aligns the top of the floated :first-letter pseudo-element in the par­ent block element.

Because the line-height is the height of the type, you get dif­fer­ent line-height for dif­fer­ent char­ac­ters eg a, t and p. Which makes it vir­tu­ally impossible to use lower case let­ters to pro­duce drop caps like effects.

So you need to reduce the line-height of first-letter to the cap height of your chosen typeface then use margin-top or padding-top to ver­tic­ally align the top of first-letter with the top of the remain­ing text. Not only is this more com­plic­ated than just chan­ging line-height, you need to do two cal­cu­la­tions instead of one. Inter­net Explorer applies mar­gin and pad­ding to floated :first-letter pseudo-elements dif­fer­ently than other browsers. By fix­ing the :first-letter pseudo-element in place after apply­ing line-height then apply­ing mar­gin and pad­ding to it, which does not effect sur­round­ing elements.

:first-letter pseudo element

Five dif­fer­ent browsers ren­der­ing floated :first-letter pseudo-elements with slightly dif­fer­ent prop­er­ties. Chrome pro­duces the same res­ults as Safari. Note IE8b2 does strange things to back­grounds both col­our and images of the:first-letter pseudo-element.

So to get a cross browser drop cap effect using :first-letter pseudo-element that ver­tic­ally aligns, you need to apply float: left, then find the height of the typeface cap height, reduce the line-height to that, adjust margin-top so it aligns cor­rectly in Fire­fox and in Opera or a Web­Kit browser, and then using con­di­tional com­ments for an IE only stylesheet remove margin-top and change line-height to cor­rectly ver­tic­ally align the type.

It would of been a lot easier if Moz­illa Cor­por­a­tion and Microsoft read the CSS spe­cific­a­tions the same way as every­body else and applied the same rules.

5 Responses to “Drop caps, first-letter and Firefox”

  1. Ann Arbor Web Designer Says:

    Con­form­ity is not the key on a diverse resource like the internet.

  2. Ann Arbor Michigan Web Site Design Says:

    Long time since you wrote a new post. Have you aban­doned this site?

  3. John Says:

    Con­form­ity is not the key on a diverse resource like the internet.”

    Yeah but stand­ards should be applied. Its ridicu­lous try­ing to alter code to suit sev­eral dif­fer­ent browsers.

  4. Wistful Writer Says:

    Hey, just wanted to stop by and say thanks! I was fid­dling around with @font-face and drop­caps recently and was shocked to see what was right in Fire­fox was way off in both Chrome and Safari. Your tips got everything work­ing pretty well, thanks a bunch!

  5. Alex B Says:

    I wanted to add a thanks for this art­icle and a lament that the bug still exists, though I’m not sure which browser’s “fault” it is. I’ve tried your advice above and, though it looks OK, I still find a vari­ance of about 10px between dif­fer­ent browsers. This issue is fur­ther com­plic­ated by the font you’re using (since one browser may be using the .woff and another the .otf), and whether you already have a loc­ally cached font. IE9 seems to dis­play things dif­fer­ently again.

    Another descrip­tion of this bug which must plague other people try­ing to do nice, clean typo­graphy is here: http://dev.l-c-n.com/CSS3-selectors/browser-support.php#n4

    I’ve opted to do wrap the first char­ac­ter in a span as fol­lows and aban­don :first-letter completely:

    span.dropcap {
    float: left;
    font-size: [whatever]px;
    line-height: [meas­ured caps X height]px;
    margin-top: [play accord­ing to font];
    margin-bottom: [neg­at­ive value in case the top mar­gin pushes the text below the drop cap onto the next line]]
    }

Leave a Reply

Note your comments are subject to the whim of Nick Cowie, who reserves the right to delete or edit comments I consider to be spam, defamatory, too long or just obnoxious.

Google