代码之家  ›  专栏  ›  技术社区  ›  adbleke

悬停时DIV背景颜色更改

  •  -5
  • adbleke  · 技术社区  · 7 年前

    我在这里看了一些帖子,但没有成功地让我的div在鼠标悬停时改变背景色。这有点凌乱,但请容忍我。以下是html/php代码:

    <a class="hoverTwo" href="https:/doltesting.000webhostapp.com/pageTwo.php">
      <div class="hoverTwo" class="secondSection">
        <p class="hoverTwo" style="background: lightblue;
               border: solid;
               text-align: center;
               font-family: Comic Sans MS, Comic Sans, cursive;">
          <br><br><br>
         <?php=
           echo "Section Two - Unknown";
          ?>
          <br><br><br><br><br>
        </p>
      </div>
    </a>
    

    下面是CSS:

    a.hoverTwo a:hover a:visited a:link {
    background: yellow;
    color: black; }
    
    3 回复  |  直到 7 年前
        1
  •  0
  •   RickL    7 年前

    请尝试以下步骤开始,并记住内联样式会覆盖css:

    <a href="https:/doltesting.000webhostapp.com/pageTwo.php">
        <div class="secondSection">
            <p class="hoverTwo">
                <br><br><br>
                SOME TEXT
                <br><br><br>
            </p>
        </div>
    </a>
    

    在css中包含以下内容:

    .hoverTwo { background-color:lightblue;color:green; }
    .hoverTwo:hover{ background-color:yellow;color:black; }
    
        2
  •  0
  •   A Franklin    7 年前

    将此添加到css

    .hoverTwo:hover {
         background-color: blue;
    }
    

    此外,您现有的css中也存在一些错误。注意我添加的逗号。也不是说a:悬停将设置为与a本身相同的颜色。所以悬停时不会有颜色变化。

    a.hoverTwo, a:hover, a:visited, a:link {
        background: yellow;
        color: black;
    }
    
        3
  •  0
  •   adbleke    7 年前

    谢谢你的帮助。以下是有效的方法:

    <div id="sectionTwoPage">
    

    和css:

    #sectionTwoPage:hover {
    background-color: silver;
    }