Greytree

TamWiki

For a mouse who is a packrat

Technology » Drop Caps With CSS
An old typography trick updated to the web

Summary:this is what goes at the top of the site

(redirected from Main.DropCapsWithCSS)

CSS is a powerful little language for specifying all sorts of things you can do with layouts and elements on a web page. Here we look at bringing an old typography trick for books to the modern web: '''Drop Caps".

Drop Caps are pretty much what the name implies: the first letter of a paragraph is enlarged and dropped to the left of the first paragraph on the page. This is seen in books, newspapers, and magazines all over. It is a classic design trick that can be successfully brought to a web page to give it a bit of elegance.

:first-letter pseudo-class

The trick is to use the pseudo-class :first-letter on the paragraph you are styling. This selector allows you to just pick the first letter off a paragraph without the need for spans. An example shown here:

  1. .firstpar:first-letter { /* this creates a drop cap */
  2.     font-family Futura, Trebuchet, "MS Trebucht" sans-serif;
  3.     font-size: 200%;
  4.     font-weight: bold;
  5.     float: left;
  6.     padding-left: 0;
  7.     padding-top: 5px;
  8.     padding-right: 5px;
  9.     padding-bottom: 5px;
  10. }

.firstpar is a class we've created that is applied to the first paragraph tag (<p class="firstpar">) to get it to react to the definition we're providing. Appending the pseudo-class :first-letter to the class name gives us further control over what is affected.

The letter is given a type face and size commensurate with a dropped cap in this design. I chose to use the same type face as the heading font for page consistency. We make it big and bold. Next comes the tricky part: we make this character float left on the paragraph. This is what makes it a drop cap. When you float the character, it becomes a block display, and drops down compared to the rest of the first line. But it's not quite there yet. We have to add some padding to make it go into just the right position and give it a little space in which to sit. We want it to go flush to the left edge of the container, but give it a little easement around the other 3 edges. If we don't specify some top padding, the character actually will float up a bit if you've got any padding before the paragraph itself. The other padding (right, bottom) are just to give it a comfy space from the rest of the paragraph body.

Here's the corresponding html:

  1. <body>
  2.     <h1>This is an example of how to do a drop cap</h1>
  3.     <p class="firstpar">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
  4.     sed do eiusmod tempor incididunt ut labore et dolore magna
  5.     aliqua. Ut enim ad minim veniam, quis nostrud exercitation
  6.     ullamco laboris nisi ut aliquip ex ea commodo consequat.
  7.     Duis aute irure dolor in reprehenderit in voluptate velit
  8.     esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
  9.     occaecat cupidatat non proident, sunt in culpa qui officia
  10.     deserunt mollit anim id est laborum.</p>
  11.     <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
  12.     sed do eiusmod tempor incididunt ut labore et dolore magna
  13.     aliqua. Ut enim ad minim veniam, quis nostrud exercitation
  14.     ullamco laboris nisi ut aliquip ex ea commodo consequat.
  15.     Duis aute irure dolor in reprehenderit in voluptate velit
  16.     esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
  17.     occaecat cupidatat non proident, sunt in culpa qui officia
  18.     deserunt mollit anim id est laborum.</p>
  19.  
  20. </body>

Here's what our example looks like rendered:


Tags: Categories: Articles

Recent Changes | Printable View | Page History | Edit Page
Page last modified on April 09, 2012, at 09:59 AM by tamara

Attachments: