1

I have two elements in a div. One of them is hidden and I want to show it when someone hover on the visible one. Through normal CSS I have done this, but I am unable to do this through SASS.

Here is my code:

<div class"main">
<a href="#"> Link to my account </a>
<p> john </p>
</div>

I have tried it but it doesn't work for me.

.main {
    position: relative;

    a {
        font-size: 19px;
    }

    p {
        position: absolute;
        top: 0;
        left: 0;
        font-size: 18px;
        background: gray;
        padding: 2px 6px;
        visibility: hidden;
        transition: all 0.3s ease;
    }

    a:hover p {
        visibility: visible;
    }
}

So how to make the p tag visible when someone hover on the a tag using SASS?

Arkellys
  • 5,562
  • 2
  • 16
  • 40

2 Answers2

4

It's not a SASS problem, you just have to use the adjacent sibling combinator:

a:hover + p {
   visibility: visible;
}
Arkellys
  • 5,562
  • 2
  • 16
  • 40
1

The adjacent sibling selector selects all elements that are the adjacent siblings of a specified element.The following change should help you.

.main {
    position: relative;

    a {
        font-size: 19px;
    }

    p {
        position: absolute;
        top: 0;
        left: 0;
        font-size: 18px;
        background: gray;
        padding: 2px 6px;
        visibility: hidden;
        transition: all 0.3s ease;
    }

    a:hover + p {
        visibility: visible;
    }
}

Refer: https://www.w3schools.com/Css/css_combinators.asp

Narayanan Ramanathan
  • 1,310
  • 10
  • 18