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

Hover


The hover media feature checks if the user's primary input mechanism can conveniently hover over elements. This is key for creating hover effects that don't break the user experience on touch-only devices.

Example 1: Hover Capability Detection

/* styles.css */
/* Apply styles only if the primary input mechanism can hover */
@media (hover: hover) {
  .link:hover {
    /* Underline link on hover for devices that support it */
    text-decoration: underline;
  }
}

Explanation

This CSS applies a text underline to a .link element only when the user hovers over it on a device that supports hovering, like one with a mouse. This prevents the style from being permanently and incorrectly applied on tap-based devices.


Example 2: No Hover Capability

/* styles.css */
/* Apply styles if the primary input mechanism cannot hover */
@media (hover: none) {
  .tooltip-trigger .tooltip-text {
    /* Make tooltips always visible on devices without hover */
    opacity: 1;
    visibility: visible;
  }
}

Explanation

For devices that cannot hover (like most smartphones), this code makes tooltips (.tooltip-text) permanently visible. This ensures that information hidden within a hover state is still accessible.


Example 3: Combining Hover with Pointer

/* styles.css */
/* Targets devices that can hover and have a fine pointer */
@media (hover: hover) and (pointer: fine) {
  .dropdown-menu {
    /* Show dropdown on hover for mouse users */
    display: none;
  }
  .dropdown:hover .dropdown-menu {
    display: block;
  }
}

Explanation

This example targets the classic desktop experience. A dropdown menu is hidden by default and only appears when a user with a mouse hovers over the .dropdown container.


Example 4: Alternative Interaction for No-Hover

/* styles.css */
/* Provide a different experience for devices that don't support hover */
@media (hover: none) {
  .info-card .details {
    /* Ensure details are always expanded on touch devices */
    max-height: 500px;
  }
}

Explanation

On devices where hover is not the primary interaction method, this CSS ensures that the .details section within an .info-card is always expanded, preventing content from being inaccessible.


Example 5: Using any-hover for Secondary Inputs

/* styles.css */
/* Apply styles if any available input mechanism can hover */
@media (any-hover: hover) {
  .subtle-highlight:hover {
    /* Add a background color on hover, even if it's a secondary input */
    background-color: #f0f0f0;
  }
}

Explanation

any-hover is useful for devices that might have multiple input methods (e.g., a touchscreen laptop with a mouse). This code applies a hover effect if any of the user's input devices support it.