1

I have this code:

html:

<div class="item">
  <p>hello</p>
</div>

CSS:

.item {
    border: 2px dashed green;
    background-color: red;
    width: 100px;
    height: 100px;
    margin-left: 30px;
    margin-top: 30px;
}

.item :hover  {
   background-color: yellow;
}

I'm trying to change all of the item's color to the yellow on hover. However, only the text's background color changes:

enter image description here

while I want this:

enter image description here

Fiddle: https://jsfiddle.net/87qd0au9/

parsecer
  • 4,758
  • 13
  • 71
  • 140

2 Answers2

1

remove the space between item and hover

.item:hover  {
  background-color: yellow;
}
bill.gates
  • 14,145
  • 3
  • 19
  • 47
1

Simply remove the space between item and :hover.

Explanation: In CSS, the space character is a "child selector", meaning that with the space you are not targeting the item div, but its children.

Anis R.
  • 6,656
  • 2
  • 15
  • 37