How to write a:hover in inline CSS?

后端 未结 23 2602
孤城傲影
孤城傲影 2020-11-21 23:15

I have a case where I must write inline CSS code, and I want to apply a hover style on an anchor.

How can I use a:hover in inline CSS inside the HTML st

23条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 23:31

    My problem was that I'm building a website which uses a lot of image-icons that have to be swapped by a different image on hover (e.g. blue-ish images turn red-ish on hover). I produced the following solution for this:

    .container div {
      width: 100px;
      height: 100px;
      background-size: 100px 100px;
    }
    .container:hover .withoutHover {
      display: none;
    }
    .container .withHover {
      display: none;
    }
    .container:hover .withHover {
      display: block;
    }

    Hover the image to see it switch with the other. Note that I deliberately used inline CSS because I decided it was the easiest and clearest solution for my problem that uses more of these image pairs (with different URL's).

    I introduced a container containing the pair of images. The first is visible and the other is hidden (display:none). When hovering the container, the first becomes hidden (display:none) and the second shows up again (display:block).

提交回复
热议问题