In this tutorial, we are going to have some fun. We are going to focus on the first-letter selector and along the way pick up a bag of CSS selector tricks.
Let's start right away. Copy and paste this code into your favorite text editor and bring it up with your browser. Here is how we can use the first-letter selector to make our page look like a Once Upon A Time book with a gigantic letter to start things off.
body { background-color: #eee; line-height: 1.5; } #page { margin: 2% auto; padding: 1%; width: 360px; background-color: white; overflow: hidden; } p:first-letter { float: left; /* float to let text wrap around it */ font-size: 3em; color: #333; } </style> </head> <body> <div id="page"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </div> </body> </html>
In Safari 5.1.2, Opera 11.61, Internet Explorer 9.08 we get this look where the first letter is well below the top of the paragraph box:
In Firefox 10.0.2 we get this rendering that looks, well, correct.
Turns out, to make the other browsers render like Firefox, you have to add a line-height setting:
p:first-letter { float: left; /* float to let text wrap around it */ font-size: 3em; color: #333; line-height: 0.6em; }
More CSS Selector and first-letter fun
Let's assume we have a container page div wrapping two row divs of one paragraph each. Lets try some css on it.
<div id="page"> <div id="row1"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was pop</p> </div> <div id="row2"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was pop</p> </div> </div>
This CSS code will select every paragraph's first letter and make it big.
This CSS code will only make row1's first paragraph 's first letter big:
#row1 p:first-letter { float: left; /* float to let text wrap around it */ font-size: 3em; color: #333; line-height: 0.6em; }
Notice that in this example above, we had a container to help us out with selection (div.row1). Each row was a wrapper for a paragraph. This made it possible to select the first paragraph in the page. But this was a structural trick. Normally when you using a content management system, most people are not going to write their blog posts with div markup in it.
Suppose row1 had multiple paragraph tags in it.
<div id="row1"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p> <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was pop</p> </div>
How would you markup the first paragraph's first letter and not the other paragraphs within it? Here's how:
p { margin-bottom: 1em; } #row1 { border: 1px solid green; margin-bottom: 1em; } #row2 { border: 1px solid red; margin-bottom: 1em; } #row1 p:first-child:first-letter { float: left; /* float to let text wrap around it */ font-size: 3em; color: #333; line-height: 0.6em; }
By specifying row1's first paragraph as the first child and using the first-letter selector, you can get the same effect without touching the other paragraphs. Note that I put a red border around row2 to distinguish it from row1 in green.
Lets assume we have four paragraphs in our row1 div:
<div id="row1"> <p>Aorem Ipsum is simply dummy text of the printing and typesetting industry.</p> <p>Bt has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was pop</p> <p>Corem Ipsum is simply dummy text of the printing and typesetting industry.</p> <p>Dt has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was pop</p> </div>
And what will this code do?
#row1 p:nth-child(2n+2):first-letter { float: left; /* float to let text wrap around it */ font-size: 3em; color: #333; line-height: 0.6em; }
This uses the nth-child operator which takes a mathematical expression and iterates through the tree by running n=0 to the end of the child list. In this case, every even numbered paragraph of row1 will get a big fat letter.
Finally, lets take an example that is common in content layouts - the multi column structure. This is one demonstrated in books and magazine layouts where the first letter of the first paragraph has a big letter. To pull this sort of layout out
we have to use a div structure with the same class name:
<div id="page"> <div class="col"> <p>Aorem Ipsum is simply dummy text of the printing and typesetting industry.</p> </div> <div class="col"> <p>Aorem Ipsum is simply dummy text of the printing and typesetting industry.</p> </div> </div>
.col{ float: left; width: 48%; margin-right:2%; }
To get at the first letter in the first div, there are three ways from clever to not so clever.
We can use the first-of-type selector (thanks to Tony at CSSCreator) on the class col like so:
div.col:nth-of-type(1) p:first-child:first-letter { float: left; /* float to let text wrap around it */ font-size: 3em; color: #333; line-height: 0.6em; }
Or lastly, mark the first div with "first":
<code> <div id="page"> <div class="col first"> <p>Aorem Ipsum is simply dummy text of the printing and typesetting industry.</p> </div> <div class="col"> <p>Aorem Ipsum is simply dummy text of the printing and typesetting industry.</p> </div> </div>
.col.first p:first-child:first-letter { float: left; /* float to let text wrap around it */ font-size: 3em; color: #333; line-height: 0.6em; }
Summary
In this tutorial you learned about the first-letter selector. In particular how to style:
Every first letter in a paragraph
Only the first paragraph through wrapper
Only the first paragraph via the first-child selector
The first letter of any paragraph via a mathematical expression
A multi column layout to set the first paragraph's first letter