Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show :after when hovering over the parent element via CSS

Is it possible to display:block; the :after pseudo-element when I hover over the parent element itself?

#myDiv{
   position:relative;
   width:50px;
   height:50px;
   border-style:solid;
   background-color:red;
}
#myDiv:after{
   position:absolute;
   content:"";
   display:none;
   width:50px;
   height:50px;
   border-style:solid;
   background-color:blue;
}

I have tired:

#myDiv:hover #myDiv:after{
    display:block;
}
#myDiv:hover + #myDiv:after{
    display:block;
}
#myDiv:hover > #myDiv:after{
    display:block;
}

Yet no luck, here's a fiddle.

like image 733
Ali Bassam Avatar asked Mar 18 '13 23:03

Ali Bassam


People also ask

How do you make something appear when you hover over it CSS?

To display div element using CSS on hover a tag: First, set the div element invisible i.e display:none;. By using the adjacent sibling selector and hover on a tag to display the div element.

What does :: After mean in CSS?

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.

What is hover called in CSS?

The CSS :hover selector is one of many pseudo-classes that are used to style elements. :hover is used to select elements that users hover their cursor or mouse over. It can be used on all elements, not only on links.


2 Answers

Change it to #myDiv:hover::after

You can use either :after or ::after but in the selectors module (3) it states that the latter is now intended to be used to distinguish them from pseudo-classes

like image 183
user2129623 Avatar answered Nov 15 '22 21:11

user2129623


You are trying to reference the same element, so you don't need to duplicate the ID selector. No need to use the child selector either, just use:

#myDiv:hover:after {
    display: block;
}

jsFiddle Demo

like image 36
kapa Avatar answered Nov 15 '22 23:11

kapa