Spring Sale! Save 30% on all books w/ code: PLANET24
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 = Web Developer. Security Specialist. WordPress Buff.
Banhammer: Protect your WordPress site against threats.

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 »
Banhammer: Protect your WordPress site against threats.
Thoughts
I live right next door to the absolute loudest car in town. And the owner loves to drive it.
8G Firewall now out of beta testing, ready for use on production sites.
It's all about that ad revenue baby.
Note to self: encrypting 500 GB of data on my iMac takes around 8 hours.
Getting back into things after a bit of a break. Currently 7° F outside. Chillz.
2024 is going to make 2020 look like a vacation. Prepare accordingly.
First snow of the year :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.