Tailwind zooming background images

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Thanks Catalin, glad you like it! Tailwind is amazing, the more I use it, the more I love it
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

You might be familiar with this pretty cool background zoom effect, you hover a card, and the image zooms towards you?
Well, today, you will learn how to make this yourself in Tailwind CSS!
The end result will look like this:

Let's start by looking at the HTML structure and how we would format this.
We will need cards that have an image and some text inside. Each card will look like this.
<div class="card-zoom">
<div class="card-zoom-image"></div>
<h1 class="card-zoom-text">CAR</h1>
</div>
As you may have spotted, I'm not using inline classes since we will be re-using these blocks multiple times.
Before we continue, let's see how we can add custom background-images to our Tailwind CSS.
Open up your tailwind config, and add an extend option for backgroundImages.
module.exports = {
theme: {
extend: {
backgroundImage: {
beetle:
"url('https://custom.image/name.jpg')"
},
},
},
variants: {},
plugins: [],
}
That is how we can add custom background images for our Tailwind config. We can then use these images using the bg sub class.
<div class="card-zoom-image bg-beetle"></div>
You can see that we use the bg prefix with the name we put in our Tailwind config file.
Now let's get working on adding some generic styling for our cards.
We will use the @apply rule to make sure we only use Tailwind classes.
The first element is the card-zoom class. It's the main wrapper for the image and the text.
.card-zoom {
@apply relative flex items-center justify-center m-3 overflow-hidden shadow-xl w-60 h-60 rounded-2xl;
}
This will make sure everything inside the card-zoom div is centered, and it has rounded corners with a nice shadow effect.
The next element will be the image inside. This has to be an absolute class since we will be zooming the whole div on hover.
.card-zoom-image {
@apply absolute w-full h-full transition-all duration-500 ease-in-out transform bg-center bg-cover;
}
As you can see, we make it absolute and the full size of the parent element. We then add transition and a transform to give it a nice animated effect later on.
Then we have the text left. This again must be an absolute element since we will be animating this as well.
.card-zoom-text {
@apply absolute text-5xl font-black transition-all duration-500 ease-in-out transform scale-150 text-gray-50 opacity-60;
}
One of the reasons I went with the separate classes is since we want to add the hover on the parent class.
So once we hover the card-zoom, the inner image and text element should start animating.
We can add these animations using the following classes.
.card-zoom:hover .card-zoom-image {
@apply scale-150;
}
.card-zoom:hover .card-zoom-text {
@apply scale-100;
}
This will make sure the image zooms to be bigger, and the text will zoom smaller, giving it a cool background zoom effect.
If you want to check out the full demo, check out this Tailwind CSS Playground demo
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter