Also called initial letters, initial caps are enlarged characters that are used as the first letter of a paragraph. Initial caps can be in a different font, different color, or you could even use images instead! In this tutorial we’re going to learn how to create initial caps using special HTML markup, CSS styling, and then we’re going to automate the process with a PHP function.
The HTML
In order to style the first letter of a paragraph differently, we need to wrap it inside a special HTML element that we can later style by adding a few lines of CSS. For this tutorial I’m going to use a simple span element, with the “initialcap” class. The resulting paragraph will look like this:
<span class="initialcap">L</span>orem ipsum dolor sit amet, ...
Remember not to repeat the letter after the span
The CSS
Here comes the tricky part, due to the fact that every theme has different font size and line height values you’ll have to adapt this part to your theme. I’m going to assume that our paragraph has a font-size of 14 pixels and a line height of 18 pixels.
1. Float! We need to make our capital letter float left in order for the paragraph text to flow around it.
span.initialcap{ float: left; }
2. Font size. We assign it a font size of 40 pixels (again, this may vary depending on your theme’s CSS), and a line height of 35 pixels, which is twice the line height of the paragraph minus 1 pixel (just in case).
span.initialcap{ float: left; font-size: 40px; line-height: 35px; /* Double the line height minus 1 pixel */ }
3. Extra styling. We’re going to use a serif font, make it blue, and add a little bit of spacing on the right.
span.initialcap{ float: left; font-size: 40px; line-height: 35px; /* Double the line height minus 1 pixel */ font-family: Georgia, "Times New Roman", Times, serif; color: #2583ad; /* Blue */ padding-right: 5px; }
You’re probably going to have to play a little bit with the font size and line height values though.
Automation
Ok, so you’ve added the necessary CSS to your style.css file, but isn’t it a bit of a hassle to have to add the HTML markup every time you write a new post? Well, this bit of PHP code will do this for you:
function wpguy_initial_cap($content){ // Regular Expression, matches a single letter // * even if it's inside a link tag. $searchfor = '/>(<a [^>]+>)?([^<s])/'; // The string we're replacing the letter for $replacewith = '>$1<span class="capital">$2</span>'; // Replace it, but just once (for the very first letter of the post) $content = preg_replace($searchfor, $replacewith, $content, 1); // Return the result return $content; } // Add this function to the WordPress hook add_filter('the_content', 'wpguy_initial_cap');
How does it work? Basically, our function takes the content of the blog post, and wraps the first letter with our special markup.
All you need to do is paste it inside the functions.php file of your theme.
I hope you enjoyed this tutorial! Remember to post your thoughts and questions in the Comments section.

Blog reactions