Combine :after with :hover

CssCss SelectorsPseudo Element

Css Problem Overview


I want to combine :after with :hover in CSS (or any other pseudo selector). I basically have a list and the item with the selected class has an arrow shape applied using :after. I want the same to be true for objects that are being hovered over but cant quite get it to work. Heres the code

#alertlist {
  list-style: none;
  width: 250px;
}

#alertlist li {
  padding: 5px 10px;
  border-bottom: 1px solid #e9e9e9;
  position: relative;
}

#alertlist li.selected,
#alertlist li:hover {
  color: #f0f0f0;
  background-color: #303030;
}

#alertlist li.selected:after {
  position: absolute;
  top: 0;
  right: -10px;
  bottom: 0;
  border-top: 10px solid transparent;
  border-bottom: 10px solid transparent;
  border-left: 10px solid #303030;
  content: "";
}

<ul id="alertlist">
  <li>Alert 123</li>
  <li class="selected">Alert 123</li>
  <li>Alert 123</li>
</ul>

Css Solutions


Solution 1 - Css

Just append :after to your #alertlist li:hover selector the same way you do with your #alertlist li.selected selector:

#alertlist li.selected:after, #alertlist li:hover:after
{
    position:absolute;
    top: 0;
    right:-10px;
    bottom:0;

    border-top: 10px solid transparent;
    border-bottom: 10px solid transparent;
    border-left: 10px solid #303030;
    content: "";
}

Solution 2 - Css

in scss

&::after{
content: url(images/RelativeProjectsArr.png);
margin-left:30px;
}

&:hover{
	background-color:$turkiz;
	color:#e5e7ef;

	&::after{
	content: url(images/RelativeProjectsArrHover.png);
	}
}

Solution 3 - Css

 #alertlist li:hover:after,#alertlist li.selected:after
{
    position:absolute;
    top: 0;
    right:-10px;
    bottom:0;

    border-top: 10px solid transparent;
    border-bottom: 10px solid transparent;
    border-left: 10px solid #303030;
    content: "";
}​

jsFiddle Link

Solution 4 - Css

cssSelector:hover::after,

cssSelector:hover::before

lets say we have a tag

a:hover::after {
    /*CSS Property*/
}
a:hover::before {
    /*CSS Property*/
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChrisView Question on Stackoverflow
Solution 1 - CssBoltClockView Answer on Stackoverflow
Solution 2 - CssErez LiebermanView Answer on Stackoverflow
Solution 3 - CssKishore KumarView Answer on Stackoverflow
Solution 4 - CssT.PraveenView Answer on Stackoverflow