Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the area of CSS pseudo-element clickable?

Tags:

Here's the FIDDLE for play around.

I have created <div class="foo"> and have a generated CSS content using .foo:after.
I want to have that generated content clickable by setting a link.

If I wrap the .foo with an anchor it creates a link around .foo and .foo:after.
However I want to make the area of .foo:after clickable, but not the .foo itself.

Is there a way that I can achieve this using pure CSS? Perhaps changing the markup?

HTML

<div class="container">     <a href="http://example.com">         <div class="foo"></div>     </a> </div> 

CSS

.foo{     width: 400px;     height: 150px;     background-color: #DFBDE0; }  .foo:after{     content: "";     position: absolute;     background-color: #CB61CF;     width: 100px;     height: 100px;     right: 0; } 

Screeshot

enter image description here

like image 747
aniskhan001 Avatar asked Aug 23 '14 18:08

aniskhan001


People also ask

Are pseudo elements clickable?

The answer is no.

How do I center a pseudo element in CSS?

Using margin:auto to center. As long as the element has a width declared you can use the absolute centering method. To use this method the right and left properties must be set to 0 for margin: auto to be effective.

Can you hover a pseudo element?

To clarify, you CAN NOT give :hover to a pseudo element. There's no such thing as ::after:hover in CSS as of 2018.


1 Answers

This should work, it stops the link being clickable but then allows it on the pseudo element using the pointer-events attribute.

a {   pointer-events: none; }  .foo:after{     pointer-events: all; } 

You should put a class on the link so that it doesn't affect all links.

like image 60
matchboxhero Avatar answered Sep 21 '22 04:09

matchboxhero