How to write a:hover in inline CSS?

后端 未结 23 2573
孤城傲影
孤城傲影 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

    According to your comments, you're sending a JavaScript file anyway. Do the rollover in JavaScript. jQuery's $.hover() method makes it easy, as does every other JavaScript wrapper. It's not too hard in straight JavaScript either.

    0 讨论(0)
  • 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;
    }
    <p>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).
    </p>
    <div class=container>
    <div class=withHover style="background-image: url('https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQrqRsWFJ3492s0t0NmPEcpTQYTqNnH188R606cLOHm8H2pUGlH')"></div>
    <div class=withoutHover style="background-image: url('http://i.telegraph.co.uk/multimedia/archive/03523/Cat-Photo-Bombs-fa_3523609b.jpg')"></div>
    </div>

    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).

    0 讨论(0)
  • 2020-11-21 23:33

    You could do it at some point in the past. But now (according to the latest revision of the same standard, which is Candidate Recommendation) you can't .

    0 讨论(0)
  • 2020-11-21 23:33

    You can do this. But not in inline styles. You can use onmouseover and onmouseout events:

    <div style="background: #333; padding: 10px; cursor: pointer"
       onmouseover="this.style.backgroundColor='#555';" onmouseout="this.style.backgroundColor='#333';">
          Hover on me!
    </div>

    0 讨论(0)
  • 2020-11-21 23:34

    I just figured out a different solution.

    My issue: I have an <a> tag around some slides/main content viewer as well as <a> tags in the footer. I want them to go to the same place in IE, so the whole paragraph would be underlined onHover, even though they're not links: the slide as a whole is a link. IE doesn't know the difference. I also have some actual links in my footer that do need the underline and color change onHover. I thought I would have to put styles inline with the footer tags to make the color change, but advice from above suggests that this is impossible.

    Solution: I gave the footer links two different classes, and my problem was solved. I was able to have the onHover color change in one class, have the slides onHover have no color change/underline, and still able to have the external HREFS in the footer and the slides at the same time!

    0 讨论(0)
  • 2020-11-21 23:35

    You can use the pseudo-class a:hover in external style sheets only. Therefore I recommend using an external style sheet. The code is:

    a:hover {color:#FF00FF;}   /* Mouse-over link */
    
    0 讨论(0)
提交回复
热议问题