7 - Creating Cool CSS Image Effects
7 - Creating Cool CSS Image Effects
But manual graphic editing tools don't work when we have to do this for
hundreds of thousands of images. In that case, web developers use CSS to
serve cool image effects without any significant dependency on graphic
designers.
This article will lay out some of the commonly used CSS image effects that
will help you create a unique visual experience for your users. With several
examples, you'll learn how to apply effects and animation to photos using
CSS techniques; they are as follows:
Now that we have an image to work with let's use it in an HTML document,
as shown below. And don't forget to replace the image src with your unique
ImageKit URL or any other image source.
<img src="image/new-york_lbAbZZKZG.jpg" alt="new york">
img {
-webkit-filter: grayscale(100%); /* Safari 6.0 - 9.0 */
filter: grayscale(100%);
}
The unit of the blur() function can be any of px (pixels), rem (relative to the
root font size of HTML document), em (relative to the font size of the parent
element), or percentage (%), depending on your choice.
Here is the CSS for the blurred image effect.
img {
-webkit-filter: blur(5px); /* Safari 6.0 - 9.0 */
filter: blur(5px);
}
As the name suggests, the image shadow effect allows us to draw a shadow
around an HTML element using the box-shadow property. A box-shadow is
described by X and Y offsets relative to the element, blur and spread radius,
and color.
In the example below, we have created a card from our sample image and
added a shadow to it using the box-shadow property. In the CSS snippet below,
see the box-shadow property added for the class card for the values of x and y
offsets, blur, spread, and shadow color.
HTML to create a card:
<div class="card">
<img src="image/new-york_lbAbZZKZG.jpg" alt="new york" />
<div class="card-info">
New York is a city in the United States of America.
</div>
</div>
The final result of the above is a beautiful card with shadows that make it
look slightly raised compared to the rest of the page.
Image Rounded Corners
Rounding the corners of images and, in general, HTML elements is another
popular effect used on the web. Some use cases of rounded corner effects are
profile images, content cards, and buttons.
Since in the above example, the four corners have the same values, the border-
radius property can be rewritten using the CSS shorthand method, as shown
below.
border-radius: 15px;
This shorthand method applies a radius of 15px to all four corners of the
image.
The values of each individual corner radius can be specified using their
dedicated CSS properties as well.
border-top-left-radius: 10px;
border-top-right-radius: 20px;
border-bottom-right-radius: 30px;
border-bottom-left-radius: 40px;
/* shorthand */
border-radius: 10px 20px 30px 40px;
You can use any CSS units such as the px, rem, em, or percentage (%) to
specify the radius.
To create a rounded corner image, let's apply a radius of 50px to our image
using the shorthand method as shown below.
img{
border-radius: 50px;
}
Image Rotation with CSS Transform
img {
width: 300px;
transform: rotate(25deg); /* rotate the image in 25 degrees */
}
The transform rotate property can also take a negative value to rotate the
image in an anti-clockwise direction, as shown below:
img {
transform: rotate(-25deg); /* rotate the image in -25 degress */
}
We will now use these clockwise and anti-clockwise rotations using the
CSS transform property to create a vibration effect when we hover on the
image.
To create an animation, we need to use CSS Keyframes. Keyframes allow us
to define the state of an element during an animation. In the example below,
we have used small rotations at fixed intervals of 10% of the animation
duration. vibrate is the name we have assigned to the keyframe here.
@keyframes vibrate {
0% { transform: rotate(0deg); }
10% { transform: rotate(-1deg); }
20% { transform: rotate(1deg); }
30% { transform: rotate(0deg); }
40% { transform: rotate(1deg); }
50% { transform: rotate(-1deg); }
60% { transform: rotate(0deg); }
70% { transform: rotate(-1deg); }
80% { transform: rotate(1deg); }
90% { transform: rotate(0deg); }
100% { transform: rotate(-1deg); }
}
We can now apply the vibrate keyframe to our image on hover as shown
below. The infinite value of animation-iteration-count results in the animation
going on forever.
img:hover {
/* Start the vibrate animation with duration 0.5 seconds */
animation: vibrate 0.5s;
/* When the animation is finished, start again */
animation-iteration-count: infinite;
}
To achieve this, we use the width or height property in CSS as shown below.
img {
width: 300px; /* set a small width */
}
Note that we are still loading a large image, and only downscaling it in the
browser, which is not ideal for quick page load time or lower page size. Let's
look at how ImageKit can be used to solve this problem and also recreate the
CSS image effects with it.
This section will demonstrate how to use the ImageKit to achieve some of the
effects implemented with regular CSS above.
image/new-york_lbAbZZKZG.jpg?tr=e-grayscale
Unlike an image converted to grayscale in the browser using CSS, the image
delivered from the image URL above is grayscale by default.
Grayscale image made using ImageKit URL transformations
Easy, right? Another advantage is that when you save or open a grayscale
image created with ImageKit URL in a new tab, it will retain the black and
white effect, unlike the grayscale image made with regular CSS.
For example, the URL below gives us a blurred image with the blur set to 7.
image/new-york_lbAbZZKZG.jpg?tr=bl-7
Again, the original image itself here is blurred and we do not need any HTML
or CSS to create this effect.
image/new-york_lbAbZZKZG.jpg?tr=r-max
You can replace the max value in the radius transformation in the URL with
any positive integer value to generate an image with the corresponding
rounded corner effect.
Image Rotation with ImageKit
ImageKit allows you to rotate an image directly from the URL at specific
angles. You simply need to pass the rt-value transformation parameter in the
URL. The rotation values allowed are 0, 90, 180, 270, 360, and auto,
representing the degrees the image should rotate.
The value auto automatically orients your image correctly using the EXIF
Orientation in your image metadata.
Image/new-york_lbAbZZKZG.jpg?tr=rt-180
This results in the following image that is rotated without using any CSS.
Image rotated by 180 degrees using ImageKit transformations
Image Thumbnails with ImageKit
When we looked at creating image thumbnails using CSS, we scaled down a
high-resolution image in the browser. This is not ideal as the browser still
needs to load a heavy image which slows down the load time and wastes the
user's bandwidth.
image/new-york_lbAbZZKZG.jpg?tr=w-200
Or we can load a 400x300 px image by specifying both width and height
parameters in our URL.
image/new-york_lbAbZZKZG.jpg?tr=w-400,h-300
There are several other resize and cropping options available in ImageKit.
You can read more about these options in this detailed dynamic image
resizing guide.
Conclusion
CSS effects are great for developers. They can save a lot of time and reduce
dependency on graphic design tools for creating cool image effects.