Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Is it possible to make the ::after selector behave like a button?

Look at the code of below, as you can see there is a close icon floated to the right of a span element.

span {
    width: 100%;
    display: inline-block;
}

span:after {
    content: "\2715";
    float: right;
    position: absolute;
}

span:hover:after {
    cursor: pointer;
}
<span>Content</span>

I want the :after to behave like a button. As you could see, it makes the cursor a pointer on hover. How can I make it to behave like a button? For example, how can I add an onclick function to it?

like image 900
Martín Nieva Avatar asked Oct 06 '19 23:10

Martín Nieva


People also ask

What does :: After do in CSS?

::after (:after) In CSS, ::after creates a pseudo-element that is the last child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.

Can a button have a pseudo-element?

Yes you can use it – as long as you as don't need to support some very old browsers, e.g. MS IE 7 or lower. I don't know of any other browser that doesn't understand pseudo elements on empty HTML tags.

How do you keep active CSS style after clicking a button?

A link becomes active when you click on it. Tip: The :active selector can be used on all elements, not only links. Tip: Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :hover selector to style links when you mouse over them.

When would you use the :: before or :: after pseudo-element in your CSS?

The ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert. Use the ::after selector to insert something after the content.


1 Answers

Generally speaking, the pseudo element will inherit the events and event behavior assigned to the non-pseudo element that "owns" it.

So for instance, adding a click event listener to the above <span> will cause any pseudo elements of that span elemenet to inherit the same click event behavior.

If you wanted to achieve independence between the pseudo element and the "owner" element, in terms of click behavior (ie click behavior for the "pseudo" element only) you could use the pointer-events CSS property as shown below:

const span = document.querySelector("span");

span.addEventListener("click", () => alert("hey!"));
span {
    display: inline-block;
    position: relative;
    background:red;
    /* Blocks the click event from firing when span is clicked */
    pointer-events:none;
}

span:after {
    content: "'Pesudo button' - click me!";
    position: absolute;
    margin-left:1rem;
    background:green;
    width:15rem;
    text-align:center;
    color:white;
    /* Allows the click event to fire when the pesudo element is clicked */
    pointer-events:all;
}

span:hover:after {
    cursor: pointer;
}
<span>I own the pesudo element</span>
like image 89
Dacre Denny Avatar answered Sep 25 '22 04:09

Dacre Denny