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

Mix Blend Mode


The mix-blend-mode CSS property dictates how an element's content should blend with the content of the element's direct parent and the element's background. This powerful tool allows for the creation of visually rich effects, such as image overlays and dynamic text treatments, by combining colors in various ways.


Example 1: normal

/* In this example, the h1 element has a red background color and is blended with a div element that has a blue background color. The mix-blend-mode is set to normal, which is the default value. This means that the top layer (the h1) will simply be placed on top of the bottom layer (the div), without any color blending. */
.parent-element {
  background-color: blue;
  width: 300px;
  height: 300px;
}

.child-element-1 {
  background-color: red;
  mix-blend-mode: normal; /* Default value, no blending */
  color: white;
  text-align: center;
  padding: 50px;
}

Explanation

The normal value is the default setting for mix-blend-mode. It renders the element without any blending, meaning the top element's content will obscure the content of the elements beneath it. This is the standard behavior for elements on a webpage.


Example 2: multiply

/* Here, the multiply value is used. This blend mode multiplies the colors of the top and bottom layers. The resulting color is always darker. White color in either layer will result in the other layer's color, while black will result in black. */
.parent-element {
  background-image: url('https://via.placeholder.com/300');
}

.child-element-2 {
  background-color: #ff6347; /* A shade of tomato */
  mix-blend-mode: multiply;
  height: 300px;
  width: 300px;
}

Explanation

The multiply blend mode multiplies the luminance of the base color by the blend color. The resulting color is always as dark as or darker than the original. This mode is effective for creating shadow effects or for coloring grayscale images.


Example 3: screen

/* The screen value inverts the colors, multiplies them, and then inverts the result. This leads to a brighter outcome. It's the opposite of multiply and is great for creating highlights or glow effects. */
.parent-element {
  background-image: url('https://via.placeholder.com/300/0000FF/808080');
  background-size: cover;
}

.child-element-3 {
  background-color: #00ffff; /* Cyan */
  mix-blend-mode: screen;
  height: 300px;
  width: 300px;
}

Explanation

The screen blend mode inverts both layers' colors, multiplies them, and then inverts the result. This produces a lighter color and is useful for creating glowing effects or for making dark images brighter. Black in either layer will not change the other layer's color.


Example 4: overlay

/* Overlay combines the multiply and screen blend modes. If the bottom layer is dark, the top layer is multiplied. If the bottom layer is light, the top layer is screened. This creates a result that preserves the highlights and shadows of the bottom layer. */
.parent-element {
  background-image: url('https://via.placeholder.com/300');
}

.child-element-4 {
  background: linear-gradient(to right, #ff0000, #0000ff); /* Red to blue gradient */
  mix-blend-mode: overlay;
  height: 300px;
  width: 300px;
}

Explanation

The overlay blend mode is a combination of multiply and screen. It darkens dark areas and lightens light areas, increasing the contrast of the underlying layer. It is commonly used to superimpose patterns or textures onto images.


Example 5: darken

/* The darken blend mode compares the color of the top and bottom layers and selects the darker of the two. It does this for each of the red, green, and blue channels separately. */
.parent-element {
  background-color: #ffff00; /* Yellow */
  padding: 50px;
}

.child-element-5 h2 {
  color: #ff00ff; /* Magenta */
  mix-blend-mode: darken;
  font-size: 3rem;
  text-align: center;
}

Explanation

The darken blend mode compares the color channels of the blend and base layers and selects the darker of the two. It does not produce new colors, only selects from the existing ones. This is useful for placing dark text over a multi-colored background.


Example 6: lighten

/* Lighten is the opposite of darken. It compares the colors of the top and bottom layers and selects the lighter of the two. This is also done on a per-channel basis. */
.parent-element {
  background-image: url('https://via.placeholder.com/300/4B0082/FFFFFF');
  background-size: cover;
}

.child-element-6 {
  background-color: #ffa500; /* Orange */
  mix-blend-mode: lighten;
  height: 300px;
  width: 300px;
}

Explanation

The lighten blend mode compares the color channels of the blend and base layers and selects the lighter of the two. Similar to darken, it does not create new colors. This is effective for placing light-colored elements over various backgrounds.


Example 7: difference

/* The difference blend mode subtracts the darker color from the lighter color. The result is often a stark and inverted color effect. White will invert the color of the other layer, while black will have no effect. */
.parent-element {
  background-color: #00ff00; /* Green */
}

.child-element-7 {
  background-color: #ff0000; /* Red */
  mix-blend-mode: difference;
  height: 300px;
  width: 300px;
}

Explanation

The difference blend mode subtracts the darker of the two colors from the lighter one. This is useful for creating high-contrast, artistic effects. It can be used to see the variations between two images.