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

Prefers Color Scheme


The prefers-color-scheme media feature is used to detect if the user has requested a light or dark color theme in their operating system or browser settings. This is essential for creating user-friendly dark modes.

Example 1: Dark Mode Styles

/* styles.css */
/* Styles for users who prefer a dark color scheme */
@media (prefers-color-scheme: dark) {
  body {
    background-color: #121212;
    color: #ffffff;
  }
}

Explanation

This code checks if the user's system is set to a dark theme. If so, it changes the body background to a dark color and the text color to light, creating a basic dark mode.


Example 2: Light Mode Styles

/* styles.css */
/* Explicitly define light mode styles for users who prefer it */
@media (prefers-color-scheme: light) {
  body {
    background-color: #ffffff;
    color: #333333;
  }
}

Explanation

While often the default, explicitly defining light theme styles can be useful. This ensures a consistent light theme for users who have specifically chosen it in their system preferences.


Example 3: Overriding Dark Mode for a Specific Element

/* styles.css */
/* Force a light theme for a specific component, even in dark mode */
@media (prefers-color-scheme: dark) {
  .light-widget {
    background-color: #f8f9fa;
    color: #212529;
    border: 1px solid #dee2e6;
  }
}

Explanation

This example shows how to keep a specific element, .light-widget, styled with a light theme even when the rest of the page is in dark mode. This can be useful for embedded content or ads.


Example 4: Adjusting Images for Dark Mode

/* styles.css */
/* Dim images slightly in dark mode to reduce brightness */
@media (prefers-color-scheme: dark) {
  img {
    filter: brightness(0.8) contrast(1.2);
  }
}

Explanation

Bright images can be jarring in a dark theme. This CSS applies a filter to all img elements in dark mode, slightly reducing their brightness and increasing contrast for better visual comfort.


Example 5: Using CSS Custom Properties for Theming

/* styles.css */
/* Define color variables for light and dark themes */
:root {
  --bg-color: #fff;
  --text-color: #000;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-color: #222;
    --text-color: #eee;
  }
}

body {
  background-color: var(--bg-color);
  color: var(--text-color);
}

Explanation

This is a modern and efficient way to handle theming. It defines CSS Custom Properties (variables) for colors and then redefines them within the prefers-color-scheme: dark media query, making theme management clean and scalable.