Adjusting the Image Contrast using CSS3


The filter property is used to set visual effects, such as drop shadow, contrast, brightness, saturation, shadow to images in CSS. The following is the syntax −

Syntax

filter: none |  drop-shadow() | blur() | brightness() | contrast() | grayscale() | hue-rotate() | invert() | opacity() | saturate() | sepia() | url();

As you can see above, with the filter property, we can set the following effects: contrast, drop shadow, blur, brightness, grayscale, hue-rotate, invert, opacity, saturate, sepia, url.

To adjust the contrast of an image in CSS3, use the contrast value for filter property. The contrast is set with a percentage value i.e.

  • Black Image: 0%

  • Black Image: Values set below 0%

  • Actual Image: 100% i.e., default

  • More contrast: Set over 100%

Let us say the following is our image −

After changing the contrast, it will appear like this −

Adjust the Contrast of an Image

Example

Let us now see an example to adjust image contrast with filter property and value contrast

<!DOCTYPE html>
<html>
<head>
   <style>
      img.demo {
         filter: brightness(120%);
         filter: contrast(120%);
      }
   </style>
</head>
<body>
   <h1>Learn MySQL</h1>
   <img src="https://www.tutorialspoint.com/mysql/images/mysql-mini-logo.jpg" alt="MySQL" width="160" height="150">
   <h1>Learn MySQL</h1>
   <img class="demo" src="https://www.tutorialspoint.com/mysql/images/mysql-mini-logo.jpg" alt="MySQL" width="160" height="150">
</body>
</html>

Image Contrast set to 0%

Example

Let us see an example when the image contrast is set to 0% −

<!DOCTYPE html>
<html>
<head>
   <style>
      img.demo {
         filter: contrast(0%);
      }
   </style>
</head>
<body>
   <h1>Learn MySQL</h1>
   <img src="https://www.tutorialspoint.com/mysql/images/mysql-mini-logo.jpg" alt="MySQL" width="160" height="150">
   <h1>Learn MySQL</h1>
   <img class="demo" src="https://www.tutorialspoint.com/mysql/images/mysql-mini-logo.jpg" alt="MySQL" width="160" height="150">
</body>
</html>

Image Contrast set to 100%

Example

Let us see another example to set the contrast to 100% −

<!DOCTYPE html>
<html>
<head>
   <style>
      img.demo {
         filter: contrast(100%);
      }
   </style>
</head>
<body>
   <h1>Learn MySQL</h1>
   <img src="https://www.tutorialspoint.com/mysql/images/mysql-mini-logo.jpg" alt="MySQL" width="160" height="150">
   <h1>Learn MySQL</h1>
   <img class="demo" src="https://www.tutorialspoint.com/mysql/images/mysql-mini-logo.jpg" alt="MySQL" width="160" height="150">
</body>
</html>

Updated on: 27-Oct-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements