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

::Before Pseudo Element


The CSS ::before pseudo-element creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content property. This allows for adding decorative elements, custom markers, or labels without altering the HTML markup, leading to cleaner and more maintainable code.


Example 1: Adding a "New!" Badge

.new-item::before {
  content: "New!"; /* Text to be inserted */
  background-color: #ff4500; /* Orange-red background */
  color: white; /* White text color */
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 0.8em;
  margin-right: 8px; /* Space between badge and item text */
}

Explanation This code snippet adds a small "New!" badge before any element with the class new-item. It's a common technique for highlighting recent additions on a website, styled to be eye-catching.


Example 2: Custom Bullet Points

ul.custom-list li::before {
  content: "\27A4"; /* Unicode for a right-pointing arrow */
  color: #1e90ff; /* Dodger blue color */
  font-weight: bold;
  display: inline-block;
  width: 1em;
  margin-left: -1em; /* Positions the pseudo-element correctly */
}

Explanation This example replaces standard HTML list bullets with a custom arrow symbol. The content property uses a Unicode character, and negative margin is applied to align it properly.


Example 3: Numbering with CSS Counters

body {
  counter-reset: heading-counter; /* Initializes the counter */
}
h2::before {
  counter-increment: heading-counter; /* Increments the counter for each h2 */
  content: "Section " counter(heading-counter) ": "; /* Displays the counter value */
  color: #555;
}

Explanation This demonstrates using CSS counters to automatically number headings. The ::before pseudo-element displays "Section" followed by the incremented counter number, which is useful for articles or documentation.


Example 4: Adding an Icon to a Link

a.external-link::before {
  content: "\1F517"; /* Unicode for a link icon */
  font-family: "Segoe UI Symbol", sans-serif; /* Ensures icon displays */
  margin-right: 5px;
  text-decoration: none; /* Prevents underline on icon */
}

Explanation This code adds a link icon before any anchor tag with the class external-link. It provides a visual cue to users that the link will take them to an external site.


Example 5: Decorative Separator

.separator::before {
  content: "---";
  color: #ccc;
  letter-spacing: 0.5em; /* Spreads out the dashes */
  display: block;
  text-align: center;
  margin-bottom: 15px;
}

Explanation Here, ::before is used to create a simple, decorative separator. This is a lightweight alternative to using an image or a border for stylistic division between content blocks.


Example 6: Adding Quotes to a Blockquote

.custom-quote::before {
  content: "\201C"; /* Unicode for a left double quotation mark */
  font-size: 3em;
  font-weight: bold;
  color: #ddd;
  float: left;
  margin-top: -0.2em;
  margin-right: 0.1em;
}

Explanation This CSS adds a large, decorative opening quotation mark to a blockquote element. It enhances the visual presentation of quoted text, making it stand out from the regular paragraph content.


Example 7: Breadcrumb Separators

.breadcrumb a::before {
  content: "/";
  padding: 0 10px;
  color: #6c757d;
}

.breadcrumb a:first-child::before {
  content: ""; /* Removes separator before the first item */
  padding: 0;
}

Explanation This code inserts a slash separator before each link in a breadcrumb trail, except for the very first one. This is a classic, clean method for building navigation paths without extra HTML elements.