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


Pseudo-classes are keywords added to selectors that specify a special state of the selected element(s). User action pseudo-classes, in particular, apply styles to elements based on how the user is interacting with them. This allows for dynamic and responsive interfaces that provide visual feedback to the user.


:hover

The :hover pseudo-class applies styles to an element when the user's mouse cursor is positioned over it. It is commonly used to indicate that an element is interactive.

Example 1: Button Hover Effect

/* Style for a button */
.my-button {
  background-color: #007bff;
  color: white;
  padding: 10px 20px;
  border: none;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

/* Style for the button on hover */
.my-button:hover {
  background-color: #0056b3;
}

Explanation

This code changes the background color of the button when a user hovers over it, providing clear visual feedback. The transition property creates a smooth color change.


Example 2: Link Underline Effect

/* Style for a link */
.my-link {
  color: #007bff;
  text-decoration: none;
  position: relative;
}

/* Create a pseudo-element for the underline on hover */
.my-link:hover::after {
  content: '';
  position: absolute;
  width: 100%;
  height: 2px;
  background-color: #007bff;
  left: 0;
  bottom: -5px;
}

Explanation

Instead of the default underline, this example adds a custom underline using a pseudo-element when the link is hovered. This allows for more creative control over the underline's appearance.


Example 3: Image Opacity Change

/* Style for an image */
.my-image {
  opacity: 1;
  transition: opacity 0.3s ease;
}

/* Change the opacity of the image on hover */
.my-image:hover {
  opacity: 0.7;
}

Explanation

This code reduces the opacity of an image when the user's cursor is over it. This effect can be used to indicate that the image is part of a gallery or can be clicked.


Example 4: Div Shadow on Hover

/* Style for a div */
.my-card {
  width: 200px;
  height: 150px;
  background-color: #f8f9fa;
  border: 1px solid #dee2e6;
  transition: box-shadow 0.3s ease;
}

/* Add a box shadow to the div on hover */
.my-card:hover {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

Explanation

When a user hovers over the div, a subtle box-shadow is applied. This is a common technique for lifting elements on a page and indicating interactivity.


Example 5: Change Child Element Style on Parent Hover

/* Style for a container and a hidden paragraph */
.container {
  position: relative;
}

.container p {
  visibility: hidden;
  opacity: 0;
  transition: opacity 0.3s ease;
}

/* Make the paragraph visible when hovering over the container */
.container:hover p {
  visibility: visible;
  opacity: 1;
}

Explanation

This example demonstrates how hovering over a parent element (.container) can affect a child element. The paragraph inside the container becomes visible only when the container is hovered.