Latest Tweets7G Firewall (beta): perishablepress.com/7g-firewal…
Perishable Press

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.

Jeff Starr
About the Author Jeff Starr = Web Developer. Security Specialist. WordPress Buff.
Archives
2 responses
  1. Jim S Smith February 21, 2019 @ 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 ]