Advertisement
  1. Web Design
  2. HTML/CSS
  3. HTML Templates

Exploring Creative CSS Hover Effects for Inline Links

Scroll to top

In this tutorial we’re talking about CSS hover effects. And not just any hover effects either! We’re going to create some much more interesting alternatives to the standard inline link effects we’ve all been using for years.

Creative Link Hover Demos

I’ve created a quick demo to show all these link effects; a series of list items arranged with CSS Grid, and in each one I’ll place an inline link which can be styled. Take a look at each one, feel free to fork the demo, and see what you can do with them!

CSS Inline Link Hover Effects

Check out the video, or read on for more explanations!

Global Styles

For all these demos I first set some global styles and variables for the a elements:

1
/* Variables */
2
:root {
3
  --link-1: #D65472;
4
  --link-2: #37C5F0;
5
  --link-3: gold;
6
  --text: #18272F;
7
  --counter: #30B67D;
8
}
9
10
/* Reset */
11
a {
12
	text-decoration: none;
13
	color: var(--text);
14
	font-weight: 700;
15
	vertical-align: top;
16
}
I’ve added an id to each link so I can target them all easily.

Hover Effect 1: Background Box Shadow

This effect will swipe a background box shadow across the inline link, changing the color of the link text as it does so.

hover effect 1hover effect 1hover effect 1
  • We begin by adding some padding all around the link, then to prevent that padding upsetting the flow of the text we add a negative margin of the same value.
  • Instead of using the background property we use box-shadow because we can transition it.
1
#style-1 {
2
  padding: 0 0.25rem;
3
  margin: 0 -0.25rem;
4
  box-shadow: inset 0 0 0 0 var(--link-1);
5
  transition: color 0.3s ease-in-out, box-shadow 0.3s ease-in-out;
6
  color: var(--link-1);
7
}
8
#style-1:hover {
9
  color: white;
10
  box-shadow: inset 100px 0 0 0 var(--link-1);
11
}

Hover Effect 2: Animated Underline

Our second effect adds an underline, but animates it from the middle of the text outwards for some extra dynamism.

hover effect 2hover effect 2hover effect 2
  • The first thing to do is give the anchor a position: relative; because we’ll be using pseudo elements which we want to position on it.
  • We use the ::before pseudo element which we position top: 100%; to make it sit along the bottom of the link.
  • We use transform: scaleX(); and transition to animate the pseudo element.
  • We transition the color of the link too, to match the underline animation.
1
#style-2 {
2
  position: relative;
3
  transition: color 0.3s ease-in-out;
4
}
5
#style-2::before {
6
  content: "";
7
  position: absolute;
8
  top: 100%;
9
  width: 100%;
10
  height: 3px;
11
  background-color: var(--link-1);
12
  transform: scaleX(0);
13
  transition: transform 0.3s ease-in-out;
14
}
15
#style-2:hover {
16
  color: var(--link-1);
17
}
18
#style-2:hover::before {
19
  transform: scaleX(1);
20
}

Hover Effect 3: Passing Underline

When you hover over this link you’ll see the underline animate in from the left, then disappear to the right.

hover effect 3hover effect 3hover effect 3
  • We start again with position: relative; on the parent element because we’ll be working with a pseudo element.
  • We style the ::before with some basic rules, including a border-radius: 4px; just to give it a slightly softer look.
  • We’ll use the same transition transform idea as the previous demo (transform: scaleX(0);) but we’ll change the transform origins.
  • So we begin by setting transform-origin: right; on the ::before element.
  • Then on the :hover we use transform-origin: left;.
1
#style-3 {
2
  position: relative;
3
}
4
#style-3::before {
5
  content: "";
6
  position: absolute;
7
  width: 100%;
8
  height: 4px;
9
  border-radius: 4px;
10
  background-color: var(--text);
11
  bottom: 0;
12
  left: 0;
13
  transform-origin: right;
14
  transform: scaleX(0);
15
  transition: transform 0.3s ease-in-out;
16
}
17
#style-3:hover::before {
18
  transform-origin: left;
19
  transform: scaleX(1);
20
}

Hover Effect 4: Text Replace

This hover effect is the most complex and the first one we’ll change the markup for. We’ll be replacing the link text with whatever we store in a data-attribute on the link.

