Recent Article
Book-style Chapter Introductions Using Pure CSS
Posted on February 1, 2008. By Rob Glazebrook.
Do you remember all those wonderful hand-me-down children’s books from your childhood, or perhaps those dusty old tomes you’ve leafed through at a used bookstore recently? I still like looking through old books of Days Gone By. Their typefaces, in particular, give me a warm fuzzy feeling in the far reaches of my tummy. The scripty drop-caps at the beginnings of chapters, and the first line of small caps leading into the first full paragraph, in particular, fill me with all sorts of nostalgia.
So today’s tutorial will show you how easy it is to create book-style chapter (article, whatever) introductions using nothing but pure CSS — no XHTML was harmed in the making of this tutorial. We’ll use two types of selectors which I haven’t talked about yet here: adjacent sibling selectors and pseudo-element selectors. I’ll explain each type briefly before we get started.
Adjacent Sibling Selectors are a way of styling an element that appears directly adjacent to another element. So, for example, if you knew that the first paragraph to follow an image in your XHTML was always going to be a caption, and you wanted that styled differently, you could write something like this:
img + p {
color: #999;
font-style: italic;
}
This would turn the paragraph immediately following the image gray and italicize it, all without any extra code in your XHTML.
Pseudo-Element Selectors refer to parts of your XHTML that aren’t technically elements of their own right, but can be easily distinguished from the surrounding code due to their nature. The most common two pseudo-elements (and the two we’re using here) are :first-letter and :first-line.
Creating a Book-Style Introductory Line
So what we need to do to create our book-style line is create a drop-cap of the first letter, and the rest of the first line in small caps. And we want to do it without having to resort to classes or IDs to get it done. What we’re assuming in this exercise is that your XHTML is already well-formed – your articles or chapters always start with a heading tag (I’m using <h4> tags), and you’re using paragraph tags instead of (shudder) line breaks. So here’s how we do it:
You’ll start with some fairly simple XHTML that looks something like this:
<h4>Article Titles for Fun and Profit</h4> <p>This is our first paragraph. Don’t you think the first line should stand out?</p> <p>Our second paragraph doesn’t need such fancy-pants styling.</p>
And then we’ll use CSS to style the first line of the first paragraph following our 4th level heading:
h4 + p:first-line {
font-variant: small-caps;
font-size: 1.1em;
}
This gives us a small-capped line that is slightly larger than the rest of the surrounding text. Now, all we need to do is create our drop cap:
h4 + p:first-letter {
float: left;
font-size: 2.5em;
font-weight: bold;
font-family: "Monotype Corsiva",
"Apple Chancery", fantasy;
margin: 5px 5px 5px 0;
}
Here, I’ve floated the letter to the left (which causes the rest of the text to flow around it), increased its size, made it boldface, set it in a scripty font (you could chose any font here, but I was going for a bookish look), and added a bit of a margin to make sure there isn’t any overlap.
We’ve managed to create an introductory line without a single byte of XHTML.
And that’s all there is to it! You can see a finished example here. We’ve managed to create an old-school book-style introductory line without adding a single byte of XHTML. This is a quick and easy way to add some visual appeal to your articles. It could even make for an interesting addition to a print style sheet, adding a bit of classic authenticity.
Now, I have one caveat: this does not work in IE6. But the text simply degrades nicely into an otherwise unaffected first line. No harm, no foul.
There’s more great articles where this came from.













