does the order of styles matter?

前端 未结 5 1388
不知归路
不知归路 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 17:02

    CSS rules are applied in order if they have the same specificity. In your case, they do, so order matters.

    With the order you have in your question, the rules apply text-decoration: none. The second and third rules causes hovering the mouse over the link to modify those two styles in order because the a tag is inside the li tag. First, the color turns black; then, the color turns red and the underline appears.

    Reverse the order of the last two rules like so:

        .menu a
        {
            text-decoration: none;
        }
        .menu li a:hover
        {
            color: red;
            text-decoration: underline;
        }
        .menu li:hover a
        {
            color: black;
        }
    

    Now, the text-decoration: none gets applied as before. Then, upon mouse-over, the first rule changes the color to red and adds an underline, followed by the color changing to black.

提交回复
热议问题