DEV Community

Cover image for The Magic of CSS Filters and Hover Effects
Elliot Brenya sarfo
Elliot Brenya sarfo

Posted on • Edited on

The Magic of CSS Filters and Hover Effects

CSS filters are so powerful to the extent that it can transform image into so many forms without distorting the image.

In this post, we'll be exploring the various CSS filters available and how you can use them to enhance the appearance of your images. We'll also be discussing how to add hover effects to your images to make them more interactive and engaging for your website's visitors.

So, if you're ready to level up your website's visuals, keep reading!

Before we dive into the code, let's first understand what CSS filters are. Simply put, CSS filters allow you to manipulate the appearance of an element, such as an image, by applying a visual effect. These effects can range from adjusting the brightness and contrast of an image to adding blur or changing its color hue.

Now, let's take a look at some of the common CSS filters that are available:

brightness: This filter allows you to adjust the overall brightness of an image. A value of 100% will maintain the original brightness, while values above 100% will increase the brightness and values below 100% will decrease it.

contrast: This filter allows you to adjust the overall contrast of an image. A value of 100% will maintain the original contrast, while values above 100% will increase the contrast and values below 100% will decrease it.

grayscale: This filter converts an image to grayscale, removing all its color information. A value of 100% will fully convert the image to grayscale, while values below 100% will leave a hint of color in the image.

hue-rotate: This filter allows you to rotate the hue of an image by a specified degree. For example, a value of 90deg will rotate the hue by 90 degrees clockwise, while a value of -90deg will rotate it counterclockwise.

invert: This filter inverts the colors of an image, creating a negative effect. A value of 100% will fully invert the colors, while values below 100% will partially invert them.

saturate: This filter allows you to adjust the saturation of an image. A value of 100% will maintain the original saturation, while values above 100% will increase the saturation and values below 100% will decrease it.

sepia: This filter converts an image to sepia, giving it a warm, vintage effect. A value of 100% will fully convert the image to sepia, while values below 100% will leave a hint of color in the image.

blur: This filter adds blur to an image, making it appear out of focus. The larger the value, the more blur is applied to the image.

Now that we have a basic understanding of the different CSS filters available, let's see how we can implement them in our code.

Image description

First, let's set up our HTML structure. We'll create a div with a class of "group" that contains multiple divs with a class of "item". Each "item" div will have a unique background image:



<div class="group">
  <div class="item" style="background-image: url(image1.jpg)"></div>
  <div class="item" style="background-image: url(image2.jpg)"></div>
  <div class="item" style="background-image: url(image3.jpg)"></div>
  <!-- more items -->
</div>


Enter fullscreen mode Exit fullscreen mode

Next, let's add some basic styling to our "item" divs. We'll set their width, height, background position, and background size:



.item {
  width: 15vw;
  height: 75vh;
  background-position: center;
   background-size: 75vh;
}


Enter fullscreen mode Exit fullscreen mode

Now, let's add the hover effect and CSS filters to our "item" divs. We'll start by adding a hover effect that expands the width of the div on hover. Then, we'll add a different CSS filter to each "item" div using the "nth-of-type" selector.



.item:hover {
  transition: width 0.5s ease;
  width: 25vw;
}

.item:hover:nth-of-type(1) {
  filter: brightness(110%);
}

.item:hover:nth-of-type(2) {
  filter: contrast(110%);
}

.item:hover:nth-of-type(3) {
  filter: grayscale(100%);
}


Enter fullscreen mode Exit fullscreen mode

And that's it! Now, when you hover over each "item" div, the width will smoothly expand and the corresponding CSS filter will be applied.

CSS filters are a powerful tool for enhancing the visual appeal of your website's images. By experimenting with different combinations of filters and hover effects, you can create unique and eye-catching designs that will surely stand out to your website's visitors.

I hope this post has helped you understand the basics of CSS filters and how you can use them to enhance your website's visuals.

Get source code here https://codepen.io/chpo1530/pen/YzjNQyz

Top comments (0)