Show :after when hovering over the parent element via CSS

倾然丶 夕夏残阳落幕 提交于 2019-12-05 22:34:17

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/15488827/show-after-when-hovering-over-the-parent-element-via-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!