hover effect 4hover effect 4hover effect 4
  • Start with the usual position: relative; style on the parent.
  • Define basic styles for the ::before and ::after pseudo elements.
  • Our ::before element will be the underline.
  • The ::after element will hold the text we set in the data-replace="" attribute (which will match the link text). We set this with content: attr(data-replace);.
  • We push the ::after element to the right by using transform: translate3d(); (to take advantage of hardware acceleration).
  • We also need to make it invisible which we do with an overflow: hidden; in combination with display: inline-block; on the parent.
  • On :hover we move the ::after element back into position.
  • We wrap the text link in a <span> element, which we then transition out of the link as the ::after element transitions in, and vice versa.
1
#style-4 {
2
  overflow: hidden;
3
  position: relative;
4
  display: inline-block;
5
}
6
#style-4::before, #style-4::after {
7
  content: "";
8
  position: absolute;
9
  width: 100%;
10
  left: 0;
11
}
12
#style-4::before {
13
  background-color: var(--link-1);
14
  height: 2px;
15
  bottom: 0;
16
  transform-origin: 100% 50%;
17
  transform: scaleX(0);
18
  transition: transform 0.3s cubic-bezier(0.76, 0, 0.24, 1);
19
}
20
#style-4:hover::before {
21
  transform-origin: 0% 50%;
22
  transform: scaleX(1);
23
}
24
#style-4::after {
25
  content: attr(data-replace);
26
  height: 100%;
27
  top: 0;
28
  transform-origin: 100% 50%;
29
  transform: translate3d(200%, 0, 0);
30
  transition: transform 0.3s cubic-bezier(0.76, 0, 0.24, 1);
31
  color: var(--link-1);
32
}
33
#style-4:hover::after {
34
  transform: translate3d(0, 0, 0);
35
}
36
#style-4 span {
37
  display: inline-block;
38
  transition: transform 0.3s cubic-bezier(0.76, 0, 0.24, 1);
39
}
40
#style-4:hover span {
41
  transform: translate3d(-200%, 0, 0);
42
}

Hover Effect 5: Multiple Properties

Let’s make things a little simpler again. This example uses a handful of different properties to give a growing offset background effect.

hover effect 5hover effect 5hover effect 5
  • Begin with the usual setup styles which we need to manipulate pseudo elements.
  • Set a z-index: -1; to send the block behind the link text.
  • Offset the ::before element on hover by using a negative margin, and expressing the width as calc(100% + 10px);
1
#style-5 {
2
  position: relative;
3
  font-weight: bold;
4
}
5
#style-5::before {
6
  content: "";
7
  background-color: var(--link-3);
8
  position: absolute;
9
  left: 0.5rem;
10
  bottom: 5px;
11
  width: 100%;
12
  height: 8px;
13
  z-index: -1;
14
  transition: all 0.3s ease-in-out;
15
}
16
#style-5:hover::before {
17
  left: -5px;
18
  bottom: 0;
19
  height: 100%;
20
  width: calc(100% + 10px);
21
}

Hover Effect 6: Multi-Line Gradient

The effects so far work well when the inline links sit on one line, but what if the link spreads across multiple lines? Let’s have a play with an effect which works in that scenario.

hover effect 6hover effect 6hover effect 6
  • Use a long link to best see the effect for this one.
  • We apply a background gradient for the bottom border.
  • It looks like this: background-image: linear-gradient(white 50%, var(--link-2) 50%); and gives us a background which is half white, half blue to start with.
  • We then use a background-size: auto 175%; to scale the background gradient 175% vertically. This effectively enlarges the whole background, which is aligned to the top edge, cropping much of the blue in the process. In this way we create our underline.
  • On hover, we position the background so that it aligns with the bottom of the link instead.
1
#style-6 {
2
  background-image: linear-gradient(white 50%, var(--link-2) 50%);
3
  background-size: auto 175%;
4
  transition: background 0.3s ease-in-out;
5
}
6
#style-6:hover {
7
  background-position-y: 100%;
8
}

Conclusion

Nothing beats a good practical tutorial eh? I hope you enjoyed creating these link effects and developing your skills as you progressed through them. Don’t forget you can watch the video at the top of the page for a more visual explanation of these hover effects. Thanks for watching/reading!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.