Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
Web Dev + WordPress + Security

CSS Background Hover Slide Effect

While working on the site’s 24th redesign, I played around with a number of styles to customize the appearance of links. As you can see by hovering over any link, I decided to keep the styles as minimal as possible while still letting the user know that, “hey, this is a link”. This quick post shares one of the link styles I was considering, it’s sort of a “slide-up” background-color effect that happens when the user hovers over the hyperlink.

Demo

Code

So if you like the demo and want to use the code or just play around whatever, you can inspect the demo source code or grab it from here:

a {
	padding: 5px; 
	display: inline-block;
	text-decoration: none; 
	transition: all 0.3s;
	background-size: 100% 200%; 
	background-image: linear-gradient(to top, #333 50%, #fff 50%); 
	color: #333;
	}
a:hover { 
	background-position: 0 100%; 
	color: #fff; 
	}

The only real “trick” here is using linear-gradient for the background-image, and doubling the background height @ 200%. The transition property is added to give the “sliding” effect, instead of just jumping back and forth between background styles. All the other CSS is added for general styling and clarity. Feel free to experiment, I know just from experimenting with border styles, colors, and so forth, it’s possible to enhance the background slide effect and make some pretty sweet looking anchor tags.

Bonus

A lot of times you want to target just certain links, or not target certain links. When I was experimenting with the hover background link styles, there were some links that I wanted to exclude (like the site header logo, sidebar ads, image links, and so forth). So I had two choices:

  • Add a class to every link that should have the hover effect
  • OR, add a class to the few links that should NOT have the hover effect

Of these, the easiest, least invasive, obvious choice was to just add an .exclude class to the few links that should not be styled. Going that route, I also needed to modify the hover-effect CSS code:

a:not(.exclude) {
	padding: 5px; 
	display: inline-block;
	text-decoration: none; 
	transition: all 0.3s;
	background-size: 100% 200%; 
	background-image: linear-gradient(to top, #333 50%, #fff 50%); 
	color: #333;
	}
a:not(.exclude):hover { 
	background-position: 0 100%; 
	color: #fff; 
	}

Notice the only difference here: we refined the two anchor selectors by adding :not(.exclude) to each. So now the styles will be applied to any link that doesn’t have an exclude class. So now you have the power.

About the Author
Jeff Starr = Designer. Developer. Producer. Writer. Editor. Etc.
BBQ Pro: The fastest firewall to protect your WordPress.

2 responses to “CSS Background Hover Slide Effect”

  1. Jim S Smith 2019/02/21 8:55 pm

    Nice little primer (or refresher, for some) on some of the finer qualities of CSS3.

    Sometimes,

    I’ll use the hover effects on certain elements that have a filled in “title” attribute. Makes for a great “Hey! Over here.” message.

    I also definitely appreciate that we have the “transition” property to play with. It’s nice to be able to do from CSS, instead of being reliant on JavaScript for EVERY effect.

    – Jim

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
.htaccess made easy: Improve site performance and security.
Thoughts
I disabled AI in Google search results. It was making me lazy.
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources. Also: “The Web” is not a valid answer.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.