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 Filter Contrast()


The CSS filter property applies graphical effects like blurring or color shifting to an element. The contrast() function is a part of the filter property and is used to adjust the contrast of an element. A value of 0% will create an image that is completely gray, while a value of 100% will leave the image unchanged. Values greater than 100% will increase the contrast.


Example 1: No Contrast

.no-contrast {
  /* This sets the contrast of the image to 0% */
  filter: contrast(0%);
}

Explanation

This code snippet targets an element with the class no-contrast. It applies a contrast value of 0%, which removes all contrast, resulting in a completely gray element.


Example 2: Half Contrast

.half-contrast {
  /* This reduces the contrast of the image by 50% */
  filter: contrast(50%);
}

Explanation

Here, the half-contrast class sets the contrast level to 50%. This makes the dark areas lighter and the light areas darker, reducing the overall tonal difference in the element.


Example 3: Default Contrast

.default-contrast {
  /* This leaves the image contrast unchanged */
  filter: contrast(100%);
}

Explanation

The default-contrast class applies a contrast value of 100%. This is the default value, so the element's appearance will not change.


Example 4: Increased Contrast

.increased-contrast {
  /* This increases the contrast of the image by 50% */
  filter: contrast(150%);
}

Explanation

This CSS rule targets the increased-contrast class and increases the contrast by 50% over the original. This makes the dark areas of the element darker and the light areas lighter, creating a more pronounced visual distinction.


Example 5: Doubled Contrast

.doubled-contrast {
  /* This doubles the contrast of the image */
  filter: contrast(200%);
}

Explanation

The doubled-contrast class sets the contrast to 200%. This doubles the existing contrast, resulting in a very sharp and dramatic visual effect on the element.


Example 6: Using a Decimal Value

.decimal-contrast {
  /* This increases the contrast slightly using a decimal value */
  filter: contrast(1.2);
}

Explanation

The contrast() function also accepts decimal values. In the decimal-contrast class, a value of 1.2 is equivalent to 120%, providing a slight boost in contrast.


Example 7: Combining with Other Filters

.combined-filter-contrast {
  /* This applies both a contrast and a grayscale filter */
  filter: contrast(175%) grayscale(50%);
}

Explanation

You can apply multiple filter functions at once. The combined-filter-contrast class first increases the contrast to 175% and then applies a 50% grayscale effect to the element.