Every time I come across a CSS solution for a nice drop cap, I never end up using it, because it always involves modifying your XHTML in some way. That’s fine if it’s some static content, but for a blog, having to modify your markup with every post is a pain, and just not worth the effort. That’s why I want to post this very simple way to to add drop caps to your site, with just a few simple pseudo classes: first-letter, first-line, and first-child. They are pretty self explanatory. We’ll use first-letter to affect the first letter in a paragraph, first-line to affect the first line in a paragraph, and first-child to affect the first paragraph of multiple paragraphs. As always, I’ve put up an example.
Now one could just try using first-letter on it’s own for the drop cap, but the problem you end up with is a drop cap on every paragraph. Visually unappealing and a bit of a type crime in my opinion. So instead, we’ll use two pseudo classes together.
p:first-child:first-letter{float:left;font-size:1.6em;margin-right:4px;}
With this we’re just affecting the first letter of the first paragraph. Remember to float it on the left so you don’t have one large character sitting above the rest on the first line.
And now to use first-line
p:first-child:first-line{font-variant:small-caps;}
This will set the first line of the first paragraph in small caps. Optional, but I think it’s a nice touch.
In the example I added a bit more style to drop caps. If you’d like to get a little fancier, try something like this:
p:first-child:first-letter{float:left;font-size:1.6em;padding:5px;background:#000;color:#fff;margin-right:4px;font-weight:bold;}
p:first-child:first-line{font-variant:small-caps;}
I’ve tested this successfully in Firefox, Safari, Camino, Opera, and IE7. These pseudo classes won’t work in IE6, but it won’t affect the usability of your site. The drop caps just won’t appear.
Since IE6 doesn’t support child selectors, note that you could accomplish this in Javascript with jQuery, like so:
<code>
$(‘p:first-child:first-letter’).css({float: ‘left’, ‘font-size’: ’1.6em’, padding: ‘5px’, background: ‘#000’});
</code>
@Chriskalani: I like that browser compatibility chart. I made something along those lines myself with examples: http://kimblim.dk/csstest/
Beautiful & clever code. If IE6 had support for child-slectors than it would be te perfect solution. Thi sis a great solution anyway. Thanks.
This is off-topic from your post. But i need your design opinion on this tumblr theme I am working on : which one looks better dark design OR light design ?
This is great! Special thanks to David for adding the jQuery code so now it works on IE6 as well.
Nice work. What I had been doing is only putting it to H2 tags that way it wouldn’t repeat.
Hi, was referred to your blog.
I just keep a draft of this code and cut and paste.
I only use it for the beginning paragraph – not every paragraph, so no big deal.
`<span style=“margin-top: 0px; font-weight: bold; font-size: 36pt; float: left; padding-top: 0.2em; padding-bottom: 0.5em; height: 36pt; color: black;”>B</span>`
chriskalani
posted on Jan 21, 06:39 PMI have been playing around a bit with :first-child and :last-child recently. Here is a good browser compatibility chart ( http://www.css3.info/modules/selector-compat/ ). It’s weird because some support first and not last and vice-versa.