![Click here for more information. [your ad here] Become a Sponsor! Click here to learn more.](/img/your-ad-here.gif)
On 2/2/2008 at 7:48pm, Daniel said,
This is a pretty cool technique! I like the idea of applying it to a print stylesheet. I might give it a try in my next project.
On 2/2/2008 at 8:55pm, Caustic Dave said,
Very cool! Thank you for sharing this CSS tip.
On 2/2/2008 at 8:59pm, Dito said,
very cool, so glad i stumbled across this!
On 2/3/2008 at 2:00am, crawlspace|media » Blog Archive » Daily Ma.gnolia Links for February 02 said,
[…] Book-style Chapter Introductions Using Pure CSS – CSSnewbie Co-worker’s excellent method for drop-caps and old-style ornamentation in pure css. Well done and worth a look. […]
On 2/3/2008 at 12:21pm, Rob Glazebrook said,
Thanks for all the comments, everyone. :)
On 2/4/2008 at 3:36am, fcicq's del.icio.us » Blog Archive » links for 2008-02-04 said,
[…] Book-style Chapter Introductions Using Pure CSS – CSSnewbie (tags: css typography webdesign) […]
On 2/4/2008 at 9:48am, Internet Brain » Book-style Chapter Introductions Using Pure CSS said,
[…] Book-style Chapter Introductions Using Pure CSS – CSSnewbie doesn’t work in IE6, just looks like the rest of the text. […]
On 2/6/2008 at 4:47pm, Fatih Hayrioğlu’nun not defteri » 07 Şubat web’den şeçme haberler said,
[…] CSS ile kitap bölüm girişi örneği. Bağlantı […]
On 2/12/2008 at 3:51am, Max Design - standards based web design, development and training » Some links for light reading (12/2/08) said,
[…] Book-style Chapter Introductions Using Pure CSS […]
On 2/12/2008 at 4:34am, David Storey said,
While this works well, there may be situations where you don’t know if the p element will be the first element after the article heading, such as if you have a figure or an introductory list or such. With css3 you can get around this by having something such as p:first-of-type:first-letter, which will always find the first p element in the article (assuming you use the correct selector before the p). The showstopper here is it doesn’t work in as many browsers (possibly only Opera, Konqueror and the latest WebKit nightlies), but it degrades in the same way.
On 2/12/2008 at 8:57am, Rob Glazebrook said,
Agreed, David… this technique relies on a certain level of consistency in one’s writing.
And that’s a great tip on the first-of-type selector. It’ll be nice when CSS3 is more widely supported in the browser community.
On 2/12/2008 at 5:20pm, Web Standards In Design said,
Great tips, but how do they fair in IE
On 2/12/2008 at 5:21pm, Web Standards In Design said,
Oops, my comment got chopped in half!
How do they fair in IE 6 and less, in Win and Linux?
On 2/12/2008 at 8:52pm, Rob said,
Good question…
This technique does not work in IE6 or below — it works fine in IE7, but not anything lower on the IE side. Instead, it degrades into perfectly normal text. No harm done.
However, it seems to work well in Linux browsers. Konqueror and Firefox for Linux both display it without a problem.
On 2/13/2008 at 7:05am, Michael Greve said,
It looks good in Firefox, but when I try it in IE7 the floating works (sort of), but the first-letter is raised above the second line, and is normal text.
On 2/13/2008 at 7:28am, Rob said,
Hi Michael,
I’ve noticed, too, that the floating differs between IE7 and Firefox, and between IE7 and a lot of other browsers. Basically, you’d just have to tweak the margin a little bit to get a good compromise between the two. For instance, if you put “margin: 10px 5px 5px 0;” instead of what I have above, that would push it five more pixels further down.
And the font looking normal depends mostly on what fonts you have installed and available on your system. It’ll look like the regular font unless you have the one I installed available. You’d just have to play around with font-family list until you find a set that works more places. I was going more for brief than exhaustive in my example. :)
On 2/14/2008 at 8:03am, Joe Clark said,
You need p+p {text-indent:1em} if you want to simulate book typography. The extra space between grafs also has to go. You’ve produced the Microsoft Word version of a book.
On 2/14/2008 at 9:02am, Rob Glazebrook said,
Good point, Joe. I will say that I was only shooting for a book-style introduction, and not an entire book layout. However, now you have me intrigued to see how much of a “classic” layout I could emulate. Perhaps a follow-up post should be in the works. :) Thanks for your comment!
On 2/18/2008 at 10:33am, Richard Morton said,
Great stuff and very useful.
I am ever hopeful that one of these days a tip like this won’t say “by the way this doesn’t work in IE6″
On 2/18/2008 at 9:32pm, robglazebrook.com » Blog Archive » links for 2008-02-19 said,
[…] Book-style Chapter Introductions Using Pure CSS – CSSnewbie (tags: css tutorial typography book webdesign) […]
On 2/29/2008 at 5:05am, Best Of February 2008 | Best of the Month | Smashing Magazine said,
[…] Book-style Chapter Introductions Using Pure CSSThis article describes how you can create the scripty drop-caps at the beginnings of chapters, and the first line of small caps leading into the first full paragraph. With CSS. The technique doesn’t work in IE. […]
On 2/29/2008 at 2:19pm, Webcreatives » Book-style Chapter Introductions Using Pure CSS - CSSnewbie said,
[…] Book-style Chapter Introductions Using Pure CSS - CSSnewbie […]
On 2/29/2008 at 6:07pm, Braintrove.com said,
Nice. Also check out this article on how to “Create a Drop Cap Using CSS” (http://braintrove.com/article/29).
On 3/4/2008 at 10:49pm, Jermayn Parker said,
Best way I have seen this done before, no extra html elements. Love it!
On 3/12/2008 at 8:27pm, Kolz Blog » Blog Archive » Best Of February 2008 said,
[…] Book-style Chapter Introductions Using Pure CSS This article describes how you can create the scripty drop-caps at the beginnings of chapters, and the first line of small caps leading into the first full paragraph. With CSS. The technique doesn’t work in IE. […]
On 3/16/2008 at 10:32am, slingbox said,
http://allilworld.info/chanel-handbags/coco-chanel-purse.php
On 3/16/2008 at 10:59pm, patrolbo said,
http://allilworld.info/chanel-handbags/new-chanel-purse.php
On 3/18/2008 at 6:33am, Floroskop said,
Hello!
I think this try.
On 3/26/2008 at 7:37pm, aboxfull said,
http://hgawe.info/replica-watches/rolex-5320-watches.php
On 3/28/2008 at 3:23pm, toyboxbe said,
http://dhloi.info/bass-shoes/bass-shoes-outlet-stores.php
On 3/29/2008 at 2:03pm, justinti said,
http://ericallil.info/cheap-hotels-flagstaff.php
On 5/2/2008 at 10:38pm, utahboxe said,
http://forge.mysql.com/people/person.php?id=1505
On 5/3/2008 at 2:44am, suggesti said,
http://forge.mysql.com/people/person.php?id=1454
On 5/4/2008 at 6:38pm, mylittle said,
http://www.imeem.com/people/1NURks6
On 5/4/2008 at 6:49pm, boxoffic said,
http://forge.mysql.com/people/person.php?id=1547
On 5/4/2008 at 9:14pm, toyboxmp said,
http://www.imeem.com/people/_8hi7p7
On 5/6/2008 at 11:54pm, CSS Drop Caps 101 (with examples) | Alpha Blog Designs said,
[…] Book-Style Chapter Introductions Using CSS - CSS Newbie […]
On 5/12/2008 at 4:18am, xboxgame said,
http://www.bebo.com/RolexWatchesR
On 5/12/2008 at 8:48am, litterbo said,
http://www.bebo.com/AtenololA
On 5/12/2008 at 8:29pm, livingin said,
http://foro.shellsec.net/censurado-carisoprodol-vt4774.html
On 5/13/2008 at 12:13pm, windowbo said,
http://boards.webmd.com/?224@@7d8fce4d@
On 5/13/2008 at 5:34pm, commentb said,
http://boards.webmd.com/?224@@7d8fce9a@
On 5/13/2008 at 5:34pm, yellowbo said,
http://boards.webmd.com/?224@@7d8fcebb@
On 5/14/2008 at 1:37am, woodenje said,
http://boards.webmd.com/?224@@7d8fec2c@
On 5/14/2008 at 7:48pm, woodjewe said,
http://boards.webmd.com/?224@@7d8fffa1@
On 5/14/2008 at 7:48pm, soapboxd said,
http://boards.webmd.com/?224@@7d8fff51@
On 5/15/2008 at 12:49am, storageb said,
http://www.imeem.com/people/7fX06sg/blogs/2008/05/14/3v06bXpj/bad_credit_home_loan
On 5/15/2008 at 12:49am, toyboxes said,
http://www.imeem.com/people/7fX06sg/blogs/2008/05/14/DictGr7f/bad_credit_mortgage
On 5/15/2008 at 1:13pm, heartsha said,
http://www.bebo.com/BadCreditLoanB6
On 5/15/2008 at 9:21pm, partyina said,
http://www.ilike.com/user/Free_Ring_Tone_F
On 5/16/2008 at 4:22am, bandinab said,
http://www.ilike.com/user/Keypress_Ringtones_K
On 5/16/2008 at 5:39pm, galleryb said,
http://www.ilike.com/user/Handbags_H
On 5/16/2008 at 9:25pm, toyboxmp said,
http://boards.webmd.com/?224@@7d904f06@
On 5/17/2008 at 10:29am, freeplas said,
http://boards.webmd.com/?224@@7d904f40@
Leave a Reply