CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

::First Letter


The ::first-letter pseudo-element in CSS is used to apply styles to the very first letter of the first line of a block-level element. This is perfect for creating drop caps and other typographic effects to enhance readability and visual interest in your text. Only a specific subset of CSS properties can be used with this pseudo-element.


Example 1: Basic Drop Cap

/* CSS */
p::first-letter {
  font-size: 3em; /* Makes the first letter three times larger */
  color: #2a6496;  /* Sets a distinct blue color */
  font-weight: bold; /* Makes the letter bold */
  float: left; /* Allows text to wrap around the letter */
  margin-right: 0.1em; /* Adds space to the right of the letter */
  line-height: 1; /* Adjusts the line height for proper alignment */
}

Explanation This code targets the first letter of any paragraph (<p>) element. It creates a classic "drop cap" effect by increasing the font size, changing the color, and using float: left to position it within the flow of the text.


Example 2: Styling with Background and Padding

/* CSS */
article::first-letter {
  background-color: #f0ad4e; /* Adds a yellow background */
  color: white; /* Sets the letter color to white */
  padding: 0.2em; /* Adds padding around the letter */
  border-radius: 5px; /* Rounds the corners of the background */
  font-family: 'Georgia', serif; /* Uses a serif font for a classic look */
}

Explanation Here, the first letter of an <article> is styled with a background color and padding. This technique makes the initial letter stand out as if it were an illuminated manuscript letter.


Example 3: Targeting a Specific Class

/* CSS */
.intro::first-letter {
  font-size: 250%; /* Increases font size by 250% */
  color: #d9534f; /* Applies a red color */
  font-style: italic; /* Makes the letter italic */
}

Explanation This example demonstrates how to apply the ::first-letter style only to elements with a specific class, in this case, .intro. This allows for more targeted and controlled styling within your document.


Example 4: Using a Different Font Family

/* CSS */
h2::first-letter {
  font-family: 'Playfair Display', serif; /* Uses an elegant display font */
  font-size: 1.5em; /* A subtle size increase */
  color: darkslateGray; /* A sophisticated, dark color */
}

Explanation This CSS targets the first letter of <h2> headings. It applies a different, more decorative font family to create a stylish typographic hierarchy.


Example 5: Combining with Text Shadow

/* CSS */
blockquote::first-letter {
  font-size: 4rem; /* A large font size */
  color: #333; /* A dark grey color */
  text-shadow: 2px 2px 3px #aaa; /* Adds a subtle shadow for depth */
  float: left;
  margin: 0 0.2em 0 0;
}

Explanation This code styles the first letter of a <blockquote>. A text-shadow is added to give the large initial letter a sense of depth and make it pop off the page.


Example 6: Removing Bold from Headers

/* CSS */
h1::first-letter {
  font-weight: normal; /* Overrides the default bold of the h1 */
  color: #5cb85c; /* A green color */
  margin-right: 4px; /* Adds a small margin */
}

Explanation Sometimes, you might want the first letter to be styled differently but not bold. This example shows how to override the default font-weight of an <h1> element for just the first letter.


Example 7: Border and Margin Styling

/* CSS */
.content-box::first-letter {
  font-size: 3em;
  color: #46b8da;
  float: left;
  line-height: 0.8;
  margin: 0.1em 0.1em 0 0; /* Top, Right, Bottom, Left margins */
  border-bottom: 3px solid #46b8da; /* Adds a border below the letter */
}

Explanation This style adds a decorative border to the bottom of the first letter. Combined with careful margin and line-height adjustments, it creates a unique and modern typographic effect.