Open In App

Create a Drop Caps Effect using CSS

Last Updated : 24 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Drop caps are defined as the first capital letter of a paragraph in a much larger font size that has the depth of two or more lines of normal text. 

The task can be done by using the CSS ::first-letter pseudo-element to create a beautiful drop caps effect. The ::first-letter selector in CSS is used to apply style to the first letter of the first line of a block-level element, the condition is it should not be preceded by other content.

Syntax:

::first-letter {
// CSS Property
}

Approach

  • The HTML document defines the document type and version.
  • Within the <head> section, a <style> block is included to define CSS rules for the document, specifically targeting the first letter of elements with the gfg class and all <div> elements.
  • The <body> section is styled with text-align: center and includes a main heading <h1> with green text explaining the drop caps effect using CSS.
  • The <p> tag contains multiple <div> elements, each displaying the words “Geeks”, “For”, and “Geeks” respectively, with the first letter styled to be larger and blue, and bold for <div> elements.
  • Another <p> element with the class gfg is present, displaying the text “A computer science portal for geeks.” with its first letter styled to be larger and green due to the .gfg::first-letter CSS rule.

Example: The example shows the above-explained approach.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        .gfg::first-letter {
            font-size: 250%;
            color: green;
        }

        div::first-letter {
            font-size: 250%;
            color: blue;
            font-weight: bold;
        }
    </style>
</head>

<body style="text-align: center;">
    <h1 style="color:green;">
        How to create drop caps 
        effect using CSS
    </h1>

    <p>
        <div>Geeks</div>
        <div>For</div>
        <div>Geeks</div>
    </p>
    
    <p class="gfg">
        A computer science portal for geeks.
    </p>
</body>

</html>

Output:



Next Article

Similar Reads