Easy CSS Drop Caps

Article
02Jan
5

Adding drop caps to the beginning of a paragraph, as I did with my latest redesign, is actually a pretty simple and straight-forward process. The effect can be achieved with just a couple of lines of CSS making use of two special ‘pseudo-elements’.

This effect should work in most browsers (yes, even Internet Explorer, according to MSDN both ‘elements’ should work in IE5.5+), the only real variation may be which font is displayed for the drop cap, and the way the specific browser interprets ‘em’ font-sizes. Alternatively pixel sizes could be used for the drop cap font, which should allow you to get greater consistency between browsers.

As mentioned we’re going to use two pseudo-elements as outlined in the CSS2 specification, the two ‘elements’ in question are :first-line and :first-letter. If you’re curious and want to know more about these ‘elements’ or the concept of ‘pseudo-elements’ feel free to follow the links and have a read of the specification, its not necessary for the purposes of this brief how-to, but some people are into that sort of thing.

Ok, let’s start. First off we’ll need to set up the default paragraph style before we do anything else. Nothing very complex here, just a few basic styles to set size, style, alignment etc.

.excerpt p {
	text-align:justify;
	margin-top:2em;
	font-size:1.2em;
	font-style:italic;
	color:#666;
}

Next up is the style for the :first-letter, as you would probably be expecting this style will apply only to the first letter of the paragraph in question. You can do pretty much whatever you want to the style, in this case we’re making the font larger, bold, changing the font and adding a little margin around the drop cap.

The only important bits that need to be set are:

  • float:left; - this allows the rest of the paragraph to wrap around the drop cap.
  • line-height:0.75em; - I played with a few values here, but this seemed to work best for the font family I’m working with, changing this value will affect the way that your drop cap lines up with the rest of your paragraph.

Remember its all personal preference, play around with sizes, fonts, margins etc until you are happy with the look.

.excerpt p:first-letter {
	font-size:6em;
	line-height:0.75em;
	float:left;
	margin-right:0.1em;
	font-family:"Warnock Pro", "Baskerville", "Goudy Old Style","Palatino","Book Antiqua",serif;
	font-weight:bold;
	font-style:normal;
}

Now you can also setup a separate style for the :first-line, and yep, you guessed it, it only affects the first line of your paragraph. Again, like before, you can change whatever characteristics you like, I’ve made it uppercase and increased the font-size slightly.

Nothing complex about this, just style it like you would any other paragraph. As far as the style goes, you could go completely nuts and make the test italics, purple, with a lime green dotted underline… but, well, I’d probably suggest thats taking things too far.

.excerpt p:first-line {
	text-transform:uppercase;
	font-size:1.3em;
	line-height:1em;
	font-style:normal;
}

There you go, simple as that, now you’ve got awesome drop caps too. Experiment with different combinations, maybe just use the drop cap, or maybe just use the first line style. You could change the colour to white and give the drop cap a background colour for an inverted look, go nuts.

Please leave your name and email, so we can show off your gravatar


5 Comments on this

  1. I find that I get radically different drop-caps results on this very page between safari 3.2, firefox 3 and IE7. In safari, they are 2-3X as big as they are in firefox. In IE7, they are again huge and set 1-2 lines below where they should be. Each browser is seeing its own thing, much like they do with forms.

    I dont know how you can call this an effective implementation. I’ve had the exact same problems with my drop caps and it has prevented me from using them. Have you tested this implementation?

  2. :first-line and :first-letter are handy, but I sometimes wonder why there isn’t :first-word as well?

  3. Nice, But How to do drop cap can be found in some other web sources too.. but can you say how you managed to change the size of the title in your blog post with WordPress ?

    “…actually a pretty simple and straight-forward process…” It is small in size where as, “…as I did with my latest redesign, is …” has a large text size.. can you say that trick ?

    • EDIT: Sorry not title, but excerpt.. you made two text style in single post excerpt.

      • That’s done with the :first-line style (last step above), for example the whole excerpt paragraph might have a font size of “1em”, and then you give the p:first-line style a font size of something like “1.3em”, which will make it larger than the rest of the text in the paragraph - giving you two text styles in a single excerpt. Hope that makes sense.

deanjrobinson.com
twitter was not updated. | tumblr was not updated. | lastfm was not updated. |