How to Style Small Caps Text with CSS

Introduction

Styling text as small caps in CSS can give it a more formal or decorative look. Small caps transform lowercase letters into smaller uppercase letters while keeping actual uppercase letters larger.

In this tutorial, you'll learn how to style text as small caps using the font-variant property in CSS. This technique is commonly used for headings or to add emphasis in specific parts of a document.

Problem Statement

Create a CSS code that:

  • Styles text as small caps using the font-variant property.
  • Demonstrates how to apply the small caps effect to different HTML elements.

Example:

  • Input: A paragraph element with the text "Small Caps Example".
  • Output: The text is displayed in small caps.

Solution Steps

  1. Use font-variant: small-caps: Apply the font-variant property to transform lowercase letters into small caps.
  2. Apply to Different Elements: Use font-variant on various HTML elements like p, h1, or span for different text styles.

HTML Structure

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Small Caps Text Example</title>
    <style>
        /* Step 1: Style text with small caps */
        .small-caps-text {
            font-variant: small-caps;
        }

        /* Additional styling for headings with small caps */
        .small-caps-heading {
            font-variant: small-caps;
            font-size: 2rem;
        }
    </style>
</head>
<body>

    <p class="small-caps-text">This text is styled in small caps using font-variant: small-caps.</p>
    <h1 class="small-caps-heading">This heading is in small caps.</h1>

</body>
</html>

Output

You can play with the above HTML in Online HTML Editor and Compiler. Here is the output of the above HTML page.:

Explanation

Step 1: Use font-variant: small-caps

  • To transform lowercase letters into small caps, use the following CSS:
    .small-caps-text {
        font-variant: small-caps;
    }
    

Step 2: Apply Small Caps to Headings

  • You can also apply small caps to headings or other elements for a more prominent effect:
    .small-caps-heading {
        font-variant: small-caps;
        font-size: 2rem;
    }
    

Conclusion

Styling small caps text in CSS is easy using the font-variant: small-caps property. This technique gives your text a refined, formal look, and can be applied to both paragraphs and headings to enhance the visual hierarchy of your content.

Comments