Animating the logo on hover in Brine (7.0)
Custom code is not always about fixing things or breaking through Squarespace’s Style Editor boundaries.
Sometimes is just about having a bit of fun with your design.
So, for today’s CSS trick, we’re going to be looking at how we can create an awesome animated logo on hover, when using any of the Brine family templates.
If you’re working with 7.1, I encourage you to go through the tutorial to understand the reasoning behind what we’re doing so that you can adapt it to work for you! And if you want help with this or have questions along the way, I’d love to personally support you inside the Club!
Ready? Let’s go!
Note: in case you’re interested, the coffee mug icon used for the logo in this tutorial was made by Smashicons and I downloaded it from Flaticon.
The goal is to have our regular logo in place at all times and then animate it only on hover.
But, the trick is that we’re going to create the illusion that the image is animating, when in reality we’ll be using 2 separate images to create the effect: one will be a regular png, and the other one an animated gif.
Then, on hover, we’ll switch up their corresponding visibility to make it seem like the logo is “playing”.
This means that we need to upload a second image to our logo container in order to have them both in the exact same place and be able to create the effect.
And, since we can’t do that through Squarespace’s settings, we’ll have to do it via CSS.
So, the first thing we have to do is check out our current logo image and its surroundings:
Ok, so we have that our current logo is an img element with a class of .Header-branding-logo
Ideally we need to give that image a sibling so that we can overlap them and make them match in size, in order to create our effect.
But to create that sibling, because we can’t tweak the HTML, we’ll have to add it in as a pseudo-element.
This means that we need to add the pseudo-element to the direct parent container of our image in order to have it sit as a second “adopted” child.
With that in mind, let’s look at its direct parent container:
Alright, so this is an a element with a couple of classes, including one called .Header-branding that may just do the trick.
Let’s “attach” our pseudo-element to it.
In this case it doesn’t really matter if we use a ::before or an ::after since the original image and the pseudo-element won’t be displaying at the same time.
I’ll go with an ::after and start with the basics to create an empty container that has the same width and height as its direct parent container (.Header-branding):
Now, to upload our animated gif, we’ll use the background-image property along with a few other tweaks to make sure the gif is in place within the pseudo-element container we just created:
Ok!
We’re not seeing anything yet because we have to either give our ::after a display value or position it.
Since we’re looking to overlap it with the existing logo, we’ll give it a position of absolute and set its top and left offsets to 0.
It seems like our pseudo-element isn’t using .Header-branding as its reference point since even when we set it to top: 0 and left: 0, the gif isn’t aligned to the top/left corner of the original logo container.
Let’s change that by giving .Header-branding a position of relative:
Alright, much better!
However, our gif seems to be slightly bigger than the existing logo image.
We know that our ::after container has the exact same width and height of the .Header-branding container we’re attaching it to.
So, perhaps the img element itself has a different size and isn’t expanding to fit its parent container like the pseudo-element is.
Let’s take a look and see what we find:
Yep, that seems to be it.
As suspected, the img element has a max-width set (via Site Styles), so we’ll need to use that same size on our gif in order to make them identical.
So, instead of background-size: contain let’s use background-size: 195px and see how that goes:
Perfect!
Last but not least, it’s time to set up our hover mode animation.
First, we’ll hide the pseudo-element at all times by giving it an opacity of 0:
And then, when hovering over the .Header-branding container (the one holding BOTH images), we’ll make the original img element hide by giving it an opacity of 0, and then target the ::after to make it show up with an opacity of 1:
Let’s test it out!
I increased the size of the screen to make the logo more prominent:
How about that?!
We just created a super simple yet awesome-looking animated logo on hover.
I hope you liked this one!
Until next time,
B.