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

CSS Opacity


The CSS filter property applies graphical effects like blur or color shifting to an element. The opacity() function within the filter property controls the transparency of an element. A value of 0% makes the element completely transparent, while 100% makes it fully opaque.


Example 1: Basic Opacity

.element-basic-opacity {
  /* Applies a 50% opacity to the element */
  filter: opacity(50%);
}

Explanation This code selects an element with the class .element-basic-opacity and reduces its opacity by half. This makes the element see-through, allowing any content behind it to be partially visible.


Example 2: Opacity on Hover

.element-hover-opacity {
  transition: filter 0.3s ease-in-out; /* Smooth transition for the filter effect */
}

.element-hover-opacity:hover {
  /* Makes the element fully transparent on hover */
  filter: opacity(0%);
}

Explanation This example makes an element with the class .element-hover-opacity completely disappear when the user hovers their mouse over it. The transition property ensures the change in opacity is smooth.


Example 3: Decimal Value Opacity

.element-decimal-opacity {
  /* Applies a 25% opacity using a decimal value */
  filter: opacity(0.25);
}

Explanation The opacity() function also accepts decimal values between 0 and 1. In this case, 0.25 is equivalent to 25%, making the element with the class .element-decimal-opacity mostly transparent.


Example 4: Fully Opaque

.element-full-opacity {
  /* This is the default state, making the element fully visible */
  filter: opacity(1);
}

Explanation Setting opacity() to 1 or 100% renders the element completely opaque, which is the default appearance. This can be useful for resetting an element's opacity on a specific interaction.


Example 5: Opacity on an Image

.image-opacity {
  width: 200px;
  /* Applies a 70% opacity to an image */
  filter: opacity(70%);
}

Explanation This CSS rule targets an image and applies a 70% opacity. This technique is often used to create subtle, faded image effects or to overlay text on a less distracting background image.


Example 6: Combining with Other Filters

.element-combined-filter {
  /* First applies a grayscale effect, then sets opacity to 60% */
  filter: grayscale(100%) opacity(60%);
}

Explanation The filter property allows for chaining multiple filter functions. This code first converts the element to grayscale and then applies a 60% opacity, creating a more complex visual effect.


Example 7: Opacity within a Parent Element

.parent-element:hover .child-element {
  /* Child becomes 30% opaque when hovering over the parent */
  filter: opacity(0.3);
  transition: filter 0.5s; /* Adds a smooth transition effect */
}

Explanation This example demonstrates how to change the opacity of a child element when a user interacts with its parent. When hovering over .parent-element, the .child-element within it fades to 30% opacity.