Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Change color of ":after" element on hover

Tags:

Lets say i have a list of elements :

<ol>
    <li>First</li>
    <li>Second</li>
    <li>Third</li>
</ol>

When i hover a list item , i need to be able to get more info about each subject. I did this with CSS :after pseudo-element .

ol {
    list-style-type: decimal-leading-zero;
}

li:hover:after {
    content: " + More info";
}

Here is a jSfiddle

Now when i hover on the More info text , i want to change it's color . I don't really get it how to acheive this effect . i have tried the following css rules , but it does not work as expected .

li:after:hover {
    color: red;
}

li:hover:after:hover {
    color: red;
}

Is it possible to do in CSS ?

If yes , another question is interesting , can i attach an onclick event to the :after element ?

like image 457
Alexander Avatar asked Nov 13 '13 11:11

Alexander


People also ask

How do I hover after CSS?

The :before and :after selectors in CSS is used to add content before and after an element. The :hover is pseudo-class and :before & :after are pseudo-elements. In CSS, pseudo-elements are written after pseudo-class. In CSS3 double colon(::) is used to denote pseudo-element.

How do I change the hover color?

To change the color of your link on hover, use the :hover pseudo property on the link's class and give it a different color.

What is :: before and :: after in 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.


2 Answers

this works perfectly

.topButton:hover:after{
    background-color: #000;
}
like image 104
kyle Avatar answered Oct 13 '22 09:10

kyle


As per ExtPro's comment, you can't

But you can do like...

HTML :

<ol>
    <li>First<span> + More info</span></li>
    <li>Second<span> + More info</span></li>
    <li>Third<span> + More info</span></li>
</ol>

CSS :

li > span {
    display:none;
}

li:hover > span {
    display:inline;
}

li > span:hover {
    color:red;
}

Use :active as onClick ?

li > span:active {
    color:blue;
}

Demo : http://jsfiddle.net/79Lvn/2/


or JS/jQuery way ? just...

$('ol > li').append('<span> + More info</span>');

Demo : http://jsfiddle.net/79Lvn/4/

like image 36
l2aelba Avatar answered Oct 13 '22 08:10

l2aelba