does the order of styles matter?

前端 未结 5 1393
不知归路
不知归路 2021-01-31 16:27

below is my markup. when i move the mouse over the hyperlinks they get underlined and turn red. but if i swap the order of the last two rules, the hyperlinks still get underline

5条回答
  •  一个人的身影
    2021-01-31 16:51

    Yes the order matters, and in this case it is not really the order which is why you are having the same result regardless of the order.

    The .menu li:hover a is applied to the li, which is a parent of the a and the hover is not applied to the a it is applied to the li.

    The .menu li a:hover is applied to the a.

    Regardless of the order the .menu li a:hover style will be applied to the a.

    The better way to handle that is to have the hover pseudo selector applied to only the a element and make set display: block on it, with height and width set to 100%. This will fill the entire LI

    Hope this clarifies things.

提交回复
热议问题