Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

How to make a pseudo element in a link also clickable?

Hello,

I wonder if there is an option to make my menu fully working. Basically, I am trying to achieve the flipping links effect in menu.

I have made the basic list markup and added some links with pseudo element on them. I worked out the animation on hover. But after the links are flipped, they are no longer "links" and are not clickable anymore.

Maybe someone can help me with this?

Here is a code on codepen http://codepen.io/MyCarrera/full/PqRqrr/

And here is the source code:

HTML:

<ul id="main-menu">
  <li><a href="#">Главная</a></li>
  <li><a href="#">Новости</a></li>
  <li><a href="#">Контакты</a></li>
  <li><a href="#">Ссылки</a></li>
</ul>

CSS:

ul {
  display: block;
  list-style: none;
  margin: 0;
  padding: 0;
  background: #3db2e1;
  margin: 10px;
  padding: 0;
}

ul li {
  display: inline-block;
  margin: 0;
  padding: 0;
}

ul li a {
  display: block;
  padding: 15px 20px;
  line-height: 20px;
  color: #fff;
  background: #3db2e1;
  font-family: Raleway, sans-serif;
  line-height: 1;
  font-size: 14px;
  letter-spacing: 1px;
  text-transform: uppercase;
  text-decoration: none;
  overflow: visible;
  transition: all .3s;
  transform-origin: 50% 0;
  transform-style: preserve-3d;
}

ul li:hover a,
ul li a:hover {
  color: #dff2fa;
  transform: rotateX(90deg) translateY(-23px);
}

ul li a::after {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: -1;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding: 16px 20px;
  color: #dff2fa;
  background: #19799f;
  content: attr(data-title);
  transition: background 0.3s;
  transform: rotateX(-90deg);
  transform-origin: 50% 0;
}

JS:

$(document).ready(function() {
 $("#main-menu a").each(function() {
    var linkTitle = $(this).text();
    $(this).attr('data-title', linkTitle);
  });
});

1 Answer

Erwin Meesters
Erwin Meesters
15,088 Points

I don't think it's possible with the provided syntax. pseudo-elements are not part of the DOM so you can't bind any events directly to them, you can only bind to their parent elements. By changing your html a bit you can achieve the same results with the animation effect and keep the links clickable. Check out this post by David Walsh: http://davidwalsh.name/3d-menu