0

Consider I have this DOM,

<div id="banner-message">
  <p>Hello World</p>
  <button>Hover to change color</button>
</div>

I want to select p from button why below css doesn't work?

#banner-message {
  button {
    & + p {
      color: red;
    }
  }
}
Sharon Chai
  • 437
  • 5
  • 13

4 Answers4

0

Remove the &. By the way, in your SCSS, you're selecting button + p but in your markup it's p + button. See here for demo

Andy Aldo
  • 850
  • 9
  • 24
0

The code given is not working as you are traversing previous siblings. CSS selectors work for next siblings. There is no previous sibling selector. You can refer to the accepted answer for Is there a "previous sibling" CSS selector? for more information. Otherwise you will have to change the markup as mentioned by @Andy Aldo.

Ganesh Pandhere
  • 1,540
  • 7
  • 8
0

With the + selector, you are selecting the <p> AFTER the button... See this: https://jsfiddle.net/8y2mstbo/2/

Zeronull
  • 221
  • 2
  • 4
0

This is not possible with css to select previous element. so you should try script for this or you should change the structure.

Shiva
  • 172
  • 1
  • 10